/
handlerAdmin.go
84 lines (71 loc) · 3.16 KB
/
handlerAdmin.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
// Copyright (c) 2016 OpenM++
// This code is licensed under the MIT license (see LICENSE.txt for details)
package main
import (
"net/http"
"github.com/openmpp/go/ompp/omppLog"
)
// serviceConfigHandler return service configuration, including configuration of model catalog and run catalog.
// GET /api/service/config
func serviceConfigHandler(w http.ResponseWriter, r *http.Request) {
st := struct {
RootDir string // server root directory
RowPageMaxSize int64 // default "page" size: row count to read parameters or output tables
RunHistoryMaxSize int // max number of model run states to keep in run list history
DoubleFmt string // format to convert float or double value to string
LoginUrl string // user login URL for UI
LogoutUrl string // user logout URL for UI
Env map[string]string // server config environmemt variables
ModelCatalog ModelCatalogConfig // "public" state of model catalog
RunCatalog RunCatalogConfig // "public" state of model run catalog
}{
RootDir: theCfg.rootDir,
RowPageMaxSize: theCfg.pageMaxSize,
RunHistoryMaxSize: theCfg.runHistoryMaxSize,
DoubleFmt: theCfg.doubleFmt,
Env: theCfg.env,
ModelCatalog: *theCatalog.toPublicConfig(),
RunCatalog: *theRunCatalog.toPublicConfig(),
}
jsonResponse(w, r, st)
}
// allModelsRefreshHandler reload models catalog: rescan models directory tree and reload model.sqlite.
// POST /api/admin/all-models/refresh
func allModelsRefreshHandler(w http.ResponseWriter, r *http.Request) {
// model directory required to build list of model sqlite files
modelLogDir, _ := theCatalog.getModelLogDir()
modelDir, _ := theCatalog.getModelDir()
if modelDir == "" {
omppLog.Log("Failed to refersh models catalog: path to model directory cannot be empty")
http.Error(w, "Failed to refersh models catalog: path to model directory cannot be empty", http.StatusBadRequest)
return
}
omppLog.Log("Model directory: ", modelDir)
// refresh models catalog
if err := theCatalog.RefreshSqlite(modelDir, modelLogDir); err != nil {
omppLog.Log(err)
http.Error(w, "Failed to refersh models catalog: "+modelDir, http.StatusBadRequest)
return
}
// refresh run state catalog
if err := theRunCatalog.RefreshCatalog(etcDir); err != nil {
omppLog.Log(err)
http.Error(w, "Failed to refersh model runs catalog", http.StatusBadRequest)
return
}
w.Header().Set("Content-Location", "/api/admin/all-models/refresh/"+modelDir)
w.Header().Set("Content-Type", "text/plain")
}
// allModelsCloseHandler clean models catalog: close all model.sqlite connections and clean models catalog
// POST /api/admin/all-models/close
func allModelsCloseHandler(w http.ResponseWriter, r *http.Request) {
// close models catalog
modelDir, _ := theCatalog.getModelDir()
if err := theCatalog.Close(); err != nil {
omppLog.Log(err)
http.Error(w, "Failed to close models catalog: "+modelDir, http.StatusBadRequest)
return
}
w.Header().Set("Content-Location", "/api/admin/all-models/close/"+modelDir)
w.Header().Set("Content-Type", "text/plain")
}