-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmain.go
247 lines (217 loc) · 6.95 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
//go:generate granate
package main
import (
"flag"
"fmt"
"net/http"
"os"
"os/signal"
"sync"
"syscall"
"github.com/go-kit/kit/log"
kitprometheus "github.com/go-kit/kit/metrics/prometheus"
kithttp "github.com/go-kit/kit/transport/http"
goredis "github.com/go-redis/redis"
stdprometheus "github.com/prometheus/client_golang/prometheus"
"github.com/nicolaiskogheim/go-kit-graphql-todo/auth"
"github.com/nicolaiskogheim/go-kit-graphql-todo/graphql"
"github.com/nicolaiskogheim/go-kit-graphql-todo/inmem"
"github.com/nicolaiskogheim/go-kit-graphql-todo/models"
"github.com/nicolaiskogheim/go-kit-graphql-todo/redis"
"github.com/nicolaiskogheim/go-kit-graphql-todo/schema"
"github.com/nicolaiskogheim/go-kit-graphql-todo/session"
"github.com/nicolaiskogheim/go-kit-graphql-todo/todo"
"github.com/nicolaiskogheim/go-kit-graphql-todo/user"
)
const (
defaultPort = "8080"
defaultDebugPort = "1337"
)
func main() {
var (
port = envString("PORT", defaultPort)
// TODO(nicolai): This will be used with a /metrics endpoint
// debugPort = envString("DEBUG_PORT", defaultDebugPort)
httpAddr = flag.String("http.addr", ":"+port, "HTTP listen address")
)
var logger log.Logger
{
logger = log.NewLogfmtLogger(os.Stderr)
logger = &serializedLogger{Logger: logger}
logger = log.With(logger,
"ts", log.DefaultTimestampUTC,
"caller", log.DefaultCaller,
)
}
redisClient := goredis.NewClient(&goredis.Options{
Addr: "localhost:6379",
Password: "", // no password set
DB: 0, // use default DB
})
var (
todos = inmem.NewTodoRepository()
users = inmem.NewUserRepository()
sessions = redis.NewSessionRepository(*redisClient)
)
fieldKeys := []string{"method"}
var todoService todo.Service
{
todoService = todo.NewService(todos)
todoService = todo.NewLoggingService(logger, todoService)
todoService = todo.NewInstrumentingService(
kitprometheus.NewCounterFrom(stdprometheus.CounterOpts{
Namespace: "api",
Subsystem: "todo_service",
Name: "request_count",
Help: "Number of requests received.",
}, fieldKeys),
kitprometheus.NewSummaryFrom(stdprometheus.SummaryOpts{
Namespace: "api",
Subsystem: "todo_service",
Name: "request_latency_microseconds",
Help: "Total duration of requests in microseconds.",
}, fieldKeys),
todoService,
)
}
var userService user.Service
{
userService = user.NewService(users)
userService = user.NewLoggingService(logger, userService)
userService = user.NewInstrumentingService(
kitprometheus.NewCounterFrom(stdprometheus.CounterOpts{
Namespace: "api",
Subsystem: "user_service",
Name: "request_count",
Help: "Number of requests received.",
}, fieldKeys),
kitprometheus.NewSummaryFrom(stdprometheus.SummaryOpts{
Namespace: "api",
Subsystem: "user_service",
Name: "request_latency_microseconds",
Help: "Total duration of requests in microseconds.",
}, fieldKeys),
userService,
)
}
var sessionService session.Service
{
sessionService = session.NewService(sessions)
}
_ = sessionService
var authService auth.Service
{
authService = auth.NewService(sessionService, userService)
}
var gqls graphql.Service
{
root := models.Root{TodoService: todoService, UserService: userService}
schema.Init(schema.ProviderConfig{
Query: root,
Mutation: root,
})
gqls = graphql.NewService(schema.Schema())
gqls = graphql.NewLoggingService(logger, gqls)
gqls = graphql.NewInstrumentingService(
kitprometheus.NewCounterFrom(stdprometheus.CounterOpts{
Namespace: "api",
Subsystem: "graphql_service",
Name: "request_count",
Help: "Number of requests received.",
}, fieldKeys),
kitprometheus.NewSummaryFrom(stdprometheus.SummaryOpts{
Namespace: "api",
Subsystem: "graphql_service",
Name: "request_latency_microseconds",
Help: "Total duration of requests in microseconds.",
}, fieldKeys),
gqls,
)
}
httpLogger := log.With(logger, "component", "http")
mux := http.NewServeMux()
mux.Handle("/auth", auth.MakeHandler(authService, httpLogger))
mux.Handle("/graphql", graphql.MakeHandler(gqls, httpLogger,
kithttp.ServerBefore(authService.Authenticate)))
mux.Handle("/", http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
rw.Write(graphiql)
}))
http.Handle("/", accessControl(mux))
http.Handle("/metrics", stdprometheus.Handler())
errc := make(chan error, 2)
go func() {
c := make(chan os.Signal)
signal.Notify(c, syscall.SIGINT)
errc <- fmt.Errorf("%s", <-c)
}()
// TODO(nicolai): narqo debug listener
go func() {
logger.Log("transport", "http", "address", httpAddr, "msg", "listening")
errc <- http.ListenAndServe(*httpAddr, nil)
}()
logger.Log("terminated", <-errc)
}
func accessControl(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Methods", "GET, POST, OPTIONS")
w.Header().Set("Access-Control-Allow-Headers", "Origin, Content-Type")
if r.Method == "OPTIONS" {
return
}
h.ServeHTTP(w, r)
})
}
func envString(varName, fallback string) string {
value := os.Getenv(varName)
if value == "" {
return fallback
}
return value
}
type serializedLogger struct {
mtx sync.Mutex
log.Logger
}
func (l *serializedLogger) Log(keyvals ...interface{}) error {
l.mtx.Lock()
defer l.mtx.Unlock()
return l.Logger.Log(keyvals...)
}
var graphiql = []byte(`
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/graphiql/0.11.2/graphiql.css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/fetch/1.1.0/fetch.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.6.1/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.6.1/react-dom.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/graphiql/0.11.2/graphiql.js"></script>
</head>
<body style="width: 100%; height: 100%; margin: 0; overflow: hidden;">
<div id="graphiql" style="height: 100vh;">Loading...</div>
<script>
function graphQLFetcher(graphQLParams) {
graphQLParams.variables = graphQLParams.variables ? JSON.parse(graphQLParams.variables) : null;
return fetch("/graphql", {
method: "post",
body: JSON.stringify(graphQLParams),
credentials: "include",
}).then(function (response) {
return response.text();
}).then(function (responseBody) {
try {
return JSON.parse(responseBody);
} catch (error) {
return responseBody;
}
});
}
ReactDOM.render(
React.createElement(GraphiQL, {fetcher: graphQLFetcher}),
document.getElementById("graphiql")
);
</script>
</body>
</html>
`)