Documentation ¶
Index ¶
- Variables
- func Recover(next http.Handler) http.Handler
- func ValidateJwtCookie(r *http.Request) (*jwt.Token, error)
- type Claims
- type Controller
- func (c *Controller) ExecuteTemplate(w http.ResponseWriter, data any) error
- func (c *Controller) ServeHTTP(w http.ResponseWriter, r *http.Request)
- func (c *Controller) WithHandlerFunc(f func(c *Controller, w http.ResponseWriter, r *http.Request)) *Controller
- func (c *Controller) WithTemplates(paths ...string) *Controller
Constants ¶
This section is empty.
Variables ¶
View Source
var ExamHandler = func(c *Controller, w http.ResponseWriter, r *http.Request) { _, err := ValidateJwtCookie(r) if err != nil { panic(err) } participantID := r.PathValue("participantID") session, err := c.sStore.Read(r.PathValue("uuid")) if err != nil { panic(err) } exam, ok := session.Exams[participantID] if !ok { panic(errors.New("Exam not found in the store!")) } examWithSession := struct { *models.Exam Session *models.Session }{exam, session} switch r.Method { case http.MethodGet: log.Info("Sending exam to", "participant", exam.Participant, "exam", exam) err = c.ExecuteTemplate(w, examWithSession) if err != nil { panic(err) } case http.MethodPost: err := r.ParseForm() if err != nil { panic(err) } answers := make([]*models.ParticipantAnswer, 0) participant := session.Participants[participantID] for quizID, values := range r.Form { correct := false quiz := session.Quizzes[quizID] for _, answerID := range values { if quiz.Correct.ID == answerID { correct = true } answers = append(answers, &models.ParticipantAnswer{Quiz: quiz, Answer: session.Answers[answerID], Correct: correct}) } } response, err := c.rStore.Create( &models.Response{ SessionTitle: session.Title, Participant: participant, Answers: answers, }) if err != nil { panic(err) } log.Info("Saving response", "response", response) } }
View Source
var LoginHandler = func(c *Controller, w http.ResponseWriter, r *http.Request) { if r.Method == http.MethodPost { err := r.ParseForm() if err != nil { panic(err) } pToken := r.FormValue("participantToken") if pToken == "" { panic(errors.New("Token not found parsing the request!")) } log.Info("Received", "participantToken", pToken) var loggedParticipant *models.Participant done := false for _, session := range c.sStore.ReadAll() { if done { break } for _, exam := range session.Exams { if pToken == exam.Participant.Token { loggedParticipant = exam.Participant done = true } } } log.Info("Participant logged in as", "participant", loggedParticipant) if loggedParticipant == nil { panic(errors.New("Participant not found!")) } claims := &Claims{ Token: pToken, StandardClaims: jwt.StandardClaims{ ExpiresAt: time.Now().Add(jwtExpiresAt).Unix(), }, } tokenString, err := jwt.NewWithClaims(jwt.SigningMethodHS256, claims).SignedString([]byte(jwtKey)) if err != nil { panic(err) } http.SetCookie(w, &http.Cookie{ Name: "Authorize", Value: tokenString, Expires: time.Now().Add(jwtExpiresAt), }) log.Info("Released", "jwt", tokenString) log.Info("Redirect to", "url", "/sessions") http.Redirect(w, r, "/sessions", http.StatusSeeOther) } err := c.ExecuteTemplate(w, nil) if err != nil { panic(err) } }
View Source
var SessionsHandler = func(c *Controller, w http.ResponseWriter, r *http.Request) { token, err := ValidateJwtCookie(r) if err != nil { panic(err) } claims := token.Claims.(jwt.MapClaims) var participantSessions []struct { ParticipantID string *models.Session } for _, session := range c.sStore.ReadAll() { for _, exam := range session.Exams { if exam.Participant.Token == claims["token"] { s := struct { ParticipantID string *models.Session }{exam.Participant.ID, session} participantSessions = append(participantSessions, s) break } } } err = c.ExecuteTemplate(w, participantSessions) if err != nil { panic(err) } }
Functions ¶
Types ¶
type Claims ¶
type Claims struct { Token string `json:"token"` jwt.StandardClaims }
type Controller ¶
type Controller struct {
// contains filtered or unexported fields
}
func NewController ¶
func NewController(sStore *file.SessionFileStore, rStore *file.ResponseFileStore) *Controller
func (*Controller) ExecuteTemplate ¶
func (c *Controller) ExecuteTemplate(w http.ResponseWriter, data any) error
func (*Controller) ServeHTTP ¶
func (c *Controller) ServeHTTP(w http.ResponseWriter, r *http.Request)
func (*Controller) WithHandlerFunc ¶
func (c *Controller) WithHandlerFunc(f func(c *Controller, w http.ResponseWriter, r *http.Request)) *Controller
func (*Controller) WithTemplates ¶
func (c *Controller) WithTemplates(paths ...string) *Controller
Click to show internal directories.
Click to hide internal directories.