Skip to content

Commit 63e5946

Browse files
committed
Move read graph handler into the handlers module.
1 parent 1731405 commit 63e5946

File tree

2 files changed

+46
-36
lines changed

2 files changed

+46
-36
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package handlers
2+
3+
import (
4+
"encoding/json"
5+
"net/http"
6+
7+
"github.com/clems4ever/go-graphkb/internal/knowledge"
8+
"github.com/clems4ever/go-graphkb/internal/sources"
9+
)
10+
11+
// GetGraphRead GET the entire graph generated by the data source
12+
func GetGraphRead(registry sources.Registry, graphDB knowledge.GraphDB) http.HandlerFunc {
13+
return func(w http.ResponseWriter, r *http.Request) {
14+
ok, source, err := IsTokenValid(registry, r)
15+
if err != nil {
16+
ReplyWithInternalError(w, err)
17+
return
18+
}
19+
20+
if !ok {
21+
ReplyWithUnauthorized(w)
22+
return
23+
}
24+
25+
g := knowledge.NewGraph()
26+
if err := graphDB.ReadGraph(r.Context(), source, g); err != nil {
27+
ReplyWithInternalError(w, err)
28+
return
29+
}
30+
31+
gJSON, err := json.Marshal(g)
32+
if err != nil {
33+
ReplyWithInternalError(w, err)
34+
return
35+
}
36+
37+
if _, err := w.Write(gJSON); err != nil {
38+
ReplyWithInternalError(w, err)
39+
}
40+
}
41+
}

internal/server/server.go

Lines changed: 5 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -117,37 +117,6 @@ func getDatabaseDetails(database knowledge.GraphDB) http.HandlerFunc {
117117
}
118118
}
119119

120-
func getGraphRead(registry sources.Registry, graphDB knowledge.GraphDB) http.HandlerFunc {
121-
return func(w http.ResponseWriter, r *http.Request) {
122-
ok, source, err := handlers.IsTokenValid(registry, r)
123-
if err != nil {
124-
handlers.ReplyWithInternalError(w, err)
125-
return
126-
}
127-
128-
if !ok {
129-
handlers.ReplyWithUnauthorized(w)
130-
return
131-
}
132-
133-
g := knowledge.NewGraph()
134-
if err := graphDB.ReadGraph(r.Context(), source, g); err != nil {
135-
handlers.ReplyWithInternalError(w, err)
136-
return
137-
}
138-
139-
gJSON, err := json.Marshal(g)
140-
if err != nil {
141-
handlers.ReplyWithInternalError(w, err)
142-
return
143-
}
144-
145-
if _, err := w.Write(gJSON); err != nil {
146-
handlers.ReplyWithInternalError(w, err)
147-
}
148-
}
149-
}
150-
151120
func flushDatabase(graphDB knowledge.GraphDB) http.HandlerFunc {
152121
return func(w http.ResponseWriter, r *http.Request) {
153122
if err := graphDB.FlushAll(r.Context()); err != nil {
@@ -176,7 +145,7 @@ func StartServer(listenInterface string,
176145
schemaPersistor schema.Persistor,
177146
sourcesRegistry sources.Registry,
178147
queryHistorizer history.Historizer,
179-
concurrency int64) {
148+
writeConcurrency int64) {
180149

181150
r := mux.NewRouter()
182151

@@ -213,9 +182,9 @@ func StartServer(listenInterface string,
213182

214183
r.Handle("/metrics", promhttp.Handler())
215184

216-
r.HandleFunc("/api/graph/read", getGraphRead(sourcesRegistry, database)).Methods("GET")
185+
r.HandleFunc("/api/graph/read", handlers.GetGraphRead(sourcesRegistry, database)).Methods("GET")
217186

218-
sem := semaphore.NewWeighted(concurrency)
187+
sem := semaphore.NewWeighted(writeConcurrency)
219188

220189
r.HandleFunc("/api/graph/schema", handlers.PutSchema(sourcesRegistry, graphUpdater, sem)).Methods("PUT")
221190
r.HandleFunc("/api/graph/assets", handlers.PutAssets(sourcesRegistry, graphUpdater, sem)).Methods("PUT")
@@ -230,12 +199,12 @@ func StartServer(listenInterface string,
230199

231200
var err error
232201
if viper.GetString("server_tls_cert") != "" {
233-
logrus.Infof("Listening on %s with TLS enabled, the connection is secure [concurrency=%d", listenInterface, concurrency)
202+
logrus.Infof("Listening on %s with TLS enabled, the connection is secure [concurrency=%d", listenInterface, writeConcurrency)
234203
err = http.ListenAndServeTLS(listenInterface, viper.GetString("server_tls_cert"),
235204
viper.GetString("server_tls_key"), r)
236205
} else {
237206
logrus.Warnf("Listening on %s with TLS disabled. Use `server_tls_cert` option to setup a certificate [concurrency=%d]",
238-
listenInterface, concurrency)
207+
listenInterface, writeConcurrency)
239208
err = http.ListenAndServe(listenInterface, r)
240209
}
241210
if err != nil {

0 commit comments

Comments
 (0)