From 34ef95ad76617713aad293b636b63c9739628f05 Mon Sep 17 00:00:00 2001 From: Jason Streifling Date: Sat, 28 Oct 2023 09:17:10 +0200 Subject: [PATCH] =?UTF-8?q?main=20aufger=C3=A4umt,=20indem=20mux=20nach=20?= =?UTF-8?q?session=20ausgelagert=20wurde?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.go | 39 ++---------------------------- packages/session/mux.go | 53 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 37 deletions(-) create mode 100644 packages/session/mux.go diff --git a/main.go b/main.go index 8d3139c..b265ff6 100644 --- a/main.go +++ b/main.go @@ -1,7 +1,6 @@ package main import ( - "fmt" "html/template" "log" "net/http" @@ -10,47 +9,13 @@ import ( "streifling.com/jason/sicherheitsunterweisung/packages/session" ) -func handleParticipants(mux *http.ServeMux, db *data.DB, cp <-chan *data.Participant, s *session.Session) { - for participant := range cp { - mux.HandleFunc("/submit-participant/"+fmt.Sprint(s.ID)+"/"+fmt.Sprint(participant.Login)+"/", s.HandleParticipant(db, participant, &s.Questions)) - for i := range s.Questions { - mux.HandleFunc("/submit-answer/"+fmt.Sprint(s.ID)+"/"+fmt.Sprint(participant.Login)+"/"+fmt.Sprint(i+1)+"/", s.HandleAnswer(db, participant, &s.Questions, int64(i+1))) - } - mux.HandleFunc("/retry/"+fmt.Sprint(s.ID)+"/"+fmt.Sprint(participant.Login)+"/", s.HandleRetry(participant, &s.Questions)) - } -} - -func handleSessions(mux *http.ServeMux, db *data.DB, cs <-chan *session.Session, ss *[]*session.Session) { - for s := range cs { - (*ss) = append(*ss, s) - participantChan := make(chan *data.Participant) - questionIDs := make([]string, 4) - - for i := 0; i < len(questionIDs); i++ { - questionIDs[i] = fmt.Sprint(i + 1) - } - - var err error - s.Questions, err = db.GetQuestions(questionIDs) - if err != nil { - log.Fatalln(err) - } - - mux.HandleFunc("/new-briefing/", s.HandleNewBriefing()) - mux.HandleFunc("/new-participant/"+fmt.Sprint(s.ID)+"/", s.HandleNewParticipant(participantChan)) - mux.HandleFunc("/submit-form/"+fmt.Sprint(s.ID)+"/", s.HandleBriefingForm(db)) - - go handleParticipants(mux, db, participantChan, s) - } -} - func main() { db, err := data.OpenDB("sicherheitsunterweisung") if err != nil { log.Fatalln(err) } - mux := http.NewServeMux() + mux := session.NewMux() sessions := make([]*session.Session, 0) sessionChan := make(chan *session.Session) @@ -62,7 +27,7 @@ func main() { mux.HandleFunc("/external-login/", session.HandleExternalLogin(&sessions)) mux.HandleFunc("/search/", session.HandleSearch(db)) - go handleSessions(mux, db, sessionChan, &sessions) + go mux.HandleSessions(db, sessionChan, &sessions) log.Fatalln(http.ListenAndServe(":8080", mux)) } diff --git a/packages/session/mux.go b/packages/session/mux.go new file mode 100644 index 0000000..0f272c5 --- /dev/null +++ b/packages/session/mux.go @@ -0,0 +1,53 @@ +package session + +import ( + "fmt" + "log" + "net/http" + + "streifling.com/jason/sicherheitsunterweisung/packages/data" +) + +type Mux struct { + *http.ServeMux +} + +func NewMux() *Mux { + mux := new(Mux) + mux.ServeMux = http.NewServeMux() + return mux +} + +func (mux *Mux) handleParticipants(db *data.DB, cp <-chan *data.Participant, s *Session) { + for participant := range cp { + mux.HandleFunc("/submit-participant/"+fmt.Sprint(s.ID)+"/"+fmt.Sprint(participant.Login)+"/", s.HandleParticipant(db, participant, &s.Questions)) + for i := range s.Questions { + mux.HandleFunc("/submit-answer/"+fmt.Sprint(s.ID)+"/"+fmt.Sprint(participant.Login)+"/"+fmt.Sprint(i+1)+"/", s.HandleAnswer(db, participant, &s.Questions, int64(i+1))) + } + mux.HandleFunc("/retry/"+fmt.Sprint(s.ID)+"/"+fmt.Sprint(participant.Login)+"/", s.HandleRetry(participant, &s.Questions)) + } +} + +func (mux *Mux) HandleSessions(db *data.DB, cs <-chan *Session, ss *[]*Session) { + for s := range cs { + (*ss) = append(*ss, s) + participantChan := make(chan *data.Participant) + questionIDs := make([]string, 4) + + for i := 0; i < len(questionIDs); i++ { + questionIDs[i] = fmt.Sprint(i + 1) + } + + var err error + s.Questions, err = db.GetQuestions(questionIDs) + if err != nil { + log.Fatalln(err) + } + + mux.HandleFunc("/new-briefing/", s.HandleNewBriefing()) + mux.HandleFunc("/new-participant/"+fmt.Sprint(s.ID)+"/", s.HandleNewParticipant(participantChan)) + mux.HandleFunc("/submit-form/"+fmt.Sprint(s.ID)+"/", s.HandleBriefingForm(db)) + + go mux.handleParticipants(db, participantChan, s) + } +}