-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmain.go
More file actions
140 lines (110 loc) · 3.73 KB
/
main.go
File metadata and controls
140 lines (110 loc) · 3.73 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package main
import (
"context"
"fmt"
"net/http"
"os"
"os/signal"
"strings"
"syscall"
"time"
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)
logger := logging.Log()
logger.Infof("Configuration is: %s", logging.PrettyPrintObject(configuration))
verifier.InitVerifier(&configuration)
verifier.InitPresentationParser(&configuration, Health())
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)
srv := &http.Server{
Addr: fmt.Sprintf("0.0.0.0:%v", configuration.Server.Port),
Handler: router,
ReadTimeout: time.Duration(configuration.Server.ReadTimeout) * time.Second,
WriteTimeout: time.Duration(configuration.Server.WriteTimeout) * time.Second,
IdleTimeout: time.Duration(configuration.Server.IdleTimeout) * time.Second,
}
// Start the server in a goroutine so it doesn't block
go func() {
logging.Log().Infof("Starting server on port %v", configuration.Server.Port)
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
logging.Log().Errorf("Failed to start server: %v", err)
os.Exit(1)
}
}()
// --- Graceful Shutdown Logic ---
quit := make(chan os.Signal, 1)
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
<-quit
logging.Log().Info("Shutting down server...")
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(configuration.Server.ShutdownTimeout)*time.Second)
defer cancel()
if err := srv.Shutdown(ctx); err != nil {
logging.Log().Errorf("Server forced to shutdown: %v", err)
}
logging.Log().Info("Server exiting gracefully")
}
// initiate the router
func getRouter() *gin.Engine {
// the openapi generated router uses the defaults, which we want to override to improve and configure logging
writer := logging.GetGinInternalWriter()
gin.DefaultWriter = writer
gin.DefaultErrorWriter = writer
router := gin.New()
router.Use(logging.GinHandlerFunc(), gin.Recovery())
for _, route := range api.NewRouter().Routes() {
router.Handle(route.Method, 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)
}