/
main.go
87 lines (73 loc) · 2.14 KB
/
main.go
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
package main
import (
"github.com/adred/wiki-player/app/controllers"
"github.com/adred/wiki-player/app/middlewares"
"github.com/adred/wiki-player/app/models"
"github.com/adred/wiki-player/app/utils"
"github.com/gin-gonic/gin"
"github.com/goinggo/tracelog"
"github.com/gorilla/sessions"
)
// Main go routine
func main() {
// Start logger
tracelog.StartFile(1, utils.ConfigEntry("LogDir"), 1)
// Get new cookie store
store := sessions.NewCookieStore([]byte(utils.ConfigEntry("SecretKey")))
store.Options = &sessions.Options{
Path: "/",
MaxAge: 3600,
HttpOnly: true,
}
// Create DB connection
dbHandle := utils.DbHandle()
// Close DB
defer dbHandle.Close()
// Init Models
sm := &models.Song{DbHandle: dbHandle}
nm := &models.Nonce{DbHandle: dbHandle}
um := models.NewUser(dbHandle)
// Init Controllers
sc := &controllers.Song{SM: sm}
nc := &controllers.Nonce{NM: nm}
vc := &controllers.View{Store: store}
uc := &controllers.User{UM: um, Store: store}
// Init Gin
mux := gin.Default()
// Load templates
mux.LoadHTMLGlob("app/views/*")
// Serve static files
mux.Static("/public", utils.ConfigEntry("StaticDir"))
// Routes for static pages
static := mux.Group("/")
{
static.GET("/", vc.Index)
static.GET("/about", vc.About)
static.GET("/tos", vc.Tos)
static.GET("/privacy-policy", vc.PrivacyPolicy)
static.GET("/credits", vc.Credits)
}
// Routes that don't need authorization
basic := mux.Group("/api/v1")
basic.Use(middlewares.Session(store))
{
basic.GET("/nonce", nc.Create)
basic.GET("/users/check-username/:username", uc.CheckUsername)
basic.GET("/users/check-email/:email", uc.CheckEmail)
basic.POST("/users/login", uc.Login)
basic.POST("/users/register", uc.Register)
}
// Routes that need authorization
auth := mux.Group("/api/v1")
auth.Use(middlewares.Session(store), middlewares.UserAuth(store), middlewares.Nonce(nm))
{
auth.GET("/songs", sc.Index)
auth.GET("/users/delete/:nonce", uc.ConfirmDelete)
auth.POST("/users/delete", uc.Delete)
auth.POST("/users/update", uc.Update)
auth.POST("/users/logout", uc.Logout)
}
// Listen and serve on 0.0.0.0:8080
mux.Run(":9000")
tracelog.Stop()
}