forked from CHESSComputing/MetaData
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
87 lines (74 loc) · 2.89 KB
/
server.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
package main
import (
"embed"
"log"
"time"
srvConfig "github.com/CHESSComputing/golib/config"
lexicon "github.com/CHESSComputing/golib/lexicon"
mongo "github.com/CHESSComputing/golib/mongo"
server "github.com/CHESSComputing/golib/server"
utils "github.com/CHESSComputing/golib/utils"
"github.com/gin-gonic/gin"
)
// content is our static web server content.
//
//go:embed static
var StaticFs embed.FS
// Verbose defines verbosity level
var Verbose int
// global variables
var _beamlines []string
var _smgr SchemaManager
// helper function to setup our router
func setupRouter() *gin.Engine {
routes := []server.Route{
server.Route{Method: "GET", Path: "/meta", Handler: MetaDetailsHandler, Authorized: false},
server.Route{Method: "GET", Path: "/record", Handler: RecordHandler, Authorized: false},
server.Route{Method: "PUT", Path: "/", Handler: DataHandler, Authorized: true, Scope: "write"},
server.Route{Method: "POST", Path: "/", Handler: DataHandler, Authorized: true, Scope: "write"},
server.Route{Method: "POST", Path: "/search", Handler: QueryHandler, Authorized: true},
server.Route{Method: "POST", Path: "/summary", Handler: SummaryHandler, Authorized: false},
server.Route{Method: "POST", Path: "/count", Handler: QueryCountHandler, Authorized: true},
server.Route{Method: "DELETE", Path: "/record", Handler: DeleteHandler, Authorized: true, Scope: "delete"},
}
r := server.Router(routes, nil, "static", srvConfig.Config.CHESSMetaData.WebServer)
return r
}
// Server defines our HTTP server
func Server() {
// init MongoDB
log.Println("init mongo", srvConfig.Config.CHESSMetaData.MongoDB.DBUri)
mongo.InitMongoDB(srvConfig.Config.CHESSMetaData.MongoDB.DBUri)
// init Verbose
Verbose = srvConfig.Config.CHESSMetaData.WebServer.Verbose
if srvConfig.Config.CHESSMetaData.SchemaRenewInterval == 0 {
SchemaRenewInterval = time.Duration(1 * 60 * 60 * time.Second) // by default renew every 1 hour
} else {
SchemaRenewInterval = time.Duration(srvConfig.Config.CHESSMetaData.SchemaRenewInterval) * time.Second
}
// initialize schema manager
_smgr = SchemaManager{}
for _, fname := range srvConfig.Config.CHESSMetaData.SchemaFiles {
_, err := _smgr.Load(fname)
if err != nil {
log.Fatalf("unable to load %s error %v", fname, err)
}
_beamlines = append(_beamlines, utils.FileName(fname))
}
log.Println("Schema", _smgr.String())
// load Lexicon patterns
lexPatterns, err := lexicon.LoadPatterns(srvConfig.Config.CHESSMetaData.LexiconFile)
if err != nil {
log.Fatal(err)
}
lexicon.LexiconPatterns = lexPatterns
_skipKeys = srvConfig.Config.CHESSMetaData.SkipKeys
if len(_skipKeys) == 0 {
// default list
_skipKeys = []string{"user", "date", "description", "schema_name", "schema_file", "schema", "did", "doi", "doi_url"}
}
// setup web router and start the service
r := setupRouter()
webServer := srvConfig.Config.CHESSMetaData.WebServer
server.StartServer(r, webServer)
}