-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
75 lines (65 loc) · 2.2 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
package main
import (
"flag"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
"github.com/kadukm/banking_spa/server/handling"
)
func main() {
dev := flag.Bool("dev", false, "a bool")
flag.Parse()
if *dev {
gin.SetMode(gin.DebugMode)
} else {
gin.SetMode(gin.ReleaseMode)
go runCommonEngine()
}
runAPIEngine()
}
func runCommonEngine() {
commonEngine := gin.Default()
buildCommonRoutes(commonEngine)
commonEngine.Run(":8080")
}
func runAPIEngine() {
apiEngine := gin.Default()
config := cors.DefaultConfig()
config.AllowOrigins = []string{"http://localhost:8080"}
config.AllowHeaders = []string{"Content-Type", handling.CSRFTokenName}
config.AllowCredentials = true
apiEngine.Use(cors.New(config))
buildAPIRoutes(apiEngine)
apiEngine.Run(":3000")
}
func buildCommonRoutes(engine *gin.Engine) {
indexHandler := func(c *gin.Context) {
c.File("./index.html")
}
engine.Static("/assets", "./assets")
engine.GET("/", handling.CSRFGeneration, indexHandler)
engine.GET("/login", handling.CSRFGeneration, indexHandler)
engine.POST("/login", handling.Login)
engine.GET("/companies/:companyID", handling.CSRFGeneration, indexHandler)
engine.GET("/admin-panel", handling.CheckSession, handling.CSRFGeneration, indexHandler)
}
func buildAPIRoutes(engine *gin.Engine) {
api := engine.Group("/api")
{
payments := api.Group("/payments")
{
payments.GET("/from_card", handling.CheckSession, handling.GetPaymentsFromCard)
payments.GET("/from_card/sort", handling.CheckSession, handling.GetPaymentsFromCardSorted)
payments.GET("/requests", handling.CheckSession, handling.GetPaymentRequests)
payments.GET("/requests/sort", handling.CheckSession, handling.GetPaymentRequestsSorted)
payments.PATCH("/from_card/:paymentID", handling.CheckSession, handling.CheckCSRFToken, handling.PatchPaymentFromCard)
payments.POST("/from_card", handling.CheckCSRFToken, handling.PostPaymentFromCard)
payments.POST("/requests", handling.CheckCSRFToken, handling.PostPaymentRequest)
payments.GET("/via_bank", handling.GetPaymentViaBank)
}
companies := api.Group("/companies")
{
companies.GET("/:companyID", handling.GetCompany)
companies.GET("/:companyID/products", handling.GetProducts)
}
}
}