summaryrefslogtreecommitdiff
path: root/internal/handlers/main.go
blob: 3f2e1b2260c09ec50663bf098bd8d7460820081f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package handlers

import (
	"demoon/config"
	"demoon/internal/database/repos"
	"demoon/internal/models"
	"html/template"
	"log/slog"
	"net/http"
	"os"
)

// Handlers structure
type Handlers struct {
	cfg  config.Config
	log  *slog.Logger
	repo repos.FullRepo
}

// NewHandlers constructor
func NewHandlers(
	cfg config.Config, l *slog.Logger, repo repos.FullRepo,
) *Handlers {
	if l == nil {
		l = slog.New(slog.NewJSONHandler(os.Stdout, nil))
	}
	h := &Handlers{
		cfg:  cfg,
		log:  l,
		repo: repo,
	}
	return h
}

func (h *Handlers) Ping(w http.ResponseWriter, r *http.Request) {
	h.log.Info("got ping request")
	w.Write([]byte("pong"))
}

func (h *Handlers) MainPage(w http.ResponseWriter, r *http.Request) {
	tmpl, err := template.ParseGlob("components/*.html")
	if err != nil {
		abortWithError(w, err.Error())
		return
	}
	testQuestion := &models.Question{
		Text:         "___ du keine Zweifel daran?",
		Options:      []string{"Haben", "Hast", "Hat", "Habt"},
		CorrectIndex: 1,
	}
	tmpl.ExecuteTemplate(w, "main", testQuestion)
}