-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
88 lines (63 loc) · 3.39 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
package main
import (
"github.com/kataras/iris/v12"
"github.com/kataras/iris/v12/middleware/logger"
"github.com/kataras/iris/v12/middleware/recover"
"github.com/iris-contrib/swagger/v12" // swagger middleware for Iris
"github.com/iris-contrib/swagger/v12/swaggerFiles" // swagger embed files
"github.com/go-playground/validator/v10"
_ "github.com/lib/pq"
"go.api.backend/api/endpoints"
_ "go.api.backend/docs"
"go.api.backend/api/middlewares"
"go.api.backend/schema/database"
"go.api.backend/service/utils"
)
// @title Shell Project
// @version 0.0
// @description Api description shell project
// @contact.name Name Test
// @contact.url http://contact.sample/text
// @contact.email [email protected]
// @authorizationurl https://example.com/oauth/authorize
// @host localhost:8080
// @BasePath /
func main() {
// region ======== GLOBALS ===============================================================
v := validator.New() // Validator instance
// TIP validation reference https://github.com/kataras/iris/wiki/Model-validation | https://github.com/go-playground/validator | https://medium.com/@apzuk3/input-validation-in-golang-bc24cdec1835
app := iris.New() // App instance
app.Validator = v // Register validation on the iris app
// Services
svcC := utils.NewSvcConfig("D:\\Source\\Go\\src\\go.api.backend\\conf.dev.yaml") // Creating Configuration Service
svcR := utils.NewSvcResponse(svcC) // Creating Response Service
// endregion =============================================================================
// region ======== MIDDLEWARES ===========================================================
// built-ins
app.Use(logger.New())
app.UseRouter(recover.New()) // Recovery middleware recovers from any panics and writes a 500 if there was one.
// customs
MdwAuthChecker := middlewares.NewAuthCheckerMiddleware([]byte(svcC.JWTSignKey)) // TODO get the JWTSignKey from OS env
// endregion =============================================================================
// region ======== DATABASE BOOTSTRAPPING ================================================
pgdb := database.Bootstrap(svcC) // Starting the database and creating the engine
// database.CreateSchema(pgdb, false) // Table creation method
// database.MkMigrations(svcC) // Making migrations
// endregion =============================================================================
// region ======== ENDPOINT REGISTRATIONS ================================================
endpoints.NewBookHandler(app, pgdb, svcR)
endpoints.NewAuthHandler(app, &MdwAuthChecker, svcR, svcC)
// endregion =============================================================================
// region ======== SWAGGER REGISTRATION ==================================================
// version https://gitee.com/luckymonkey006/swagger
// sc == swagger config
sc := &swagger.Config {
DeepLinking: true,
URL: "http://localhost:8080/swagger/apidoc.json", // The url pointing to API definition
}
// use swagger middleware to
app.Get("/swagger/{any:path}", swagger.CustomWrapHandler(sc, swaggerFiles.Handler))
// endregion =============================================================================
app.Run(iris.Addr(":8080"))
// app.Listen(":5000", iris.WithOptimizations) see https://github.com/kataras/iris/issues/1739, check if it related to the context.go 2307 line
}