96 lines
3.2 KiB
Go

/*
* Sicherheitsunterweisung
* Copyright (C) 2023 Jason Streifling
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* 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 server
import (
"fmt"
"log"
"net/http"
"strings"
"streifling.com/jason/sicherheitsunterweisung/packages/data"
)
type mux struct {
*http.ServeMux
}
func NewMux() *mux {
return &mux{ServeMux: http.NewServeMux()}
}
func getQuestions(db *data.DB, b *briefing) {
questionIDs := make([]string, 4)
for i := 0; i < len(questionIDs); i++ {
questionIDs[i] = fmt.Sprint(i + 1)
}
var err error
b.questions, err = db.GetQuestions(questionIDs)
if err != nil {
log.Fatalln(err)
}
for i := range b.questions {
for j := range b.questions[i].Answers {
if parts := strings.Split(b.questions[i].Answers[j].Text, ":/"); parts[0] == "file" {
b.questions[i].Answers[j].Text = parts[1]
b.questions[i].Answers[j].IsImage = true
} else {
b.questions[i].Answers[j].IsImage = false
}
}
}
}
func (mux *mux) handleParticipants(db *data.DB, cp <-chan *participant, b *briefing) {
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))
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("/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))
}
}
func (mux *mux) handleBriefings(db *data.DB, cb <-chan *briefing, s *Session) {
participantChan := make(chan *participant)
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))
go mux.handleParticipants(db, participantChan, b)
}
}
func (mux *mux) HandleSessions(db *data.DB, cs <-chan *Session, ss *[]*Session) {
briefingChan := make(chan *briefing)
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))
go mux.handleBriefings(db, briefingChan, s)
}
}