-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.go
94 lines (75 loc) · 2.67 KB
/
app.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
package upvsMock
import (
"crypto/rsa"
"fmt"
"net/http"
muxHandlers "github.com/gorilla/handlers"
"github.com/gorilla/mux"
log "github.com/sirupsen/logrus"
"github.com/slovak-egov/einvoice/pkg/handlerutil"
"github.com/slovak-egov/einvoice/pkg/keys"
)
var corsOptions = []muxHandlers.CORSOption{
muxHandlers.AllowedHeaders([]string{"Content-Type", "Origin", "Accept", "X-API-Key", "Authorization"}),
muxHandlers.AllowedOrigins([]string{"*"}),
muxHandlers.AllowedMethods([]string{"GET", "POST", "OPTIONS", "DELETE", "PUT", "PATCH"}),
}
type App struct {
config *Configuration
router *mux.Router
OboTokenPrivate *rsa.PrivateKey
OboTokenPublic *rsa.PublicKey
ApiTokenPublic *rsa.PublicKey
}
func NewApp() *App {
appConfig := New()
oboTokenPrivate, err := keys.GetPrivateKey(appConfig.OboTokenPrivateKey)
if err != nil {
log.WithField("error", err).Fatal("upvs.keys.oboTokenPrivate")
}
oboTokenPublic, err := keys.GetPublicKey(appConfig.OboTokenPublicKey)
if err != nil {
log.WithField("error", err).Fatal("upvs.keys.oboTokenPublic")
}
apiTokenPublic, err := keys.GetPublicKey(appConfig.ApiTokenPublicKey)
if err != nil {
log.WithField("error", err).Fatal("upvs.keys.apiTokenPublic")
}
a := &App{
config: appConfig,
router: mux.NewRouter(),
OboTokenPrivate: oboTokenPrivate,
OboTokenPublic: oboTokenPublic,
ApiTokenPublic: apiTokenPublic,
}
a.initializeHandlers()
return a
}
func registerHandler(router *mux.Router, method, path string, handler func(http.ResponseWriter, *http.Request) error) {
router.HandleFunc(path, handlerutil.ErrorHandler(handler)).Methods(method)
}
func (a *App) initializeHandlers() {
a.router.Use(handlerutil.RequestIdMiddleware)
a.router.Use(handlerutil.LoggingMiddleware)
a.router.Use(handlerutil.ErrorRecovery)
registerHandler(a.router, "GET", "/login", a.handleLogin)
registerHandler(a.router, "GET", "/logout", a.handleLogout)
registerHandler(a.router, "GET", "/api/upvs/identity", a.handleUserInfo)
registerHandler(a.router, "GET", "/api/upvs/assertion", a.handleSaml)
registerHandler(a.router, "POST", "/api/sktalk/receive", a.handleSkTalkReceive)
}
func (a *App) Run() {
srv := &http.Server{
Handler: muxHandlers.CORS(corsOptions...)(a.router),
Addr: fmt.Sprintf("%s:%d", a.config.Host, a.config.Port),
WriteTimeout: a.config.ServerWriteTimeout,
ReadTimeout: a.config.ServerReadTimeout,
}
log.WithField("address", srv.Addr).Info("app.server.started")
go func() {
if err := srv.ListenAndServe(); err != http.ErrServerClosed {
log.WithField("error", err.Error()).Fatal("app.server.failed")
}
}()
handlerutil.GracefulShutdown(srv, a.config.GracefulTimeout)
}