/
config.go
190 lines (183 loc) · 4.06 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
// Copyright 2019 Cytown. All rights reserved.
// Use of this source code is governed by a MIT style
// license that can be found in the LICENSE file.
package gintool
import (
"fmt"
"net/http"
"os"
"github.com/gin-gonic/gin"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"gopkg.in/yaml.v2"
)
// Config the configuration
type Config struct {
address string
mode string
statics map[string]string
staticFs map[string]string
errors map[int]string
templates string
logfile string
errorlog string
certFile string
keyFile string
stdlog zerolog.Logger
errlog zerolog.Logger
other interface{}
}
func initConfig() *Config {
return &Config{
errors: map[int]string{},
statics: map[string]string{},
staticFs: map[string]string{},
stdlog: log.Output(os.Stdout),
errlog: log.Output(os.Stderr),
}
}
func parseFile(path string) (*Config, error) {
err := isFile(path)
if err != nil {
return nil, err
}
buf, err := os.ReadFile(path)
if err != nil {
return nil, err
}
//fmt.Println(len(buf))
var out interface{}
err = yaml.Unmarshal(buf, &out)
if err != nil {
return nil, err
}
c := initConfig()
//fmt.Println("unmarshal: ", out)
m, err := extract(out, "gin")
if err != nil {
return nil, err
}
mm, err := extract(m, "address")
if err == nil {
c.address = mm.(string)
//fmt.Println("address:", mm)
}
mm, err = extract(m, "other")
if err == nil {
c.other = mm
//fmt.Println("address:", mm)
}
mm, err = extract(m, "log")
if err == nil {
c.logfile = mm.(string)
//fmt.Println("logfile:", mm)
}
mm, err = extract(m, "errorlog")
if err == nil {
c.errorlog = mm.(string)
//fmt.Println("errorlog:", mm)
}
mm, err = extract(m, "mode")
if err == nil {
c.mode = mm.(string)
if len(c.mode) != 0 {
gin.SetMode(c.mode)
}
fmt.Println("mode:", mm)
}
mm, err = extract(m, "tls", "certfile")
if err == nil {
ss, ok := mm.(string)
if ok && ss != "" {
err := isFile(ss)
if err != nil {
return nil, err
}
c.certFile = ss
}
}
mm, err = extract(m, "tls", "keyfile")
if err == nil {
ss, ok := mm.(string)
if ok && ss != "" {
err := isFile(ss)
if err != nil {
return nil, err
}
c.keyFile = ss
}
}
if c.certFile == "" || c.keyFile == "" {
c.certFile = ""
c.keyFile = ""
//} else {
//fmt.Println("certfile", c.certFile)
//fmt.Println("keyfile", c.keyFile)
}
mm, err = extract(m, "templates")
if err == nil {
ss, ok := mm.(string)
if ok && ss != "" {
err := isDir(ss)
if err != nil {
return nil, err
}
c.templates = ss
}
}
mm, err = extract(m, "static")
if err == nil {
//c.statics = mm.(string)
//fmt.Println("statics:", mm)
ss, ok := mm.([]interface{})
if !ok {
return nil, fmt.Errorf("wrong type statics")
}
static := make(map[string]string)
for _, s := range ss {
//fmt.Println("statics", s)
mapping, ok := s.(map[interface{}]interface{})
if !ok {
return nil, fmt.Errorf("wrong type statics mapping %v", s)
}
static[mapping["map"].(string)] = mapping["path"].(string)
}
c.statics = static
//fmt.Println("statics", c.statics)
}
mm, err = extract(m, "staticfile")
if err == nil {
ss, ok := mm.([]interface{})
if !ok {
return nil, fmt.Errorf("wrong type staticfile")
}
static := map[string]string{}
for _, s := range ss {
//fmt.Println("statics", s)
mapping, ok := s.(map[interface{}]interface{})
if !ok {
return nil, fmt.Errorf("wrong type staticfile mapping %v", s)
}
static[mapping["map"].(string)] = mapping["file"].(string)
}
c.staticFs = static
//fmt.Println("staticfile", c.staticFs)
}
mm, err = extract(m, "error", "404")
//fmt.Println("err", err)
if err == nil {
c.errors[http.StatusNotFound] = mm.(string)
//fmt.Println("404:", mm)
}
mm, err = extract(m, "error", "500")
if err == nil {
c.errors[http.StatusInternalServerError] = mm.(string)
//fmt.Println("500:", mm)
}
return c, nil
}
// Get return the saved other configuration with key/value mapping
func (c *Config) Get(key ...string) (ret interface{}) {
ret, _ = extract(c.other, key...)
return
}