forked from ic-matcom/api.dapp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
102 lines (76 loc) · 3.74 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
package main
import (
"fmt"
"github.com/go-playground/validator/v10"
"github.com/ic-matcom/api.dapp/api/endpoints"
"github.com/ic-matcom/api.dapp/api/middlewares"
_ "github.com/ic-matcom/api.dapp/docs"
"github.com/ic-matcom/api.dapp/service/utils"
"github.com/iris-contrib/swagger/v12" // swagger middleware for Iris
"github.com/iris-contrib/swagger/v12/swaggerFiles" // swagger embed files
"github.com/kataras/iris/v12"
"github.com/kataras/iris/v12/middleware/logger"
_ "github.com/lib/pq"
)
// @title eVote
// @version 0.0
// @description Hyperledger Fabric blockchain solution for electronic election system
// @contact.name Instituto Criptografía MATCOM,UH
// @contact.url http://www.matcom.uh.cu/
// @contact.email [email protected]
// @authorizationurl https://example.com/oauth/authorize
// TIPS This Ip here 👇🏽 must be change when compiling to deploy, can't figure out how to do it dynamically with Iris.
// @host localhost:7001
// @BasePath /
func main() {
// region ======== GLOBALS ===============================================================
v := validator.New() // Validator instance. Reference https://github.com/kataras/iris/wiki/Model-validation | https://github.com/go-playground/validator
app := iris.New() // App instance
app.Validator = v // Register validation on the iris app
// Services
svcConfig := utils.NewSvcConfig() // Creating Configuration Service
svcResponse := utils.NewSvcResponse(svcConfig) // Creating Response Service
// endregion =============================================================================
// region ======== MIDDLEWARES ===========================================================
// Our custom CORS middleware.
crs := func(ctx iris.Context) {
ctx.Header("Access-Control-Allow-Origin", "*")
ctx.Header("Access-Control-Allow-Credentials", "true")
if ctx.Method() == iris.MethodOptions {
ctx.Header("Access-Control-Methods",
"POST, PUT, PATCH, DELETE")
ctx.Header("Access-Control-Allow-Headers",
"Access-Control-Allow-Origin,Content-Type,authorization")
ctx.Header("Access-Control-Max-Age",
"86400")
ctx.StatusCode(iris.StatusNoContent)
return
}
ctx.Next()
}
// built-ins
app.Use(logger.New())
app.UseRouter(crs) // Recovery middleware recovers from any panics and writes a 500 if there was one.
// custom middleware
MdwAuthChecker := middlewares.NewAuthCheckerMiddleware([]byte(svcConfig.JWTSignKey))
// endregion =============================================================================
// region ======== ENDPOINT REGISTRATIONS ================================================
endpoints.NewAuthHandler(app, &MdwAuthChecker, svcResponse, svcConfig)
endpoints.NewBlockchainTxsHandler(app, &MdwAuthChecker, svcResponse, svcConfig) // Blockchain transactions handlers
endpoints.NewFilesTxsHandler(app, &MdwAuthChecker, svcResponse, svcConfig)
endpoints.NewUserTxsHandler(app, &MdwAuthChecker, svcResponse, svcConfig)
// endregion =============================================================================
// region ======== SWAGGER REGISTRATION ==================================================
// sc == swagger config
sc := &swagger.Config{
DeepLinking: true,
URL: "http://" + svcConfig.ApiDocIp + ":" + svcConfig.DappPort + "/swagger/apidoc.json", // The url pointing to API definition
}
// use swagger middleware to
app.Get("/swagger/{any:path}", swagger.CustomWrapHandler(sc, swaggerFiles.Handler))
// endregion =============================================================================
addr := fmt.Sprintf("%s:%s", svcConfig.ApiDocIp, svcConfig.DappPort)
// run localhost
app.Listen(addr)
}
// TODO: the node-client and node-peer connection must be invoke with client identity making the request