-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
115 lines (91 loc) · 2.52 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
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
package main
import (
"bytes"
"encoding/base64"
"flag"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"github.com/YaleSpinup/cost-api/api"
"github.com/YaleSpinup/cost-api/common"
log "github.com/sirupsen/logrus"
)
var (
// Version is the main version number
Version = "0.0.0"
// VersionPrerelease is a prerelease marker
VersionPrerelease = ""
// Buildstamp is the timestamp the binary was built, it should be set at buildtime with ldflags
Buildstamp = "No BuildStamp Provided"
// Githash is the git sha of the built binary, it should be set at buildtime with ldflags
Githash = "No Git Commit Provided"
configFileName = flag.String("config", "config/config.json", "Configuration file.")
version = flag.Bool("version", false, "Display version information and exit.")
)
func main() {
flag.Parse()
if *version {
vers()
}
cwd, err := os.Getwd()
if err != nil {
log.Fatal("unable to get working directory")
}
log.Infof("Starting Cost-API version %s%s (%s)", Version, VersionPrerelease, cwd)
config, err := common.ReadConfig(configReader())
if err != nil {
log.Fatalf("Unable to read configuration from: %+v", err)
}
config.Version = common.Version{
Version: Version,
VersionPrerelease: VersionPrerelease,
BuildStamp: Buildstamp,
GitHash: Githash,
}
// Set the loglevel, info if it's unset
switch config.LogLevel {
case "error":
log.SetLevel(log.ErrorLevel)
case "warn":
log.SetLevel(log.WarnLevel)
case "debug":
log.SetLevel(log.DebugLevel)
default:
log.SetLevel(log.InfoLevel)
}
if config.LogLevel == "debug" {
log.Debug("Starting profiler on 127.0.0.1:6080")
go http.ListenAndServe("127.0.0.1:6080", nil)
}
log.Debugf("Read config: %+v", config)
if err := api.NewServer(config); err != nil {
log.Fatal(err)
}
}
func configReader() io.Reader {
if configEnv := os.Getenv("API_CONFIG"); configEnv != "" {
log.Infof("reading configuration from API_CONFIG environment")
c, err := base64.StdEncoding.DecodeString(configEnv)
if err != nil {
log.Infof("API_CONFIG is not base64 encoded")
c = []byte(configEnv)
}
return bytes.NewReader(c)
}
log.Infof("reading configuration from %s", *configFileName)
configFile, err := os.Open(*configFileName)
if err != nil {
log.Fatalln("unable to open config file", err)
}
c, err := ioutil.ReadAll(configFile)
if err != nil {
log.Fatalln("unable to read config file", err)
}
return bytes.NewReader(c)
}
func vers() {
fmt.Printf("Cost-API Version: %s%s\n", Version, VersionPrerelease)
os.Exit(0)
}