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")
}