-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
101 lines (82 loc) · 2.08 KB
/
main.go
File metadata and controls
101 lines (82 loc) · 2.08 KB
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
package main
import (
"fmt"
"strings"
"app/src/domain/abstract/dtos"
"app/src/infra/database"
"app/src/main/docs"
"app/src/main/routes"
"github.com/gin-gonic/gin"
)
func formatMessage(message string) string {
return fmt.Sprintf("%s%s", strings.ToUpper(message[:1]), message[1:])
}
func main() {
var router = gin.Default()
router.LoadHTMLGlob("src/presentation/templates/**/*")
var databaseConnection = database.InitializeDatabaseConnection()
err := database.ExecuteDatabaseMigrations(databaseConnection)
if err != nil {
panic(err)
}
var routes = append(routes.BaseRoutes, routes.TemplateRoutes...)
docGenerator := docs.NewApiDocGenerator("Api", "Api description", router)
docGenerator.RegisterRoutes(routes)
router.GET("/", func(context *gin.Context) {
context.JSON(200, gin.H{
"message": "Api is running",
})
})
for _, route := range routes {
var path = route.Path
var method = route.Method
var controller = route.Controller
var middlewares = route.Middlewares
router.Handle(method, path, func(context *gin.Context) {
var data = dtos.DtoType{}
var err error
for key, value := range context.Request.Header {
data[key] = value
}
for key, value := range context.Request.URL.Query() {
data[key] = value[0]
}
if method != "GET" {
body := dtos.DtoType{}
if context.BindJSON(&body) == nil {
for key, value := range body {
data[key] = value
}
}
}
for _, middleware := range middlewares {
data, err = middleware.Execute(data)
if err != nil {
context.JSON(400, gin.H{
"error": formatMessage(err.Error()),
})
return
}
}
response, err, status := controller.Execute(databaseConnection, data)
if route.TemplatePath != "" {
if err != nil {
context.HTML(status, "index.html", err)
return
}
context.HTML(status, route.TemplatePath, response)
return
} else {
if err != nil {
context.JSON(status, gin.H{
"error": formatMessage(err.Error()),
})
return
}
context.JSON(status, response)
return
}
})
}
router.Run()
}