DEVELOPMENT ENVIRONMENT

~liljamo/tixe

ref: 74704273fd1bc8626d272a67682748940e4ac164 tixe/tixe.go -rw-r--r-- 971 bytes
74704273Jonni Liljamo docs: license 1 year, 3 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
package main

import (
	"log"
	"net/http"
	"tixe/api"
	"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...")

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

	r := setupRouter()

	r.Run(":8080")
}