-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
63 lines (52 loc) · 1.99 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
package main
// server module
//
// Copyright (c) 2023 - Valentin Kuznetsov <[email protected]>
//
import (
"embed"
"log"
"net/http"
srvConfig "github.com/CHESSComputing/golib/config"
mongo "github.com/CHESSComputing/golib/mongo"
server "github.com/CHESSComputing/golib/server"
services "github.com/CHESSComputing/golib/services"
"github.com/gin-gonic/gin"
)
// content is our static web server content.
//
//go:embed static
var StaticFs embed.FS
var _httpReadRequest *services.HttpRequest
var Verbose int
var StaticDir, StorageDir string
// helper function to setup our router
func setupRouter() *gin.Engine {
routes := []server.Route{
server.Route{Method: "GET", Path: "/docs/:name", Handler: DocsHandler, Authorized: false},
server.Route{Method: "GET", Path: "/models", Handler: ModelsHandler, Authorized: false},
server.Route{Method: "GET", Path: "/models/:name", Handler: DownloadHandler, Authorized: true},
server.Route{Method: "POST", Path: "/predict", Handler: PredictHandler, Authorized: true, Scope: "read"},
server.Route{Method: "POST", Path: "/upload", Handler: UploadHandler, Authorized: true, Scope: "write"},
server.Route{Method: "DELETE", Path: "/delete", Handler: DeleteHandler, Authorized: true, Scope: "delete"},
}
r := server.Router(routes, nil, "static", srvConfig.Config.MLHub.WebServer)
r.StaticFS("/bundles", http.Dir(StorageDir))
return r
}
// Server defines our HTTP server
func Server() {
Verbose = srvConfig.Config.MLHub.WebServer.Verbose
StaticDir = srvConfig.Config.MLHub.WebServer.StaticDir
StorageDir = srvConfig.Config.MLHub.ML.StorageDir
log.Println("storage dir", StorageDir)
_httpReadRequest = services.NewHttpRequest("read", Verbose)
// init MongoDB
log.Println("init mongo", srvConfig.Config.MLHub.MongoDB.DBUri)
mongo.InitMongoDB(srvConfig.Config.MLHub.MongoDB.DBUri)
// setup web router and start the service
r := setupRouter()
webServer := srvConfig.Config.MLHub.WebServer
log.Printf("### webServer %+v", webServer)
server.StartServer(r, webServer)
}