/* Copyright © 2024 NAME HERE */ package cmd import ( "net/http" "git.andreafazzi.eu/andrea/probo/cmd/serve" "git.andreafazzi.eu/andrea/probo/pkg/store/file" "github.com/charmbracelet/log" "github.com/spf13/cobra" ) // serveCmd represents the serve command var serveCmd = &cobra.Command{ Use: "serve", Short: "Launch a web server to adminster exam sessions", Long: `A longer description that spans multiple lines and likely contains examples and usage of using your command. For example: Cobra is a CLI library for Go that empowers applications. This application is a tool to generate the needed files to quickly create a Cobra application.`, Run: runServer, } func init() { rootCmd.AddCommand(serveCmd) } func runServer(cmd *cobra.Command, args []string) { sStore, err := file.NewDefaultSessionFileStore() if err != nil { log.Fatal("Session store loading", "err", err) } rStore, err := file.NewDefaultResponseFileStore() if err != nil { log.Fatal("Session store loading", "err", err) } mux := http.NewServeMux() loginController := serve.NewController(sStore, rStore). WithTemplates( "templates/login/layout-login.html.tmpl", "templates/login/login.html.tmpl", ). WithHandlerFunc(serve.LoginHandler) sessionsController := serve.NewController(sStore, rStore). WithTemplates( "templates/sessions/layout-sessions.html.tmpl", "templates/sessions/sessions.html.tmpl", ). WithHandlerFunc(serve.SessionsHandler) examController := serve.NewController(sStore, rStore). WithTemplates( "templates/exam/layout-exam.html.tmpl", "templates/exam/exam.html.tmpl", ). WithHandlerFunc(serve.ExamHandler) mux.Handle("GET /login", serve.Recover(loginController)) mux.Handle("POST /login", serve.Recover(loginController)) mux.Handle("GET /sessions", serve.Recover(sessionsController)) mux.Handle("GET /sessions/{uuid}/exams/{token}", serve.Recover(examController)) mux.Handle("POST /sessions/{uuid}/exams/{token}", serve.Recover(examController)) mux.Handle("GET /public/", http.StripPrefix("/public", http.FileServer(http.Dir("public")))) log.Info("Listening...") err = http.ListenAndServe(":3000", mux) if err != nil { panic(err) } }