forked from CHESSComputing/CHAPaaS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
120 lines (107 loc) · 3.47 KB
/
config.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
package main
// config module
//
// Copyright (c) 2023 - Valentin Kuznetsov <[email protected]>
//
import (
"encoding/json"
"errors"
"fmt"
"log"
"os"
"path/filepath"
)
// OAuthRecord defines OAuth provider's credentials
type OAuthRecord struct {
Provider string `json:"provider"` // name of the provider
ClientID string `json:"client_id"` // client id
ClientSecret string `json:"client_secret"` // client secret
}
// Configuration stores server configuration parameters
type Configuration struct {
// web server parts
Base string `json:"base"` // base URL
LogFile string `json:"log_file"` // server log file
Port int `json:"port"` // server port number
Verbose int `json:"verbose"` // verbose output
StaticDir string `json:"static_dir"` // speficy static dir location
// OAuth parts
OAuth []OAuthRecord `json:"oauth"` // oauth configurations
// proxy parts
XForwardedHost string `json:"X-Forwarded-Host"` // X-Forwarded-Host field of HTTP request
XContentTypeOptions string `json:"X-Content-Type-Options"` // X-Content-Type-Options option
// server parts
RootCAs string `json:"rootCAs"` // server Root CAs path
ServerCrt string `json:"server_cert"` // server certificate
ServerKey string `json:"server_key"` // server certificate
DomainNames []string `json:"domain_names"` // LetsEncrypt domain names
LimiterPeriod string `json:"rate"` // limiter rate value
// storage parts
StorageDir string `json:"storage_dir"` // storage directory
// CHAP parts
ChapDir string `json:"chap_dir"` // CHAP install area
UserDir string `json:"user_dir"` // user directory
ScriptsDir string `json:"scripts_dir"` // scripts dir area
JupyterToken string `json:"jupyter_token"` // jupyter token
JupyterHost string `json:"jupyter_host"` // jupyter host:port
JupyterRoot string `json:"jupyter_root"` // jupyter root directory
}
// Credentials returns provider OAuth credential record
func (c Configuration) Credentials(provider string) (OAuthRecord, error) {
for _, rec := range c.OAuth {
if rec.Provider == provider {
return rec, nil
}
}
msg := fmt.Sprintf("No OAuth provider %s is found", provider)
return OAuthRecord{}, errors.New(msg)
}
// Config variable represents configuration object
var Config Configuration
// helper function to parse server configuration file
func parseConfig(configFile string) error {
data, err := os.ReadFile(filepath.Clean(configFile))
if err != nil {
log.Println("Unable to read", err)
return err
}
err = json.Unmarshal(data, &Config)
if err != nil {
log.Println("Unable to parse", err)
return err
}
// default values
if Config.Port == 0 {
Config.Port = 8181
}
if Config.LimiterPeriod == "" {
Config.LimiterPeriod = "100-S"
}
if Config.StaticDir == "" {
cdir, err := os.Getwd()
if err == nil {
Config.StaticDir = fmt.Sprintf("%s/static", cdir)
} else {
Config.StaticDir = "static"
}
}
if Config.StorageDir == "" {
Config.StorageDir = "/tmp"
}
if Config.JupyterHost == "" {
log.Fatal("Empty JupyterHost, please adjust your configuration")
}
if Config.UserDir == "" {
log.Fatal("Empty UserDir, please adjust your configuration")
}
if Config.ScriptsDir == "" {
Config.ScriptsDir = "scripts"
}
if Config.JupyterRoot == "" {
log.Fatal("Empty JupyterRoot, please adjust your configuration")
}
if Config.ChapDir == "" {
log.Fatal("Empty ChapDir, please adjust your configuration")
}
return nil
}