-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
conf.go
139 lines (118 loc) · 3.46 KB
/
conf.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package conf
import (
"fmt"
"log"
"os"
"path/filepath"
"runtime"
"strconv"
"strings"
"github.com/morikuni/failure"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
// Variables of configure.
var Variables c
type c struct {
CGIMode bool
HTTPPort string
Salt string
SSHKeySize int
SSHKeyFilename string
SSHPubKeyFilename string
RepositoriesDirectory string
KeyDirectoryRelatedFromRepository string
HTTPHeaderKey string
AccessLog bool
logger *zap.Logger
}
const prefix = "ONSTATIC_"
// Init configuration variales.
func Init() {
Variables = c{
CGIMode: getenvBool("CGI_MODE", false),
HTTPPort: getenv("HTTP_PORT", "18888"),
Salt: getenv("SALT", "saltsaltsalt"),
SSHKeySize: getenvInt("SSH_KEY_SIZE", 4096),
SSHKeyFilename: getenv("SSH_KEY_FILENAME", "id_ed25519"),
SSHPubKeyFilename: getenv("SSH_PUB_KEY_FILENAME", "id_ed25519.pub"),
RepositoriesDirectory: getenv("REPOSITORIES_DIRECTORY", "repositories/"),
KeyDirectoryRelatedFromRepository: getenv("KEY_DIRECTORY_RELATED_FROM_REPOSITORY", "."),
HTTPHeaderKey: getenv("HTTP_HEADER_KEY", "onstaticonstaticonstatic"),
AccessLog: getenvBool("ACCESS_LOG", true),
logger: logger(
zapcore.DebugLevel, // for plugin logging
getenv("STDLOG_OUTPUT_PATH", "stdout"), // "/var/log/onstatic/stdout.log"),
getenv("ERRLOG_OUTPUT_PATH", "stderr"), // "/var/log/onstatic/stderr.log"),
),
}
log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)
}
func getenv(k string, d string) string {
s := strings.TrimSpace(os.Getenv(prefix + k))
if s == "" {
return d
}
return s
}
func getenvInt(k string, d int) int {
n, e := strconv.Atoi(getenv(k, ""))
if e != nil {
return d
}
return n
}
func getenvBool(k string, d bool) bool {
n, e := strconv.ParseBool((getenv(k, "")))
if e != nil {
return d
}
return n
}
func logger(logLevel zapcore.Level, logOutputPath string, logErrorPath string) *zap.Logger {
zapConfig := zap.NewProductionConfig()
zapConfig.Level = zap.NewAtomicLevelAt(logLevel)
zapConfig.DisableStacktrace = true
zapConfig.OutputPaths = []string{logOutputPath}
zapConfig.ErrorOutputPaths = []string{logErrorPath}
l, err := zapConfig.Build()
if err != nil {
if strings.Contains(fmt.Sprint(err), "no such file or directory") {
if err := createLogFileIfEmpty(logOutputPath); err != nil {
log.Fatal(err)
}
if err := createLogFileIfEmpty(logErrorPath); err != nil {
log.Fatal(err)
}
return logger(logLevel, logOutputPath, logErrorPath)
}
log.Fatalf("failed to initialize logger: %+v", err)
}
zap.ReplaceGlobals(l)
return l
}
func createLogFileIfEmpty(p string) error {
if _, err := os.Stat(p); err == nil {
return nil
}
dir := filepath.Dir(p)
if _, err := os.Stat(p); err != nil {
if err := os.MkdirAll(dir, os.ModePerm); err != nil {
return failure.Wrap(err)
}
}
f, err := os.OpenFile(p, os.O_CREATE, os.ModePerm)
f.Close()
if err != nil {
return failure.Wrap(err)
}
return nil
}
func getCurrentGoFilePath() string {
_, file, _, _ := runtime.Caller(1)
p, err := filepath.Abs(file)
if err != nil {
return ""
}
return p
}