main.go (view raw)
1package main
2
3import (
4 "embed"
5 "html/template"
6 "net/http"
7 "os"
8
9 "github.com/ckrinitsin/shopping-list/authenticate"
10 "github.com/ckrinitsin/shopping-list/handlers"
11 "github.com/ckrinitsin/shopping-list/models"
12 "github.com/gin-contrib/sessions"
13 "github.com/gin-contrib/sessions/cookie"
14 "github.com/gin-gonic/gin"
15)
16
17//go:embed templates/*
18var templatesFS embed.FS
19
20func main() {
21 r := gin.Default()
22
23 models.ConnectDatabase()
24
25 tmpl := template.Must(template.ParseFS(templatesFS, "templates/*"))
26 r.SetHTMLTemplate(tmpl)
27
28 store := cookie.NewStore([]byte(os.Getenv("SECRET")))
29 r.Use(sessions.Sessions("session", store))
30
31 r.GET("/health", func(c *gin.Context) {
32 c.JSON(http.StatusOK, gin.H{
33 "health-check": "passed",
34 })
35 })
36
37 r.POST("/login", authenticate.LoginPOST)
38 r.GET("/login", authenticate.LoginGET)
39 r.POST("/register", authenticate.RegisterPOST)
40 r.GET("/register", authenticate.RegisterGET)
41 r.POST("/logout", authenticate.Logout)
42
43 r.GET("/", authenticate.CheckAuth, shopping_list.LoadElements)
44 r.POST("/create", authenticate.CheckAuth, shopping_list.CreateEntry)
45 r.POST("/delete", authenticate.CheckAuth, shopping_list.DeleteEntries)
46 r.POST("/toggle", authenticate.CheckAuth, shopping_list.ToggleEntry)
47
48 r.Run()
49}