-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
316 lines (274 loc) · 9.04 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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
// neuprint API
//
// REST interface for neuPrint. To test out the interface, copy your token
// under your acocunt information. Then authorize Swagger by typing "Bearer " and
// pasting the token.
//
// Version: 0.1.0
// Contact: Neuprint Team<[email protected]>
//
// SecurityDefinitions:
// Bearer:
// type: apiKey
// name: Authorization
// in: header
// scopes:
// admin: Admin scope
// user: User scope
// Security:
// - Bearer:
//
// swagger:meta
//
//go:generate swagger generate spec -o ./swaggerdocs/swagger.yaml
package main
import (
"flag"
"fmt"
"net/http"
"os"
"os/signal"
"strconv"
"strings"
"syscall"
"github.com/connectome-neuprint/neuPrintHTTP/api"
"github.com/connectome-neuprint/neuPrintHTTP/config"
"github.com/connectome-neuprint/neuPrintHTTP/logging"
"github.com/connectome-neuprint/neuPrintHTTP/storage"
secure "github.com/janelia-flyem/echo-secure"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
)
func customUsage() {
fmt.Printf("Usage: %s [OPTIONS] CONFIG.json\n", os.Args[0])
flag.PrintDefaults()
}
func neuprintLogo() {
fmt.Println(" ")
fmt.Println(" ooooooooo. o8o . ")
fmt.Println(" `888 `Y88. `\"' .o8 ")
fmt.Println(" ooo. .oo. .ooooo. oooo oooo 888 .d88' oooo d8b oooo ooo. .oo. .o888oo ")
fmt.Println(" `888P\"Y88b d88' `88b `888 `888 888ooo88P' `888\"\"8P `888 `888P\"Y88b 888 ")
fmt.Println(" 888 888 888ooo888 888 888 888 888 888 888 888 888 ")
fmt.Println(" 888 888 888 .o 888 888 888 888 888 888 888 888 . ")
fmt.Println(" o888o o888o `Y8bod8P' `V88V\"V8P' o888o d888b o888o o888o o888o \"888\" ")
fmt.Println(" ")
fmt.Println("neuPrintHTTP v1.6.7")
}
func main() {
// create command line argument for port
var port = 11000
var proxyport = 0
var publicRead = false
var pidfile = ""
flag.Usage = customUsage
flag.IntVar(&port, "port", 11000, "port to start server")
flag.IntVar(&proxyport, "proxy-port", 0, "proxy port to start server")
flag.StringVar(&pidfile, "pid-file", "", "file for pid")
flag.BoolVar(&publicRead, "public_read", false, "allow all users read access")
flag.BoolVar(&storage.Verbose, "verbose", false, "verbose mode")
flag.Parse()
if flag.NArg() != 1 {
flag.Usage()
return
}
// parse options
options, err := config.LoadConfig(flag.Args()[0])
if err != nil {
fmt.Print(err)
return
}
if pidfile != "" {
pid := os.Getpid()
// Open file using READ & WRITE permission.
fout, err := os.OpenFile(pidfile, os.O_WRONLY|os.O_CREATE, 0755)
if err != nil {
fmt.Println(err)
return
}
stopSig := make(chan os.Signal)
go func() {
for range stopSig {
os.Remove(pidfile)
os.Exit(0)
}
}()
signal.Notify(stopSig, os.Interrupt, os.Kill, syscall.SIGTERM)
// Write some text line-by-line to file.
_, err = fout.WriteString(strconv.Itoa(pid))
if err != nil {
fmt.Println(err)
fout.Close()
return
}
fout.Close()
}
// create datastore based on configuration
store, err := config.CreateStore(options)
if err != nil {
fmt.Println(err)
return
}
// create echo web framework
e := echo.New()
// setup logger
logger, err := logging.GetLogger(port, options)
e.Use(logging.LoggerWithConfig(logging.LoggerConfig{
Format: "{\"dataset\": \"${dataset}\", \"uri\": \"${uri}\", \"status\": ${status}, \"bytes_in\": ${bytes_in}, \"bytes_out\": ${bytes_out}, \"duration\": ${latency}, \"time\": ${time_unix}, \"user\": \"${custom:email}\", \"category\": \"${category}\", \"debug\": \"${custom:debug}\"}\n",
Output: logger,
}))
e.Use(middleware.Recover())
e.Pre(middleware.NonWWWRedirect())
if options.DisableAuth {
e.GET("/", func(c echo.Context) error {
return c.HTML(http.StatusOK, "<html><title>neuprint http</title><body><a href='/token'><button>Download API Token</button></a><p><b>Example query using neo4j cypher:</b><br>curl -X GET -H \"Content-Type: application/json\" http://SERVERADDR/api/custom/custom -d '{\"cypher\": \"MATCH (m :Meta) RETURN m.dataset AS dataset, m.lastDatabaseEdit AS lastmod\"}'</p><a href='/api/help'>Documentation</a><form action='/logout' method='post'><input type='submit' value='Logout' /></form></body></html>")
})
// swagger:operation GET /api/help/swagger.yaml apimeta helpyaml
//
// swagger REST documentation
//
// YAML file containing swagger API documentation
//
// ---
// responses:
// 200:
// description: "successful operation"
if options.SwaggerDir != "" {
e.Static("/api/help", options.SwaggerDir)
}
readGrp := e.Group("/api")
portstr := strconv.Itoa(port)
// load connectomic default READ-ONLY API
if err = api.SetupRoutes(e, readGrp, store, func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
return next(c)
}
}); err != nil {
fmt.Print(err)
return
}
// print logo
neuprintLogo()
// start server
e.Logger.Fatal(e.Start(":" + portstr))
return
}
secure.ProxyPort = proxyport
var authorizer secure.Authorizer
// call new secure API and set authorization method
if options.AuthDatastore != "" {
authorizer, err = secure.NewDatastoreAuthorizer(options.AuthDatastore, options.AuthToken)
if err != nil {
fmt.Println(err)
return
}
} else {
authorizer, err = secure.NewFileAuthorizer(options.AuthFile)
if err != nil {
fmt.Println(err)
return
}
}
sconfig := secure.SecureConfig{
SSLCert: options.CertPEM,
SSLKey: options.KeyPEM,
ClientID: options.ClientID,
ClientSecret: options.ClientSecret,
AuthorizeChecker: authorizer,
Hostname: options.Hostname,
ProxyAuth: options.ProxyAuth,
ProxyInsecure: options.ProxyInsecure,
}
secureAPI, err := secure.InitializeEchoSecure(e, sconfig, []byte(options.Secret), "neuPrintHTTP")
if err != nil {
fmt.Println(err)
return
}
// create read only group
readGrp := e.Group("/api")
if publicRead {
readGrp.Use(secureAPI.AuthMiddleware(secure.NOAUTH))
} else {
readGrp.Use(secureAPI.AuthMiddleware(secure.READ))
}
// setup server status message to show if it is public
// swagger:operation GET /api/serverinfo apimeta serverinfo
//
// Returns whether the server is public
//
// If it is public, no authorization is required
//
// ---
// responses:
// 200:
// description: "successful operation"
e.GET("/api/serverinfo", secureAPI.AuthMiddleware(secure.NOAUTH)(func(c echo.Context) error {
info := struct {
IsPublic bool
}{publicRead}
return c.JSON(http.StatusOK, info)
}))
e.GET("/api/vimoserver", secureAPI.AuthMiddleware(secure.NOAUTH)(func(c echo.Context) error {
info := struct {
Url string
}{options.VimoServer}
return c.JSON(http.StatusOK, info)
}))
// setup default page
if options.StaticDir != "" {
e.Static("/", options.StaticDir)
customHTTPErrorHandler := func(err error, c echo.Context) {
if he, ok := err.(*echo.HTTPError); ok {
req := c.Request()
if !strings.HasPrefix(req.RequestURI, "/api") && (he.Code == http.StatusNotFound) {
c.File(options.StaticDir)
}
}
e.DefaultHTTPErrorHandler(err, c)
}
e.HTTPErrorHandler = customHTTPErrorHandler
} else {
e.GET("/", secureAPI.AuthMiddleware(secure.NOAUTH)(func(c echo.Context) error {
return c.HTML(http.StatusOK, "<html><title>neuprint http</title><body><a href='/token'><button>Download API Token</button></a><p><b>Example query using neo4j cypher:</b><br>curl -X GET -H \"Content-Type: application/json\" -H \"Authorization: Bearer YOURTOKEN\" https://SERVERADDR/api/custom/custom -d '{\"cypher\": \"MATCH (m :Meta) RETURN m.dataset AS dataset, m.lastDatabaseEdit AS lastmod\"}'</p><a href='/api/help'>Documentation</a><form action='/logout' method='post'><input type='submit' value='Logout' /></form></body></html>")
}))
}
// swagger:operation GET /api/help/swagger.yaml apimeta helpyaml
//
// swagger REST documentation
//
// YAML file containing swagger API documentation
//
// ---
// responses:
// 200:
// description: "successful operation"
if options.SwaggerDir != "" {
e.Static("/api/help", options.SwaggerDir)
}
// swagger:operation GET /api/npexplorer/nglayers
//
// layer settings for neuroglancer view
//
// JSON files containing neuroglancer layer settings per dataset
//
// ---
// responses:
// 200:
// description: "successful operation"
if options.NgDir != "" {
e.Static("/api/npexplorer/nglayers", options.NgDir)
}
// load connectomic default READ-ONLY API
if err = api.SetupRoutes(e, readGrp, store, secureAPI.AuthMiddleware(secure.ADMIN)); err != nil {
fmt.Print(err)
return
}
// print logo
neuprintLogo()
// if log file selected print location of logs
if options.LoggerFile != "" {
fmt.Printf("logging to file: %s", options.LoggerFile)
}
// start server
secureAPI.StartEchoSecure(port)
}