package main import ( "fmt" "html/template" "log" "net/http" "streifling.com/jason/sicherheitsunterweisung/packages/data" "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() sessions := make([]*session.Session, 0) sessionChan := make(chan *session.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("/search/", session.HandleSearch(db)) go handleSessions(mux, db, sessionChan, &sessions) log.Fatalln(http.ListenAndServe(":8080", mux)) }