DEVELOPMENT ENVIRONMENT

~liljamo/tixe

ref: 9024ccc11e6acdd0e6c9e3f5d9f3944c0541445c tixe/tixe.go -rw-r--r-- 1.0 KiB
9024ccc1Jonni Liljamo feat: docker compose deployment 1 year, 4 months 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
package main

import (
	"log"
	"net/http"
	"tixe/api"
	"tixe/db"
	"tixe/template"

	"github.com/gin-gonic/gin"
)

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

func root(c *gin.Context) {
	html := template.TmplEngine.Render("index.tmpl", map[string]interface{}{"title": "tixë"})
	c.Data(http.StatusOK, "text/html", html)
}

func handleNoRoute(c *gin.Context) {
	html := template.TmplEngine.Render("404.tmpl", map[string]interface{}{"title": "tixë"})
	c.Data(http.StatusNotFound, "text/html", html)
}

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

	r.NoRoute(handleNoRoute)

	r.GET("/", root)

	apiRoute := r.Group("/api")
	{
		apiRoute.GET("/", api.Root)
		apiRoute.GET("/ping", ping)
	}

	return r
}

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

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

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

	r := setupRouter()

	r.Run(":8080")
}