-
Notifications
You must be signed in to change notification settings - Fork 67
/
Copy pathmain.go
81 lines (67 loc) · 1.96 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
package main
import (
"errors"
"fmt"
"github.com/sogko/slumber-sessions"
"github.com/sogko/slumber-users"
"github.com/sogko/slumber/middlewares/context"
"github.com/sogko/slumber/middlewares/mongodb"
"github.com/sogko/slumber/middlewares/renderer"
"github.com/sogko/slumber/server"
"io/ioutil"
"time"
)
func main() {
// try to load signing keys for token authority
// NOTE: DO NOT USE THESE KEYS FOR PRODUCTION! FOR DEMO ONLY
privateSigningKey, err := ioutil.ReadFile("keys/demo.rsa")
if err != nil {
panic(errors.New(fmt.Sprintf("Error loading private signing key: %v", err.Error())))
}
publicSigningKey, err := ioutil.ReadFile("keys/demo.rsa.pub")
if err != nil {
panic(errors.New(fmt.Sprintf("Error loading public signing key: %v", err.Error())))
}
// create current project context
ctx := context.New()
// set up DB session
db := mongodb.New(&mongodb.Options{
ServerName: "localhost",
DatabaseName: "test-go-app",
})
_ = db.NewSession()
// set up Renderer (unrolled_render)
renderer := renderer.New(&renderer.Options{
IndentJSON: true,
}, renderer.JSON)
// set up users resource
usersResource := users.NewResource(ctx, &users.Options{
Database: db,
Renderer: renderer,
})
// set up sessions resource
sessionsResource := sessions.NewResource(ctx, &sessions.Options{
PrivateSigningKey: privateSigningKey,
PublicSigningKey: publicSigningKey,
Database: db,
Renderer: renderer,
UserRepositoryFactory: usersResource.UserRepositoryFactory,
})
// init server
s := server.NewServer(&server.Config{
Context: ctx,
})
// set up router
ac := server.NewAccessController(ctx, renderer)
router := server.NewRouter(s.Context, ac)
// add REST resources to router
router.AddResources(sessionsResource, usersResource)
// add middlewares
s.UseMiddleware(sessionsResource.NewAuthenticator())
// setup router
s.UseRouter(router)
// bam!
s.Run(":3001", server.Options{
Timeout: 10*time.Second,
})
}