-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
49 lines (40 loc) · 1.38 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 (
"api/config"
"api/controller"
"api/model"
"log"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
)
func main() {
e := echo.New()
cfg := config.InitConfig()
db := config.InitDB(*cfg)
config.Migrate(db)
userModel := model.UserModel{DB: db}
userControll := controller.UserController{Mdl: userModel}
itemModel := model.ItemModel{DB: db}
itemControll := controller.ItemController{Mdl: itemModel}
e.Pre(middleware.RemoveTrailingSlash()) // fungsi ini dijalankan sebelum routing
e.Use(middleware.CORS()) // WAJIB DIPAKAI agar tidak terjadi masalah permission
e.Use(middleware.LoggerWithConfig(middleware.LoggerConfig{
Format: "method=${method}, uri=${uri}, status=${status}\n",
}))
e.POST("/register", userControll.Register())
e.POST("/login", userControll.Login())
auth := e.Group("/d")
auth.Use(middleware.JWT([]byte(config.InitConfig().JWTKey)))
auth.GET("/users", userControll.GetAll())
auth.GET("/users/:id", userControll.GetID())
auth.PUT("/users/:id", userControll.Update())
auth.DELETE("/users/:id", userControll.Delete())
auth.POST("/items", itemControll.Insert())
auth.GET("/items", itemControll.GetAll())
auth.GET("/items/:id", itemControll.GetByID())
auth.DELETE("/items/:id", itemControll.Delete())
auth.PUT("/items/:id", itemControll.Update())
if err := e.Start(":8000"); err != nil {
log.Println(err.Error())
}
}