/
main.go
47 lines (40 loc) · 1.3 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
package main
import (
"context"
"github.com/emilhauk/identity-api/config"
"github.com/emilhauk/identity-api/endpoint"
"github.com/emilhauk/identity-api/store"
"github.com/sirupsen/logrus"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"net/http"
"time"
)
func setupRoutes(endpoints *endpoint.Endpoints) {
http.HandleFunc("/register", endpoints.RegisterHandler)
http.HandleFunc("/login", endpoints.LoginHandler)
http.HandleFunc("/jwt", endpoints.JwtHandler)
http.HandleFunc("/logout", endpoints.LogoutHandler)
http.HandleFunc("/keys", endpoints.PublicKeyHandler)
http.HandleFunc("/", endpoints.WebHandler)
}
func main() {
c := config.NewConfig()
logrus.SetLevel(c.LogLevel)
mongoClient, err := mongo.NewClient(options.Client().ApplyURI(c.MongoDBUrl))
if err != nil {
logrus.Panicln(err)
}
ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)
err = mongoClient.Connect(ctx)
if err != nil {
logrus.Fatalln(err)
}
defer mongoClient.Disconnect(ctx)
mongoStore := store.NewMongoStore(mongoClient)
keyStore := store.NewRSAKeyStore(c.KeyStorePath, c.DefaultKeyId)
endpoints := endpoint.NewEndpoints(&mongoStore, &keyStore)
setupRoutes(endpoints)
logrus.Infof("Identity-service listening on %s", c.Host)
logrus.Fatalln(http.ListenAndServe(c.Host, nil))
}