From 1e6d83de1dc1e18315305856316558ab9e587f8e Mon Sep 17 00:00:00 2001 From: Jason Streifling Date: Thu, 19 Oct 2023 20:06:08 +0200 Subject: [PATCH] =?UTF-8?q?Sessionhandler=20eingef=C3=BCgt=20und=20Code=20?= =?UTF-8?q?entsprechend=20umgestellt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.go | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/main.go b/main.go index c7353e6..b0baab8 100644 --- a/main.go +++ b/main.go @@ -1,33 +1,44 @@ package main import ( + "fmt" "html/template" "log" "net/http" - "streifling.com/jason/sicherheitsunterweisung/packages/data" + "streifling.com/jason/sicherheitsunterweisung/packages/db" "streifling.com/jason/sicherheitsunterweisung/packages/server" + "streifling.com/jason/sicherheitsunterweisung/packages/types" ) -func main() { - logins := make([]string, 0) +func handleSessions(mux *http.ServeMux, db *db.DB, ss []*types.Session, cs <-chan *types.Session) { + for session := range cs { + ss = append(ss, session) + mux.HandleFunc("/add-participant-"+fmt.Sprint(session.ID)+"/", server.AddParticipant(session)) + mux.HandleFunc("/submit-form-"+fmt.Sprint(session.ID)+"/", server.SubmitBriefingForm(session, db)) + } +} - db, err := data.OpenDB("sicherheitsunterweisung") +func main() { + db, err := db.Open("sicherheitsunterweisung") if err != nil { log.Fatalln(err) } + sessions := make([]*types.Session, 0) + sessionChan := make(chan *types.Session) + mux := http.NewServeMux() 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("/search/", server.DisplaySearchResults(db)) - mux.HandleFunc("/new-briefing/", server.DisplayInstructorForm()) - mux.HandleFunc("/add-participant/", server.AddParticipant(&logins)) - mux.HandleFunc("/submit-form/", server.SubmitBriefingForm(db, &logins)) mux.HandleFunc("/internal-login/", server.DisplayTable(db)) - mux.HandleFunc("/external-login/", server.DisplayParticipantForm(&logins)) + mux.HandleFunc("/external-login/", server.DisplayParticipantForm(sessions)) + mux.HandleFunc("/search/", server.DisplaySearchResults(db)) + mux.HandleFunc("/new-briefing/", server.DisplayInstructorForm(sessionChan)) + + go handleSessions(mux, db, sessions, sessionChan) log.Fatalln(http.ListenAndServe(":8080", mux)) }