forked from msf/go-minipypi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
86 lines (72 loc) · 1.71 KB
/
main.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
package main
import (
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"gopkg.in/yaml.v2"
)
// Configs used by this package.
type Configs struct {
WebConfigs WebServerConfigs
CacheConfigs CacheConfigs
S3configs S3configs
}
var (
version = "dev"
commit = "none"
date = "unknown"
)
func genConfig(filename string) {
cfg := &Configs{
WebConfigs: WebServerConfigs{
Host: "localhost",
BasePath: "/",
Port: 8080,
},
CacheConfigs: CacheConfigs{
ExpireSecs: 120,
},
S3configs: S3configs{
BucketName: "bucket-name",
CredentialsFile: "aws_credentials.ini",
Region: "eu-west-1",
},
}
d, _ := yaml.Marshal(cfg)
ioutil.WriteFile(filename, d, 0640)
}
func main() {
fmt.Printf("go-minipypi - %v, commit %v, built at %v\n", version, commit, date)
cfg := Configs{}
var configFile = flag.String("config", "config.yml", "config file")
flag.Parse()
data, err := ioutil.ReadFile(*configFile)
if err != nil {
genConfig("config.yml.gen")
println("failed to read config file, see example: config.yml.gen")
os.Exit(1)
}
if err := yaml.Unmarshal(data, &cfg); err != nil {
println("failed to parse config")
os.Exit(1)
}
log.Printf("config: %v\n", cfg)
if !isValidConfig(cfg) {
println("Invalid config")
os.Exit(1)
}
fetcher := NewS3Fetcher(cfg.S3configs)
cache := NewCachedFetcher(cfg.CacheConfigs, fetcher)
RunWebServer(cfg.WebConfigs, cache)
}
func isValidConfig(config Configs) bool {
valid := true
valid = valid && len(config.WebConfigs.BasePath) > 0
valid = valid && config.WebConfigs.Port > 0
valid = valid && config.WebConfigs.Port < 65535
valid = valid && len(config.S3configs.BucketName) > 0
valid = valid && len(config.S3configs.Region) > 0
return valid
}