forked from ponzu-cms/ponzu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
81 lines (70 loc) · 1.92 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
package config
import (
"errors"
log "github.com/sirupsen/logrus"
flag "github.com/spf13/pflag"
"github.com/spf13/viper"
"os"
)
type Paths struct {
PublicPath string
DataDir string
}
type ServeConfig struct {
HttpsPort int
HttpPort int
Bind string
DevHttps bool
Https bool
}
type Config struct {
Paths Paths
ServeConfig ServeConfig
}
func defineFlags(flagSet *flag.FlagSet, workingDir string) {
flagSet.String("public_path", "", "Optional URL path to serve ponzu on")
flagSet.String("bind", "localhost", "address for ponzu to bind the HTTP(S) server")
flagSet.Int("https_port", 443, "port for ponzu to bind its HTTPS listener")
flagSet.Int("port", 8080, "port for ponzu to bind its HTTP listener")
flagSet.Bool("https", false, "enable automatic TLS/SSL certificate management")
flagSet.Bool("dev_https", false, "[dev environment] enable automatic TLS/SSL certificate management")
flagSet.String(
"data_dir",
workingDir,
"directory where application data should be stored. Defaults to working directory",
)
}
func New() (*Config, error) {
cwd, err := os.Getwd()
if err != nil {
return nil, err
}
flags := flag.NewFlagSet("config", flag.ExitOnError)
defineFlags(flags, cwd)
if err = flags.Parse(os.Args[1:]); err != nil {
return nil, err
}
viper.SetConfigName("ponzu")
viper.SetConfigType("props")
viper.AddConfigPath(cwd)
err = viper.ReadInConfig()
if err != nil && errors.As(err, &viper.ConfigFileNotFoundError{}) {
log.Info("config file not found. will default to provided flags")
err = nil
}
if err = viper.BindPFlags(flags); err != nil {
return nil, err
}
return &Config{
Paths: Paths{
PublicPath: viper.GetString("public_path"),
},
ServeConfig: ServeConfig{
HttpsPort: viper.GetInt("https_port"),
HttpPort: viper.GetInt("port"),
Bind: viper.GetString("bind"),
DevHttps: viper.GetBool("dev_https"),
Https: viper.GetBool("https"),
},
}, nil
}