-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
117 lines (93 loc) · 2.97 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
package main
import (
"fmt"
"net/http"
"os"
"strings"
configModel "github.com/fiware/VCVerifier/config"
logging "github.com/fiware/VCVerifier/logging"
api "github.com/fiware/VCVerifier/openapi"
"github.com/fiware/VCVerifier/verifier"
"github.com/foolin/goview/supports/ginview"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
"github.com/penglongli/gin-metrics/ginmetrics"
)
// default config file location - can be overwritten by envvar
var configFile string = "server.yaml"
/**
* Startup method to run the gin-server.
*/
func main() {
configuration, err := configModel.ReadConfig(configFile)
if err != nil {
panic(err)
}
logging.Configure(
configuration.Logging.JsonLogging,
configuration.Logging.Level,
configuration.Logging.LogRequests,
configuration.Logging.PathsToSkip)
logger := logging.Log()
logger.Infof("Configuration is: %s", logging.PrettyPrintObject(configuration))
verifier.InitVerifier(&configuration)
router := getRouter()
// health check
router.GET("/health", HealthReq)
router.Use(cors.New(cors.Config{
// we need to allow all, since we do not know the potential origin of a wallet
AllowOrigins: []string{"*"},
AllowMethods: []string{"POST", "GET"},
AllowHeaders: []string{"Origin", "Content-Type", "Authorization"},
AllowCredentials: true,
}))
//new template engine
router.HTMLRender = ginview.Default()
// static files for the frontend
router.Static("/static", configuration.Server.StaticDir)
templateDir := configuration.Server.TemplateDir
if templateDir != "" {
if strings.HasSuffix(templateDir, "/") {
templateDir = templateDir + "*.html"
} else {
templateDir = templateDir + "/*.html"
}
logging.Log().Infof("Intialize templates from %s", templateDir)
router.LoadHTMLGlob(templateDir)
}
// initiate metrics
metrics := ginmetrics.GetMonitor()
metrics.SetMetricPath("/metrics")
metrics.Use(router)
router.Run(fmt.Sprintf("0.0.0.0:%v", configuration.Server.Port))
logger.Infof("Started router at %v", configuration.Server.Port)
}
// initiate the router
func getRouter() *gin.Engine {
// the openapi generated router uses the defaults, which we want to override to improve and configure logging
router := gin.New()
router.Use(logging.GinHandlerFunc(), gin.Recovery())
for _, route := range api.NewRouter().Routes() {
switch route.Method {
case http.MethodGet:
router.GET(route.Path, route.HandlerFunc)
case http.MethodPost:
router.POST(route.Path, route.HandlerFunc)
case http.MethodPut:
router.PUT(route.Path, route.HandlerFunc)
case http.MethodPatch:
router.PATCH(route.Path, route.HandlerFunc)
case http.MethodDelete:
router.DELETE(route.Path, route.HandlerFunc)
}
}
return router
}
// allow override of the config-file on init. Everything else happens on main to improve testability
func init() {
configFileEnv := os.Getenv("CONFIG_FILE")
if configFileEnv != "" {
configFile = configFileEnv
}
logging.Log().Infof("Will read config from %s", configFile)
}