-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
67 lines (53 loc) · 1.25 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
package main
import (
"github.com/mvannes/jwt-server/user"
"log"
"net/http"
"time"
"github.com/mvannes/jwt-server/config"
"github.com/mvannes/jwt-server/jwk"
"github.com/mvannes/jwt-server/jwt"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
)
func Routes(config config.Config) *chi.Mux {
r := chi.NewRouter()
r.Use(middleware.RequestID)
r.Use(middleware.RealIP)
r.Use(middleware.Logger)
r.Use(middleware.Recoverer)
r.Use(middleware.StripSlashes)
r.Use(middleware.Timeout(60 * time.Second))
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("OK"))
})
r.Mount("/jwk", jwk.Routes(config))
r.Mount("/jwt", jwt.Routes(config))
r.Mount("/users", user.Routes(config))
return r
}
func initKeys() error {
// TODO: Requires me to make a keys dir by hand now.
// Configure it, and change this to not require manual intervention.
km := jwk.NewKeyManager()
_, err := km.FetchLatestKeyVersion()
if nil == err {
return nil
}
if err == jwk.KeyNotFound {
err = km.CreateKeyPair()
}
return err
}
func main() {
c := config.Config{
DomainName: "localhost:8080",
JWKLocationURL: "/",
}
err := initKeys()
if nil != err {
log.Fatal(err)
}
r := Routes(c)
log.Fatal(http.ListenAndServe(":8080", r))
}