-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
143 lines (127 loc) · 3.59 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package main
import (
"log"
"net/http"
"strconv"
auth "github.com/iden3/go-iden3-auth/v2"
"github.com/iden3/go-iden3-auth/v2/loaders"
"github.com/iden3/go-iden3-auth/v2/pubsignals"
"github.com/iden3/go-iden3-auth/v2/state"
"github.com/iden3/go-service-template/config"
"github.com/iden3/go-service-template/pkg/logger"
httprouter "github.com/iden3/go-service-template/pkg/router/http"
"github.com/iden3/go-service-template/pkg/router/http/handlers"
"github.com/iden3/go-service-template/pkg/services/authentication"
"github.com/iden3/go-service-template/pkg/services/issuer"
"github.com/iden3/go-service-template/pkg/services/system"
"github.com/iden3/go-service-template/pkg/shutdown"
httptransport "github.com/iden3/go-service-template/pkg/transport/http"
"github.com/pkg/errors"
)
func main() {
cfg, err := config.Parse()
if err != nil {
log.Fatalf("failed to parse config: %v", err)
}
if err = logger.SetDefaultLogger(
cfg.Log.Environment,
cfg.Log.LogLevel(),
); err != nil {
log.Fatalf("failed to set default logger: %v", err)
}
// init dependencies
authverifier, err := initializationAuthVerifier(cfg)
if err != nil {
logger.WithError(err).Fatal("error creating auth verifier")
}
httpserver := newHTTPServer(
cfg,
authverifier,
cfg.Issuers,
)
newShutdownManager(httpserver).HandleShutdownSignal()
}
func newHTTPServer(
cfg *config.Config,
authverifier *auth.Verifier,
issuers []string,
) *httptransport.Server {
// init services
authenticationService := authentication.NewAuthenticationService(
authverifier,
)
issuerService := issuer.NewIssuerService(
issuers,
)
// init handlers
systemHandlers := handlers.NewSystemHandler(
system.NewReadinessService(),
system.NewLivenessService(),
)
authenticationHandlers := handlers.NewAuthenticationHandlers(
cfg.ExternalHost,
authenticationService,
)
issuerHandlers := handlers.NewIssuerHandlers(
issuerService,
)
// init routers
h := httprouter.NewHandlers(
systemHandlers,
authenticationHandlers,
issuerHandlers,
)
routers := h.NewRouter(
httprouter.WithOrigins(cfg.HTTPServer.Origins),
)
// run http server
httpserver := httptransport.New(
routers,
httptransport.WithWriteTimeout(0),
httptransport.WithHost(cfg.HTTPServer.Host, cfg.HTTPServer.Port),
)
go func() {
err := httpserver.Start()
if errors.Is(err, http.ErrServerClosed) {
logger.Info("HTTP server closed by request")
} else {
logger.WithError(err).Fatal("http server closed with error")
}
}()
return httpserver
}
func newShutdownManager(toclose ...shutdown.Shutdown) *shutdown.Manager {
m := shutdown.NewManager()
for _, s := range toclose {
m.Register(s)
}
return m
}
func chainIDToDIDPrefix(chainID int) string {
p := map[int]string{
137: "polygon:main",
80001: "polygon:mumbai",
80002: "polygon:amoy",
21000: "privado:main",
21001: "privado:test",
}
return p[chainID]
}
func initializationAuthVerifier(configuration *config.Config) (*auth.Verifier, error) {
var resolvers = make(map[string]pubsignals.StateResolver, len(configuration.SupportedStateContracts))
for network, contractAddress := range configuration.SupportedStateContracts {
rpcURL, ok := configuration.SupportedRPC[network]
if !ok {
return nil, errors.Errorf("no rpc for network %s", network)
}
chainID, _ := strconv.Atoi(network)
resolvers[chainIDToDIDPrefix(chainID)] = state.NewETHResolver(rpcURL, contractAddress)
}
verifier, err := auth.NewVerifier(loaders.FSKeyLoader{
Dir: configuration.KeysDirPath,
}, resolvers)
if err != nil {
return nil, errors.Errorf("error creating verifier: %v", err)
}
return verifier, nil
}