2023-10-05 18:08:44 +02:00
|
|
|
package main
|
2023-10-04 17:13:03 +02:00
|
|
|
|
2023-10-04 17:29:22 +02:00
|
|
|
import (
|
2023-10-19 20:06:08 +02:00
|
|
|
"fmt"
|
2023-10-10 20:57:53 +02:00
|
|
|
"html/template"
|
2023-10-04 17:29:22 +02:00
|
|
|
"log"
|
|
|
|
"net/http"
|
2023-10-16 18:51:52 +02:00
|
|
|
|
2023-10-28 08:49:28 +02:00
|
|
|
"streifling.com/jason/sicherheitsunterweisung/packages/data"
|
|
|
|
"streifling.com/jason/sicherheitsunterweisung/packages/session"
|
2023-10-04 17:29:22 +02:00
|
|
|
)
|
|
|
|
|
2023-10-28 08:49:28 +02:00
|
|
|
func handleParticipants(mux *http.ServeMux, db *data.DB, cp <-chan *data.Participant, s *session.Session) {
|
2023-10-28 08:01:34 +02:00
|
|
|
for participant := range cp {
|
2023-10-28 09:03:19 +02:00
|
|
|
mux.HandleFunc("/submit-participant/"+fmt.Sprint(s.ID)+"/"+fmt.Sprint(participant.Login)+"/", s.HandleParticipant(db, participant, &s.Questions))
|
2023-10-28 08:01:34 +02:00
|
|
|
for i := range s.Questions {
|
2023-10-28 08:49:28 +02:00
|
|
|
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)))
|
2023-10-28 08:01:34 +02:00
|
|
|
}
|
2023-10-28 08:49:28 +02:00
|
|
|
mux.HandleFunc("/retry/"+fmt.Sprint(s.ID)+"/"+fmt.Sprint(participant.Login)+"/", s.HandleRetry(participant, &s.Questions))
|
2023-10-28 08:01:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-28 08:49:28 +02:00
|
|
|
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)
|
2023-10-28 08:01:34 +02:00
|
|
|
questionIDs := make([]string, 4)
|
|
|
|
|
|
|
|
for i := 0; i < len(questionIDs); i++ {
|
|
|
|
questionIDs[i] = fmt.Sprint(i + 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
2023-10-28 08:49:28 +02:00
|
|
|
s.Questions, err = db.GetQuestions(questionIDs)
|
2023-10-28 08:01:34 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalln(err)
|
|
|
|
}
|
|
|
|
|
2023-10-28 08:49:28 +02:00
|
|
|
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))
|
2023-10-28 08:01:34 +02:00
|
|
|
|
2023-10-28 08:49:28 +02:00
|
|
|
go handleParticipants(mux, db, participantChan, s)
|
2023-10-19 20:06:08 +02:00
|
|
|
}
|
|
|
|
}
|
2023-10-05 17:42:47 +02:00
|
|
|
|
2023-10-19 20:06:08 +02:00
|
|
|
func main() {
|
2023-10-28 08:49:28 +02:00
|
|
|
db, err := data.OpenDB("sicherheitsunterweisung")
|
2023-10-05 17:42:47 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalln(err)
|
2023-10-05 19:52:11 +02:00
|
|
|
}
|
2023-10-04 18:55:48 +02:00
|
|
|
|
2023-10-28 08:01:34 +02:00
|
|
|
mux := http.NewServeMux()
|
2023-10-28 08:49:28 +02:00
|
|
|
sessions := make([]*session.Session, 0)
|
|
|
|
sessionChan := make(chan *session.Session)
|
2023-10-19 20:06:08 +02:00
|
|
|
|
2023-10-04 18:55:48 +02:00
|
|
|
mux.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static/"))))
|
2023-10-10 20:57:53 +02:00
|
|
|
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
template.Must(template.ParseFiles("templates/index.html", "templates/login.html")).Execute(w, nil)
|
|
|
|
})
|
2023-10-28 09:03:19 +02:00
|
|
|
mux.HandleFunc("/internal-login/", session.HandleInternalLogin(db, &sessions, sessionChan))
|
2023-10-28 08:49:28 +02:00
|
|
|
mux.HandleFunc("/external-login/", session.HandleExternalLogin(&sessions))
|
|
|
|
mux.HandleFunc("/search/", session.HandleSearch(db))
|
2023-10-19 20:06:08 +02:00
|
|
|
|
2023-10-28 08:01:34 +02:00
|
|
|
go handleSessions(mux, db, sessionChan, &sessions)
|
2023-10-04 17:29:22 +02:00
|
|
|
|
|
|
|
log.Fatalln(http.ListenAndServe(":8080", mux))
|
|
|
|
}
|