-
Notifications
You must be signed in to change notification settings - Fork 7
/
config.go
102 lines (82 loc) · 2.18 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
/*
file created by Junlin Chen in 2022
*/
package proxy
import (
"encoding/json"
"github.com/google/uuid"
"github.com/mc256/starlight/util"
"github.com/pkg/errors"
"io/ioutil"
"os"
"path"
)
type Configuration struct {
// server
ListenPort int `json:"port"`
ListenAddress string `json:"address"`
LogLevel string `json:"log_level"`
// database
PostgresConnectionString string `json:"postgres"`
// registry
DefaultRegistry string `json:"default_registry"`
// goharbor hook
EnableHarborScanner bool `json:"harbor"`
HarborApiKey string `json:"harbor_apikey"`
// layer cache timeout (second)
CacheTimeout int `json:"cache_timeout"`
}
func LoadConfig(cfgPath string) (c *Configuration, p string, n bool, error error) {
c = NewConfig()
if cfgPath == "" {
etcPath := util.GetEtcConfigPath()
if err := os.MkdirAll(etcPath, 0775); err != nil {
error = errors.Wrapf(err, "cannot create config folder")
return
}
p = path.Join(etcPath, "proxy_config.json")
} else {
p = cfgPath
}
b, err := ioutil.ReadFile(p)
n = false
if err != nil {
n = true
buf, _ := json.Marshal(c)
if err = ioutil.WriteFile(p, buf, 0644); err == nil {
return
} else {
error = errors.Wrapf(err, "cannot create config file")
}
}
if err = json.Unmarshal(b, c); err != nil {
error = errors.Wrapf(err, "cannot parse config file")
}
return
}
func (c *Configuration) SaveConfig() error {
etcPath := util.GetEtcConfigPath()
if err := os.MkdirAll(etcPath, 0775); err != nil {
return errors.Wrapf(err, "cannot create config folder")
}
p := path.Join(etcPath, "proxy_config.json")
buf, _ := json.MarshalIndent(c, " ", " ")
err := ioutil.WriteFile(p, buf, 0644)
if err == nil {
return nil
}
return nil
}
func NewConfig() *Configuration {
uuid.EnableRandPool()
return &Configuration{
ListenPort: 8090,
ListenAddress: "0.0.0.0",
LogLevel: "info",
PostgresConnectionString: "postgres://postgres:postgres@localhost:5432/postgres?sslmode=disable",
DefaultRegistry: "127.0.0.1:9000",
EnableHarborScanner: false,
HarborApiKey: uuid.New().String(),
CacheTimeout: 3600,
}
}