-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfig.go
More file actions
101 lines (83 loc) · 2.65 KB
/
Copy pathConfig.go
File metadata and controls
101 lines (83 loc) · 2.65 KB
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
package main
import (
"encoding/json"
"flag"
"fmt"
"os"
"gopkg.in/yaml.v2"
)
type Config struct {
ConfigFile string `yaml:"-"`
Domain string `yaml:"domain,omitempty"`
Port int `yaml:"port,omitempty"`
Https bool `yaml:"https,omitempty"`
AllowedOrigins []string `yaml:"allowed_origins,omitempty"` // CORS allowed origins
Log struct {
Datetime bool `yaml:"datetime,omitempty"`
SrcFile bool `yaml:"srcfile,omitempty"`
Info bool `yaml:"info,omitempty"`
Warning bool `yaml:"warning,omitempty"`
Error bool `yaml:"error,omitempty"`
Trace bool `yaml:"trace,omitempty"`
Debug bool `yaml:"debug,omitempty"`
} `yaml:"log,omitempty"`
}
func initFlag() {
flag.StringVar(&conf.ConfigFile, "config", "config.yaml", "config file")
flag.IntVar(&conf.Port, "port", 8090, "port")
flag.BoolVar(&conf.Https, "https", true, "https mode")
flag.BoolVar(&conf.Log.Datetime, "log-datetime", false, "log datetime enable")
flag.BoolVar(&conf.Log.SrcFile, "log-srcfile", true, "log source file enable")
flag.BoolVar(&conf.Log.Info, "log-info", true, "log info enable")
flag.BoolVar(&conf.Log.Warning, "log-warning", true, "log warning enable")
flag.BoolVar(&conf.Log.Error, "log-error", true, "log error enable")
flag.BoolVar(&conf.Log.Trace, "log-trace", true, "log trace enable")
flag.BoolVar(&conf.Log.Debug, "log-debug", false, "log debug enable")
flag.Usage = usage
}
func (c *Config) Read(fn string) error {
buf, err := os.ReadFile(fn)
if err != nil {
return fmt.Errorf("cannot read config %s: %v", fn, err)
}
err = yaml.Unmarshal(buf, c)
if err != nil {
return fmt.Errorf("cannot unmarshal config %s: %v", fn, err)
}
return nil
}
func (c *Config) makePretty() string {
buf, err := json.MarshalIndent(c, "", " ")
if err != nil {
fmt.Println(err.Error())
}
return string(buf)
}
func (c *Config) checkRequired() error {
if c.Domain == "" {
c.Domain = "localhost"
}
if c.Port == 0 {
c.Port = 8090
}
// AllowedOrigins가 비어있으면 "*"(모든 출처 허용)을 의미
// 프로덕션에서는 config.yaml에 명시적으로 허용할 출처를 지정해야 함
// 예: ["http://yourdomain.com", "https://yourdomain.com"]
if len(c.AllowedOrigins) == 0 {
// 개발 환경: 모든 출처 허용 (편의성)
c.AllowedOrigins = []string{"*"}
}
return nil
}
func initConf() error {
err := conf.Read(conf.ConfigFile)
if err != nil {
// config 파일이 없으면 기본값 사용
fmt.Printf("Warning: cannot read config file: %v (using defaults)\n", err)
}
err = conf.checkRequired()
if err != nil {
return fmt.Errorf("config check failed: %v", err)
}
return nil
}