-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
103 lines (91 loc) · 2.78 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
/*
* Nuts node
* Copyright (C) 2021 Nuts community
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
package main
import (
"context"
"errors"
"fmt"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"github.com/labstack/echo/v4"
"github.com/nuts-foundation/nuts-pxp/api/opa"
"github.com/nuts-foundation/nuts-pxp/api/pip"
"github.com/nuts-foundation/nuts-pxp/config"
"github.com/nuts-foundation/nuts-pxp/db"
"github.com/nuts-foundation/nuts-pxp/policy"
)
func main() {
// Listen for interrupt signals (CTRL/CMD+C, OS instructing the process to stop) to cancel context.
ctx, cancelNotify := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer cancelNotify()
// read config using koanf
cfg := config.Config{}
if err := cfg.Load(); err != nil {
panic(err)
}
// init DB
sqlDb, err := db.New(cfg)
if err != nil {
panic(err)
}
defer func() {
err := sqlDb.Close()
if err != nil {
panic(err)
}
}()
// init OPA
decisionMaker, err := policy.New(cfg, sqlDb)
if err != nil {
panic(err)
}
// init http server and bind to localhost:8080
echoServer := echo.New()
echoServer.HTTPErrorHandler = errorHandlerfunc
echoServer.HideBanner = true
echoServer.HidePort = true
// init API & register routes
pipController := &pip.Wrapper{DB: sqlDb}
opaController := &opa.Wrapper{DecisionMaker: decisionMaker}
pip.RegisterHandlers(echoServer, pip.NewStrictHandler(pipController, []pip.StrictMiddlewareFunc{}))
opa.RegisterHandlers(echoServer, opa.NewStrictHandler(opaController, []opa.StrictMiddlewareFunc{}))
// Start server
go func() {
if err := echoServer.Start(":8080"); err != nil && errors.Is(err, http.ErrServerClosed) {
panic("shutting down the server")
}
}()
// Wait for interrupt signal to gracefully shutdown the server with a timeout of 10 seconds.
<-ctx.Done()
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
if err := echoServer.Shutdown(ctx); err != nil {
panic(err)
}
}
func errorHandlerfunc(err error, ctx echo.Context) {
fmt.Printf("error: %s\n", err.Error())
ctx.Response().Status = http.StatusInternalServerError
if !ctx.Response().Committed {
ctx.Response().Write([]byte(err.Error()))
}
}