-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathconfig.go
190 lines (174 loc) · 5.15 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
package main
import (
"fmt"
"os"
"runtime"
"strings"
"github.com/fatih/structs"
"gopkg.in/ini.v1"
)
type CassandraConfig struct {
Hosts string `json:"hosts"`
Keyspace string `json:"keyspace"`
ProtoVersion int `json:"ProtoVersion"`
Username string `json:"username"`
Password string `json:"password"`
Enabled bool `json:"enabled"`
}
type AwsConfig struct {
AccessKeyId string `json:"accesskeyid"`
SecretAccessKey string `json:"secretaccesskey"`
Region string `json:"region"`
BucketName string `json:"bucketname"`
BucketAcl string `json:"bucketacl"`
Enabled bool `json:"enabled"`
}
type LdapConfig struct {
Enabled bool `json:"enabled"`
Server string `json:"server"`
Base string `json:"base"`
UserObjectClass string `json:"userobjectclass"`
UserCn string `json:"usercn"`
BindDn string `json:"binddn"`
BindPass string `json:"bindpass"`
}
/*
MySQLConfig (MySQL configuration struct)
=> Host :- MySQL host e.g 127.0.0.1:3306
=> Database :- Name of the database default to lfs_server_go
=> Username :- DB username
=> Password :- DB password
*/
type MySQLConfig struct {
Host string `json:"host"`
Database string `json:"database"`
Username string `json:"username"`
Password string `json:"password"`
Enabled bool `json:"enabled"`
}
// Configuration holds application configuration. Values will be pulled from
// environment variables, prefixed by keyPrefix. Default values can be added
// via tags.
type Configuration struct {
Listen string `json:"listen"`
Host string `json:"host"`
UrlContext string `json:"url_context"`
ContentPath string `json:"content_path"`
AdminUser string `json:"admin_user"`
AdminPass string `json:"admin_pass"`
Cert string `json:"cert"`
Key string `json:"key"`
Scheme string `json:"scheme"`
Public bool `json:"public"`
MetaDB string `json:"metadb"`
BackingStore string `json:"backing_store"`
ContentStore string `json:"content_store"`
LogFile string `json:"logfile"`
NumProcs int `json:"numprocs"`
Aws *AwsConfig `json:"aws"`
Cassandra *CassandraConfig `json:"cassandra"`
Ldap *LdapConfig `json:"ldap"`
MySQL *MySQLConfig `json:"mysql"`
}
func (c *Configuration) IsHTTPS() bool {
return strings.Contains(Config.Scheme, "https")
}
func (c *Configuration) UseTLS() bool {
return Config.Cert != "" && Config.Key != ""
}
func (c *Configuration) IsPublic() bool {
return Config.Public
}
// Config is the global app configuration
//var Config = &Configuration{}
var GoEnv = os.Getenv("GO_ENV")
var Config = &Configuration{}
// iterate thru config.ini and parse it
// always called when initializing Config
func init() {
configFile := os.Getenv("LFS_SERVER_GO_CONFIG")
if configFile == "" {
fmt.Println("LFS_SERVER_GO_CONFIG is not set, Using default config.ini")
configFile = "config.ini"
}
cfg, err := ini.Load(configFile)
if err != nil {
panic(fmt.Sprintf("unable to read config.ini, %v", err))
}
if GoEnv == "" {
GoEnv = "production"
}
//Force scheme to be a valid value
if cfg.Section("Main").Key("Scheme").String() != "" {
val := cfg.Section("Main").Key("Scheme").String()
switch val {
case
"http", "https":
val = val
default:
val = "http"
}
}
awsConfig := &AwsConfig{
AccessKeyId: "",
SecretAccessKey: "",
Region: "USWest",
BucketName: "lfs-server-go-objects",
BucketAcl: "bucket-owner-full-control",
Enabled: false,
}
ldapConfig := &LdapConfig{
Server: "ldap://localhost:1389",
Base: "dc=testers,c=test,o=company",
UserObjectClass: "person",
Enabled: false,
UserCn: "uid",
BindDn: "",
BindPass: "",
}
cassandraConfig := &CassandraConfig{
Hosts: "localhost",
Keyspace: "lfs_server_go",
ProtoVersion: 3,
Username: "",
Password: "",
Enabled: false,
}
mysqlConfig := &MySQLConfig{
Host: "",
Database: "lfs_server_go",
Username: "",
Password: "",
Enabled: false,
}
configuration := &Configuration{
Listen: "tcp://:8080",
Host: "localhost:8080",
UrlContext: "",
ContentPath: "lfs-content",
AdminUser: "admin",
AdminPass: "admin",
Cert: "",
Key: "",
Scheme: "http",
Public: true,
MetaDB: "lfs-test.db",
BackingStore: "bolt",
ContentStore: "filesystem",
NumProcs: runtime.NumCPU(),
Ldap: ldapConfig,
Aws: awsConfig,
Cassandra: cassandraConfig,
MySQL: mysqlConfig,
}
err = cfg.Section("Main").MapTo(configuration)
err = cfg.Section("Aws").MapTo(configuration.Aws)
err = cfg.Section("Ldap").MapTo(configuration.Ldap)
err = cfg.Section("Cassandra").MapTo(configuration.Cassandra)
err = cfg.Section("MySQL").MapTo(configuration.MySQL)
Config = configuration
}
func (c *Configuration) DumpConfig() map[string]interface{} {
m := structs.Map(Config)
return m
}