-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
50 lines (43 loc) · 1023 Bytes
/
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
package main
import (
"api/controllers"
"api/database"
"api/middleware"
"api/model"
"log"
"github.com/gin-gonic/gin"
"github.com/joho/godotenv"
)
func init() {
loadEnv()
loadDatabase()
}
func main() {
router := gin.Default()
superGroup := router.Group("/api/v1")
{
userGroup := superGroup.Group("/users")
{
userGroup.POST("/", controllers.RegisterUser)
userGroup.POST("/login", controllers.LoginUser)
// below routes are protected by jwt auth middleware
userGroup.Use(middleware.JwtAuthMiddleware())
userGroup.POST("/logout", controllers.Logout)
userGroup.GET("/", controllers.GetAllUsers)
userGroup.GET("/:id", controllers.GetUserById)
userGroup.PUT("/:id", controllers.UpdateUser)
userGroup.DELETE("/:id", controllers.DeleteUser)
}
}
router.Run("0.0.0.0:8080")
}
func loadDatabase() {
database.Connect()
database.Database.AutoMigrate(&model.User{})
}
func loadEnv() {
err := godotenv.Load(".env.local")
if err != nil {
log.Fatal("Error loading .env file")
}
}