DEVELOPMENT ENVIRONMENT

~liljamo/tixe

ref: 7746581cf2c2201816d744bc5c3ae1d1e5dc1ede tixe/tixe.go -rw-r--r-- 2.5 KiB
7746581cJonni Liljamo feat: link management! 1 year, 1 month ago
                                                                                
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*
 * Copyright (C) 2023 Jonni Liljamo <jonni@liljamo.com>
 *
 * This file is licensed under AGPL-3.0-or-later, see NOTICE and LICENSE for
 * more information.
 */
package main

import (
	"encoding/gob"
	"log"
	"net/http"
	"tixe/api"
	"tixe/auth"
	"tixe/config"
	"tixe/db"
	"tixe/handlers"
	"tixe/middlewares"
	"tixe/template"
	"tixe/types"

	"github.com/gin-contrib/sessions"
	"github.com/gin-contrib/sessions/cookie"
	"github.com/gin-gonic/gin"
)

func ping(c *gin.Context) {
	c.String(http.StatusOK, "pong")
}

func handleNoRoute(c *gin.Context) {
	session := sessions.Default(c)
	user := session.Get("user")

	html := template.TmplEngine.Render("404.tmpl", map[string]interface{}{"title": "404", "user": user})
	c.Data(http.StatusNotFound, "text/html", html)
}

func setupRouter(auth *auth.Auth) *gin.Engine {
	r := gin.Default()
	r.Static("/static", "./static")

	// Register types that will be saved in sessions
	gob.Register(map[string]interface{}{})
	gob.Register(types.User{})

	store := cookie.NewStore([]byte(config.TixeConfig.CookieSecret))
	r.Use(sessions.Sessions("auth-session", store))

	r.NoRoute(handleNoRoute)
	r.GET("/login", middlewares.CanLogin, handlers.Login)

	authRoute := r.Group("/auth")
	{
		authRoute.GET("/login", handlers.AuthLogin(auth))
		authRoute.GET("/", handlers.AuthCallback(auth))
		authRoute.GET("/logout", handlers.AuthLogout)
	}

	reqAuth := r.Group("/")
	reqAuth.Use(middlewares.IsAuthenticated)
	{
		reqAuth.GET("/", handlers.Index)

		reqAuth.GET("/tags", handlers.Tags)
		reqAuth.GET("/settings", handlers.Settings)

		reqAuth.GET("/link/:id", handlers.LinkEdit)

		apiRoute := reqAuth.Group("/api")
		{
			apiRoute.GET("/", api.Root)
			apiRoute.GET("/ping", ping)
			apiRoute.POST("/link/new", api.LinkNew)
			apiRoute.POST("/link/:id/delete", api.LinkDelete)
			apiRoute.POST("/link/:id/visual", api.LinkUpdateVisual)
			apiRoute.POST("/link/:id/link", api.LinkUpdateLink)
			apiRoute.POST("/tags/new", api.TagsNew)
			apiRoute.POST("/tags/delete", api.TagsDelete)
			apiRoute.POST("/settings/user/display_name", api.UserUpdateDisplayName)
		}
	}

	return r
}

func main() {
	log.Print("[tixe] Starting up...")

	config.ParseConfig()

	db.NewPgPool()
	defer db.PgPool.Close()
	db.RunMigrations()

	err := template.NewTemplateEngine()
	if err != nil {
		log.Fatalf("[tixe] Creating a new TemplateEngine failed, '%s'", err)
	}

	auth, err := auth.NewAuth()
	if err != nil {
		log.Fatalf("[tixe] Creating a new authenticator failed, '%s'", err)
	}

	r := setupRouter(auth)

	r.Run(":8080")
}