-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
49 lines (39 loc) · 1.01 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
package main
import (
"fmt"
"log"
"todo-rest-api/controller"
"todo-rest-api/database"
"todo-rest-api/middleware"
"todo-rest-api/model"
"github.com/gin-gonic/gin"
"github.com/joho/godotenv"
)
func main() {
loadEnv()
loadDatabase()
initRoutes()
}
func loadEnv() {
err := godotenv.Load(".env.local")
if err != nil {
log.Fatal("Error loading .env file")
}
}
func loadDatabase() {
database.Connect()
database.Database.AutoMigrate(&model.User{})
database.Database.AutoMigrate(&model.Todo{})
}
func initRoutes() {
router := gin.Default()
publicRoutes := router.Group("/auth")
publicRoutes.POST("/register", controller.Register)
publicRoutes.POST("/login", controller.Login)
protectedRoutes := router.Group("/api")
protectedRoutes.Use(middleware.JWTAuthMiddleware())
protectedRoutes.POST("/entry", controller.AddEntry)
protectedRoutes.GET("/entry", controller.GetAllEntries)
router.Run(":8000")
fmt.Println("Server running on port 8000")
}