-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
60 lines (46 loc) · 1.46 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
package main
import (
"github.com/karlkfi/inject"
"github.com/karlkfi/inject-example-server/controller"
"github.com/karlkfi/inject-example-server/model"
"github.com/facebookgo/grace/gracehttp"
"net/http"
"log"
)
func main() {
graph := objectGraph()
var mux controller.MuxServer
inject.ExtractAssignable(graph, &mux)
var controllers []controller.Controller
inject.FindAssignable(graph, &controllers)
for _, c := range controllers {
log.Println("Registering controller:", c.Name())
c.RegisterHandlers(mux)
}
// serve and listen for shutdown signals
gracehttp.Serve(
&http.Server{Addr: "0.0.0.0:8080", Handler: mux},
)
}
func objectGraph() inject.Graph {
var (
server *http.ServeMux
userRepo model.UserRepo
users = users()
indexController *controller.IndexController
userController *controller.UserController
)
graph := inject.NewGraph()
graph.Define(&server, inject.NewProvider(http.NewServeMux))
graph.Define(&userRepo, inject.NewProvider(model.NewUserRepo, &users))
graph.Define(&indexController, inject.NewProvider(controller.NewIndexController))
graph.Define(&userController, inject.NewProvider(controller.NewUserController, &userRepo))
return graph
}
func users() []model.User {
return []model.User{
{ ID: "0", Name: "Alice" },
{ ID: "1", Name: "Bob" },
{ ID: "2", Name: "Charlie" },
}
}