-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
57 lines (47 loc) · 1.31 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
package main
import (
"fmt"
"os"
"github.com/esgameco/question-site/controllers"
"github.com/esgameco/question-site/models"
"github.com/gin-contrib/sessions"
"github.com/gin-contrib/sessions/cookie"
"github.com/gin-contrib/static"
"github.com/gin-gonic/gin"
)
func main() {
fmt.Printf("test")
r := engine()
r.Use(gin.LoggerWithWriter(os.Stdout))
r.Use(gin.Recovery())
port := os.Getenv("PORT")
if port == "" {
port = "8080"
}
r.Run(":" + port)
}
func engine() *gin.Engine {
r := gin.New()
// Session handling
controller := controllers.New()
r.Use(sessions.Sessions("sessions", cookie.NewStore(models.GetSecret())))
// Serve vue.js frontend
r.Use(static.Serve("/", static.LocalFile("./questionclient/dist", false)))
r.NoRoute(func(c *gin.Context) {
c.File("./questionclient/dist/index.html")
})
// Serve API
r.POST("/api/register", controller.Register)
r.POST("/api/login", controller.Login)
r.POST("/api/logout", controller.Logout)
// Authenticated routes
r.Use(controllers.IsAuthenticated())
{
r.GET("/api/questions", controller.GetQuestionsQuery)
r.GET("/api/questions/all", controller.GetAllQuestions)
r.GET("/api/question/:id", controller.GetQuestion)
r.POST("/api/question/create", controller.CreateQuestion)
r.POST("/api/question/:id/update", controller.UpdateQuestion)
}
return r
}