Compare commits
	
		
			3 Commits
		
	
	
		
			session_br
			...
			master
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 59e1016123 | |||
| 3cfcb721e8 | |||
| c607b837b9 | 
							
								
								
									
										12
									
								
								main.go
									
									
									
									
									
								
							
							
						
						
									
										12
									
								
								main.go
									
									
									
									
									
								
							| @@ -17,7 +17,7 @@ import ( | ||||
| 	"net/http" | ||||
|  | ||||
| 	"streifling.com/jason/sicherheitsunterweisung/packages/data" | ||||
| 	"streifling.com/jason/sicherheitsunterweisung/packages/session" | ||||
| 	"streifling.com/jason/sicherheitsunterweisung/packages/server" | ||||
| ) | ||||
|  | ||||
| func main() { | ||||
| @@ -26,16 +26,16 @@ func main() { | ||||
| 		log.Fatalln(err) | ||||
| 	} | ||||
|  | ||||
| 	mux := session.NewMux() | ||||
| 	sessions := make([]*session.Session, 0) | ||||
| 	sessionChan := make(chan *session.Session) | ||||
| 	mux := server.NewMux() | ||||
| 	sessions := make([]*server.Session, 0) | ||||
| 	sessionChan := make(chan *server.Session) | ||||
|  | ||||
| 	mux.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static/")))) | ||||
| 	mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { | ||||
| 		template.Must(template.ParseFiles("templates/index.html", "templates/login.html")).Execute(w, nil) | ||||
| 	}) | ||||
| 	mux.HandleFunc("/internal-login/", session.HandleInternalLogin(db, &sessions, sessionChan)) | ||||
| 	mux.HandleFunc("/external-login/", session.HandleExternalLogin(&sessions)) | ||||
| 	mux.HandleFunc("/internal-login/", server.HandleInternalLogin(db, &sessions, sessionChan)) | ||||
| 	mux.HandleFunc("/external-login/", server.HandleExternalLogin(&sessions)) | ||||
|  | ||||
| 	go mux.HandleSessions(db, sessionChan, &sessions) | ||||
|  | ||||
|   | ||||
| @@ -18,38 +18,38 @@ type DB struct { | ||||
| } | ||||
|  | ||||
| type Person struct { | ||||
| 	ID        int64 | ||||
| 	FirstName string | ||||
| 	LastName  string | ||||
| 	ID        int64 | ||||
| } | ||||
|  | ||||
| type Instructor Person | ||||
|  | ||||
| type Participant struct { | ||||
| 	Person | ||||
| 	Company string | ||||
| 	Person | ||||
| } | ||||
|  | ||||
| type Briefing struct { | ||||
| 	ID           int64 | ||||
| 	DateTime     string | ||||
| 	Location     string | ||||
| 	Document     string | ||||
| 	AsOf         string | ||||
| 	InstructorID int64 | ||||
| 	ID           int64 | ||||
| } | ||||
|  | ||||
| type Answer struct { | ||||
| 	ID      int64 | ||||
| 	Text    string | ||||
| 	IsImage bool // TODO: relocate to sessionStructs if possible | ||||
| 	ID      int64 | ||||
| } | ||||
|  | ||||
| type Question struct { | ||||
| 	ID      int64 | ||||
| 	Text    string | ||||
| 	Answers []Answer | ||||
| 	Correct int | ||||
| 	ID      int64 | ||||
| } | ||||
|  | ||||
| type GivenAnswer struct { | ||||
|   | ||||
| @@ -9,7 +9,7 @@ | ||||
|  * You should have received a copy of the GNU General Public License along with this program.  If not, see <http://www.gnu.org/licenses/>. | ||||
|  */ | ||||
| 
 | ||||
| package session | ||||
| package server | ||||
| 
 | ||||
| import ( | ||||
| 	"crypto/rand" | ||||
| @@ -41,7 +41,7 @@ func generateLogin() (string, error) { | ||||
| 	return hex.EncodeToString(bs), nil | ||||
| } | ||||
| 
 | ||||
| func (b *Briefing) HandleNewParticipant(cp chan<- *Participant) http.HandlerFunc { | ||||
| func (b *Briefing) handleNewParticipant(cp chan<- *Participant) http.HandlerFunc { | ||||
| 	return func(w http.ResponseWriter, r *http.Request) { | ||||
| 		var err error | ||||
| 		participant := Participant{ | ||||
| @@ -67,7 +67,7 @@ func (b *Briefing) HandleNewParticipant(cp chan<- *Participant) http.HandlerFunc | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| func (b *Briefing) HandleBriefingForm(db *data.DB, s *Session) http.HandlerFunc { | ||||
| func (b *Briefing) handleBriefingForm(db *data.DB, s *Session) http.HandlerFunc { | ||||
| 	return func(w http.ResponseWriter, r *http.Request) { | ||||
| 		b.DateTime = time.Now().Format("2006-01-02 15:04:05") | ||||
| 		b.Location = r.PostFormValue("location") | ||||
| @@ -9,7 +9,7 @@ | ||||
|  * You should have received a copy of the GNU General Public License along with this program.  If not, see <http://www.gnu.org/licenses/>. | ||||
|  */ | ||||
| 
 | ||||
| package session | ||||
| package server | ||||
| 
 | ||||
| import ( | ||||
| 	"github.com/google/uuid" | ||||
| @@ -17,27 +17,27 @@ import ( | ||||
| ) | ||||
| 
 | ||||
| type tableHTMLData struct { | ||||
| 	SessionID uuid.UUID | ||||
| 	OTD       []data.OverviewTableData | ||||
| 	SessionID uuid.UUID | ||||
| } | ||||
| 
 | ||||
| type participantHTMLData struct { | ||||
| 	BriefingID uuid.UUID | ||||
| 	Participant | ||||
| 	BriefingID uuid.UUID | ||||
| } | ||||
| 
 | ||||
| type questionHTMLData struct { | ||||
| 	Question data.Question | ||||
| 	participantHTMLData | ||||
| 	QuestionID int64 | ||||
| 	Question   data.Question | ||||
| } | ||||
| 
 | ||||
| type resultAnswer struct { | ||||
| 	ID      int64 | ||||
| 	Text    string | ||||
| 	Correct bool | ||||
| 	Chosen  bool | ||||
| 	IsImage bool // TODO: relocate to sessionStructs if possible | ||||
| 	ID      int64 | ||||
| } | ||||
| 
 | ||||
| type resultQuestion struct { | ||||
| @@ -46,12 +46,12 @@ type resultQuestion struct { | ||||
| } | ||||
| 
 | ||||
| type resultHTMLData struct { | ||||
| 	participantHTMLData | ||||
| 	Questions []resultQuestion | ||||
| 	participantHTMLData | ||||
| } | ||||
| 
 | ||||
| type summaryHTMLData struct { | ||||
| 	ParticipantsData []participantHTMLData | ||||
| 	SessionID        uuid.UUID | ||||
| 	BriefingID       uuid.UUID | ||||
| 	ParticipantsData []participantHTMLData | ||||
| } | ||||
| @@ -9,7 +9,7 @@ | ||||
|  * You should have received a copy of the GNU General Public License along with this program.  If not, see <http://www.gnu.org/licenses/>. | ||||
|  */ | ||||
| 
 | ||||
| package session | ||||
| package server | ||||
| 
 | ||||
| import ( | ||||
| 	"fmt" | ||||
| @@ -9,7 +9,7 @@ | ||||
|  * You should have received a copy of the GNU General Public License along with this program.  If not, see <http://www.gnu.org/licenses/>. | ||||
|  */ | ||||
| 
 | ||||
| package session | ||||
| package server | ||||
| 
 | ||||
| import ( | ||||
| 	"fmt" | ||||
| @@ -32,15 +32,15 @@ func (mux *Mux) handleParticipants(db *data.DB, cp <-chan *Participant, b *Brief | ||||
| 	for p := range cp { | ||||
| 		p.GivenAnswers = make([]int, len(b.Questions)) | ||||
| 
 | ||||
| 		mux.HandleFunc("/submit-participant/"+fmt.Sprint(b.UUID)+"/"+fmt.Sprint(p.Login)+"/", p.HandleParticipant(db, b)) | ||||
| 		mux.HandleFunc("/end-video/"+fmt.Sprint(b.UUID)+"/"+fmt.Sprint(p.Login)+"/", p.HandleEndOfVideo(b)) | ||||
| 		mux.HandleFunc("/submit-participant/"+fmt.Sprint(b.UUID)+"/"+fmt.Sprint(p.Login)+"/", p.handleParticipant(db, b)) | ||||
| 		mux.HandleFunc("/end-video/"+fmt.Sprint(b.UUID)+"/"+fmt.Sprint(p.Login)+"/", p.handleEndOfVideo(b)) | ||||
| 		var i int | ||||
| 		for i = range b.Questions { | ||||
| 			mux.HandleFunc("/submit-answer/"+fmt.Sprint(b.UUID)+"/"+fmt.Sprint(p.Login)+"/"+fmt.Sprint(i+1)+"/", p.HandleAnswer(db, b, int64(i+1))) | ||||
| 			mux.HandleFunc("/submit-answer/"+fmt.Sprint(b.UUID)+"/"+fmt.Sprint(p.Login)+"/"+fmt.Sprint(i+1)+"/", p.handleAnswer(db, b, int64(i+1))) | ||||
| 		} | ||||
| 		mux.HandleFunc("/allow-retry/"+fmt.Sprint(b.UUID)+"/"+fmt.Sprint(p.Login)+"/", p.HandleAllowRetry()) | ||||
| 		mux.HandleFunc("/retry/"+fmt.Sprint(b.UUID)+"/"+fmt.Sprint(p.Login)+"/", p.HandleRetry(b, &i)) | ||||
| 		mux.HandleFunc("/refresh-summary/"+fmt.Sprint(b.UUID)+"/"+fmt.Sprint(p.Login)+"/", p.HandleRefresh(b)) | ||||
| 		mux.HandleFunc("/allow-retry/"+fmt.Sprint(b.UUID)+"/"+fmt.Sprint(p.Login)+"/", p.handleAllowRetry()) | ||||
| 		mux.HandleFunc("/retry/"+fmt.Sprint(b.UUID)+"/"+fmt.Sprint(p.Login)+"/", p.handleRetry(b, &i)) | ||||
| 		mux.HandleFunc("/refresh-summary/"+fmt.Sprint(b.UUID)+"/"+fmt.Sprint(p.Login)+"/", p.handleRefresh(b)) | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| @@ -74,8 +74,8 @@ func (mux *Mux) handleBriefings(db *data.DB, cb <-chan *Briefing, s *Session) { | ||||
| 	for b := range cb { | ||||
| 		getQuestions(db, b) | ||||
| 
 | ||||
| 		mux.HandleFunc("/new-participant/"+fmt.Sprint(b.UUID)+"/", b.HandleNewParticipant(participantChan)) | ||||
| 		mux.HandleFunc("/submit-form/"+fmt.Sprint(b.UUID)+"/", b.HandleBriefingForm(db, s)) | ||||
| 		mux.HandleFunc("/new-participant/"+fmt.Sprint(b.UUID)+"/", b.handleNewParticipant(participantChan)) | ||||
| 		mux.HandleFunc("/submit-form/"+fmt.Sprint(b.UUID)+"/", b.handleBriefingForm(db, s)) | ||||
| 
 | ||||
| 		go mux.handleParticipants(db, participantChan, b) | ||||
| 	} | ||||
| @@ -86,9 +86,9 @@ func (mux *Mux) HandleSessions(db *data.DB, cs <-chan *Session, ss *[]*Session) | ||||
| 	for s := range cs { | ||||
| 		(*ss) = append((*ss), s) | ||||
| 
 | ||||
| 		mux.HandleFunc("/search/"+fmt.Sprint(s.UUID)+"/", s.HandleSearch(db)) | ||||
| 		mux.HandleFunc("/new-briefing/"+fmt.Sprint(s.UUID)+"/", s.HandleNewBriefing(briefingChan)) | ||||
| 		mux.HandleFunc("/briefing-done/"+fmt.Sprint(s.UUID)+"/", s.HandleBriefingDone(db)) | ||||
| 		mux.HandleFunc("/search/"+fmt.Sprint(s.UUID)+"/", s.handleSearch(db)) | ||||
| 		mux.HandleFunc("/new-briefing/"+fmt.Sprint(s.UUID)+"/", s.handleNewBriefing(briefingChan)) | ||||
| 		mux.HandleFunc("/briefing-done/"+fmt.Sprint(s.UUID)+"/", s.handleBriefingDone(db)) | ||||
| 
 | ||||
| 		go mux.handleBriefings(db, briefingChan, s) | ||||
| 	} | ||||
| @@ -9,7 +9,7 @@ | ||||
|  * You should have received a copy of the GNU General Public License along with this program.  If not, see <http://www.gnu.org/licenses/>. | ||||
|  */ | ||||
| 
 | ||||
| package session | ||||
| package server | ||||
| 
 | ||||
| import ( | ||||
| 	"fmt" | ||||
| @@ -67,7 +67,7 @@ func makeResultQuestions(sq []data.Question, givenAnswers []int) []resultQuestio | ||||
| 	return questions | ||||
| } | ||||
| 
 | ||||
| func (p *Participant) HandleParticipant(db *data.DB, b *Briefing) http.HandlerFunc { | ||||
| func (p *Participant) handleParticipant(db *data.DB, b *Briefing) http.HandlerFunc { | ||||
| 	return func(w http.ResponseWriter, r *http.Request) { | ||||
| 		p.FirstName = r.PostFormValue("first-" + fmt.Sprint(p.Login)) | ||||
| 		p.LastName = r.PostFormValue("last-" + fmt.Sprint(p.Login)) | ||||
| @@ -88,7 +88,7 @@ func (p *Participant) HandleParticipant(db *data.DB, b *Briefing) http.HandlerFu | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| func (p *Participant) HandleAnswer(db *data.DB, b *Briefing, i int64) http.HandlerFunc { | ||||
| func (p *Participant) handleAnswer(db *data.DB, b *Briefing, i int64) http.HandlerFunc { | ||||
| 	return func(w http.ResponseWriter, r *http.Request) { | ||||
| 		if i < int64(len(b.Questions)) { | ||||
| 			if err := handleGivenAnswer(p, i-1, r); err != nil { | ||||
| @@ -139,14 +139,14 @@ func (p *Participant) HandleAnswer(db *data.DB, b *Briefing, i int64) http.Handl | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| func (p *Participant) HandleAllowRetry() http.HandlerFunc { | ||||
| func (p *Participant) handleAllowRetry() http.HandlerFunc { | ||||
| 	return func(w http.ResponseWriter, r *http.Request) { | ||||
| 		p.NoIncorrect = -1 | ||||
| 		p.AllowRetry = true | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| func (p *Participant) HandleRetry(b *Briefing, i *int) http.HandlerFunc { | ||||
| func (p *Participant) handleRetry(b *Briefing, i *int) http.HandlerFunc { | ||||
| 	return func(w http.ResponseWriter, r *http.Request) { | ||||
| 		if p.AllowRetry { | ||||
| 			p.AllowRetry = false | ||||
| @@ -176,7 +176,7 @@ func (p *Participant) HandleRetry(b *Briefing, i *int) http.HandlerFunc { | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| func (p *Participant) HandleRefresh(b *Briefing) http.HandlerFunc { | ||||
| func (p *Participant) handleRefresh(b *Briefing) http.HandlerFunc { | ||||
| 	return func(w http.ResponseWriter, r *http.Request) { | ||||
| 		data := participantHTMLData{ | ||||
| 			BriefingID:  b.UUID, | ||||
| @@ -187,7 +187,7 @@ func (p *Participant) HandleRefresh(b *Briefing) http.HandlerFunc { | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| func (p *Participant) HandleEndOfVideo(b *Briefing) http.HandlerFunc { | ||||
| func (p *Participant) handleEndOfVideo(b *Briefing) http.HandlerFunc { | ||||
| 	return func(w http.ResponseWriter, r *http.Request) { | ||||
| 		data := questionHTMLData{ | ||||
| 			participantHTMLData: participantHTMLData{ | ||||
| @@ -9,7 +9,7 @@ | ||||
|  * You should have received a copy of the GNU General Public License along with this program.  If not, see <http://www.gnu.org/licenses/>. | ||||
|  */ | ||||
| 
 | ||||
| package session | ||||
| package server | ||||
| 
 | ||||
| import ( | ||||
| 	"html/template" | ||||
| @@ -26,7 +26,7 @@ type Session struct { | ||||
| 	Briefings  []*Briefing | ||||
| } | ||||
| 
 | ||||
| func (s *Session) HandleSearch(db *data.DB) http.HandlerFunc { | ||||
| func (s *Session) handleSearch(db *data.DB) http.HandlerFunc { | ||||
| 	return func(w http.ResponseWriter, r *http.Request) { | ||||
| 		var err error | ||||
| 		data := tableHTMLData{} | ||||
| @@ -41,7 +41,7 @@ func (s *Session) HandleSearch(db *data.DB) http.HandlerFunc { | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| func (s *Session) HandleNewBriefing(cb chan<- *Briefing) http.HandlerFunc { | ||||
| func (s *Session) handleNewBriefing(cb chan<- *Briefing) http.HandlerFunc { | ||||
| 	return func(w http.ResponseWriter, r *http.Request) { | ||||
| 		briefing := Briefing{Briefing: &data.Briefing{InstructorID: s.Instructor.ID}, UUID: uuid.New()} | ||||
| 		s.Briefings = append(s.Briefings, &briefing) | ||||
| @@ -51,7 +51,7 @@ func (s *Session) HandleNewBriefing(cb chan<- *Briefing) http.HandlerFunc { | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| func (s *Session) HandleBriefingDone(db *data.DB) http.HandlerFunc { | ||||
| func (s *Session) handleBriefingDone(db *data.DB) http.HandlerFunc { | ||||
| 	return func(w http.ResponseWriter, r *http.Request) { | ||||
| 		data := tableHTMLData{SessionID: s.UUID} | ||||
| 		var err error | ||||
		Reference in New Issue
	
	Block a user