-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
config.go
71 lines (63 loc) 路 1.46 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
// Copyright 2021 Changkun Ou. All rights reserved.
// Use of this source code is governed by a MIT
// license that can be found in the LICENSE file.
package config
import (
_ "embed"
"log"
"os"
"gopkg.in/yaml.v3"
)
type config struct {
Title string `yaml:"title"`
Host string `yaml:"host"`
Addr string `yaml:"addr"`
Store string `yaml:"store"`
CORS bool `yaml:"cors"`
S struct {
Prefix string `yaml:"prefix"`
} `yaml:"s"`
R struct {
Enable bool `yaml:"enable"`
Length int `yaml:"length"`
Prefix string `yaml:"prefix"`
} `yaml:"r"`
X struct {
Enable bool `yaml:"enable"`
Prefix string `yaml:"prefix"`
VCS string `yaml:"vcs"`
ImportPath string `yaml:"import_path"`
RepoPath string `yaml:"repo_path"`
GoDocHost string `yaml:"godoc_host"`
} `yaml:"x"`
Auth struct {
Enable bool `yaml:"enable"`
Username string `yaml:"username"`
Password string `yaml:"password"`
} `yaml:"auth"`
Stats struct {
Enable bool `yaml:"enable"`
HideIP bool `yaml:"hide_ip"`
} `yaml:"stats"`
}
//go:embed config.yml
var defaultConf []byte
func (c *config) parse() {
f := os.Getenv("REDIR_CONF")
d, err := os.ReadFile(f)
if err != nil {
// Just try again with default setting.
d = defaultConf
if d == nil {
log.Fatalf("cannot read configuration: %v\n", err)
}
}
err = yaml.Unmarshal(d, c)
if err != nil {
log.Fatalf("cannot parse configuration: %v\n", err)
}
}
var Conf config
func init() {
Conf.parse()
}