-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
61 lines (45 loc) · 1.24 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
package main
import (
"net/http"
"os"
"github.com/gin-gonic/gin"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/postgres"
a "github.com/me/todo-go-server/src/auth"
m "github.com/me/todo-go-server/src/models"
r "github.com/me/todo-go-server/src/routes"
s "github.com/me/todo-go-server/src/shared"
)
func Migrate(db *gorm.DB) {
db.AutoMigrate(&m.User{}, &m.Todo{})
}
func main() {
db := s.Init()
Migrate(db)
defer db.Close()
router := gin.Default()
router.LoadHTMLGlob("templates/*.tmpl.html")
router.Static("/static", "static")
router.GET("/", func(c *gin.Context) {
c.HTML(http.StatusOK, "index.tmpl.html", nil)
})
authMiddleware := a.GinJwtMiddlewareHandler()
auth := router.Group("/auth")
auth.Use(authMiddleware.MiddlewareFunc())
{
auth.GET("/refresh_token", authMiddleware.RefreshHandler)
}
v1 := router.Group("/api/v1/todos")
v1.Use(authMiddleware.MiddlewareFunc())
{
v1.POST("/", r.CreateTodo)
v1.GET("/", r.FetchTodos)
v1.GET("/:id", r.FetchSingleTodo)
v1.PUT("/:id", r.UpdateTodo)
v1.DELETE("/:id", r.DeleteTodo)
}
router.POST("/login", authMiddleware.LoginHandler)
router.POST("/signup", r.UsersRegistration)
router.GET("/users/:id", r.GetUser)
router.Run(":" + os.Getenv("PORT"))
}