-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
107 lines (97 loc) · 2.56 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
package config
import (
"encoding/json"
"fmt"
"io"
"net/url"
"os"
)
// Config is the parent configuration struct and includes fields for single
// configurations of a database, cookie, and SMTP connection.
type Config struct {
HTTPS bool `json:"https"`
Domain string `json:"domain"`
ProxyDomain string `json:"proxy_domain"`
Port int `json:"port"`
ProxyPort int `json:"proxy_port"`
TemplateDir string `json:"templates"`
AbsPath string `json:"abs_path"`
MediaDir string `json:"media"`
MediaURL string `json:"media_url"`
StaticDir string `json:"static"`
StaticURL string `json:"static_url"`
SecretKey string `json:"secret_key"`
Version string `json:"version"`
Database Database `json:"database"`
Cookie Cookie `json:"cookie"`
SMTP SMTP `json:"smtp"`
Metadata Metadata `json:"metadata"`
}
// Address returns the domain:port pair.
func (c Config) Address() string {
return fmt.Sprintf("%s:%d", c.Domain, c.Port)
}
// URL returns the domain:port scheme. Port is omitted if 80.
func (c Config) URL() (u *url.URL) {
u = &url.URL{}
if c.HTTPS {
u.Scheme = "https"
} else {
u.Scheme = "http"
}
// Fallback to the non proxy domain and ports
domain := c.ProxyDomain
if domain == "" {
domain = c.Domain
}
port := c.ProxyPort
if port == 0 {
port = c.Port
}
if port == 80 {
u.Host = domain
} else {
u.Host = fmt.Sprintf("%s:%d", domain, port)
}
return
}
// FullAddress returns the scheme, domain, port, and host - including
// proxy info
func (c Config) FullAddress() string {
return c.URL().String()
}
// StaticAddress adds the static URL to the full address
// TODO If staticURL is already a valid URL just return that
func (c Config) StaticAddress() string {
url := c.URL()
url.Path = c.StaticURL
return url.String()
}
// Parse will create a Config using the file settings.json in the
// current directory.
func Parse() (Config, error) {
return ParsePath("./settings.json")
}
// ParsePath will create a Config using the file at the given path.
func ParsePath(filename string) (Config, error) {
f, err := os.Open(filename)
if err != nil {
return Config{}, err
}
return parse(f)
}
func parse(f io.Reader) (Config, error) {
c := Config{Cookie: DefaultCookie}
if err := json.NewDecoder(f).Decode(&c); err != nil {
return c, err
}
return c, nil
}
// Default is a basic configuration with insecure values. It will return the
// Address localhost:8080
var Default = Config{
Cookie: DefaultCookie,
Port: 8080,
StaticURL: "/static/",
Metadata: Metadata{},
}