-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
150 lines (131 loc) · 4.68 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package main
import (
"fmt"
_ "github.com/Angak0k/pimpmypack/docs"
"github.com/Angak0k/pimpmypack/pkg/accounts"
"github.com/Angak0k/pimpmypack/pkg/config"
"github.com/Angak0k/pimpmypack/pkg/database"
"github.com/Angak0k/pimpmypack/pkg/inventories"
"github.com/Angak0k/pimpmypack/pkg/packs"
"github.com/Angak0k/pimpmypack/pkg/security"
"github.com/gin-gonic/gin"
swaggerFiles "github.com/swaggo/files"
ginSwagger "github.com/swaggo/gin-swagger"
)
const (
envDev = "DEV"
envLocal = "LOCAL"
)
func initApp() error {
// init env
err := config.EnvInit(".env")
if err != nil {
return fmt.Errorf("error loading .env file or environment variable : %w", err)
}
// init DB
err = database.Initialization()
if err != nil {
return fmt.Errorf("error connecting database : %w", err)
}
// init DB migration
err = database.Migrate()
if err != nil {
return fmt.Errorf("error migrating database : %w", err)
}
return nil
}
// @title PimpMyPack API
// @description API server to manage Backpack Inventory and Packing Lists
// @version 1.0
// @host pmp-dev.alki.earth
// @Schemes https
// @BasePath /api
func main() {
err := initApp()
if err != nil {
panic(err)
}
if config.Stage == envDev || config.Stage == envLocal {
gin.SetMode(gin.DebugMode)
} else {
gin.SetMode(gin.ReleaseMode)
}
router := gin.Default()
setupRoutes(router)
startServer(router)
}
func setupRoutes(router *gin.Engine) {
setupPublicRoutes(router)
setupProtectedRoutes(router)
setupPrivateRoutes(router)
setupSwaggerRoutes(router)
}
func setupPublicRoutes(router *gin.Engine) {
public := router.Group("/api")
public.POST("/register", accounts.Register)
public.POST("/login", accounts.Login)
public.GET("/confirmemail", accounts.ConfirmEmail)
public.POST("/forgotpassword", accounts.ForgotPassword)
public.GET("/sharedlist/:sharing_code", packs.SharedList)
}
func setupProtectedRoutes(router *gin.Engine) {
protected := router.Group("/api/v1")
protected.Use(security.JwtAuthProcessor())
protected.GET("/myaccount", accounts.GetMyAccount)
protected.PUT("/myaccount", accounts.PutMyAccount)
protected.PUT("/mypassword", accounts.PutMyPassword)
protected.GET("/myinventory", inventories.GetMyInventory)
protected.GET("/mypacks", packs.GetMyPacks)
protected.GET("/mypack/:id", packs.GetMyPackByID)
protected.POST("/mypack", packs.PostMyPack)
protected.PUT("/mypack/:id", packs.PutMyPackByID)
protected.DELETE("/mypack/:id", packs.DeleteMyPackByID)
protected.GET("/mypack/:id/packcontents", packs.GetMyPackContentsByPackID)
protected.POST("/mypack/:id/packcontent", packs.PostMyPackContent)
protected.PUT("/mypack/:id/packcontent/:item_id", packs.PutMyPackContentByID)
protected.DELETE("/mypack/:id/packcontent/:item_id", packs.DeleteMyPackContentByID)
protected.GET("/myinventory/:id", inventories.GetMyInventoryByID)
protected.POST("/myinventory", inventories.PostMyInventory)
protected.PUT("/myinventory/:id", inventories.PutMyInventoryByID)
protected.DELETE("/myinventory/:id", inventories.DeleteMyInventoryByID)
protected.POST("/importfromlighterpack", packs.ImportFromLighterPack)
}
func setupPrivateRoutes(router *gin.Engine) {
private := router.Group("/api/admin")
private.Use(security.JwtAuthAdminProcessor())
private.GET("/accounts", accounts.GetAccounts)
private.GET("/accounts/:id", accounts.GetAccountByID)
private.POST("/accounts", accounts.PostAccount)
private.PUT("/accounts/:id", accounts.PutAccountByID)
private.DELETE("/accounts/:id", accounts.DeleteAccountByID)
private.GET("/inventories", inventories.GetInventories)
private.GET("/inventories/:id", inventories.GetInventoryByID)
private.POST("/inventories", inventories.PostInventory)
private.PUT("/inventories/:id", inventories.PutInventoryByID)
private.DELETE("/inventories/:id", inventories.DeleteInventoryByID)
private.GET("/packs", packs.GetPacks)
private.GET("/packs/:id", packs.GetPackByID)
private.POST("/packs", packs.PostPack)
private.PUT("/packs/:id", packs.PutPackByID)
private.DELETE("/packs/:id", packs.DeletePackByID)
private.GET("/packcontents", packs.GetPackContents)
private.GET("/packcontents/:id", packs.GetPackContentByID)
private.POST("/packcontents", packs.PostPackContent)
private.PUT("/packcontents/:id", packs.PutPackContentByID)
private.DELETE("/packcontents/:id", packs.DeletePackContentByID)
private.GET("/packs/:id/packcontents", packs.GetPackContentsByPackID)
}
func setupSwaggerRoutes(router *gin.Engine) {
if config.Stage == envDev || config.Stage == envLocal {
router.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
}
}
func startServer(router *gin.Engine) {
address := ":8080"
if config.Stage == envLocal {
address = "localhost:8080"
}
if err := router.Run(address); err != nil {
panic(err)
}
}