forked from nkanaev/yarr
-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
62 lines (52 loc) · 1.18 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
package main
import (
"log"
"os"
"strings"
"github.com/jgkawell/yarr/server"
"github.com/jgkawell/yarr/storage"
)
var Version string = "0.0"
var GitHash string = "unknown"
type config struct {
address string
username string
password string
database string
}
func main() {
c := readConfig()
log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)
log.SetOutput(os.Stdout)
store, err := storage.New(c.database)
if err != nil {
log.Fatal("Failed to initialise database: ", err)
}
srv := server.NewServer(store, c.address)
srv.Username = c.username
srv.Password = c.password
log.Printf("starting server at %s", srv.GetAddr())
srv.Start()
}
func readConfig() config {
c := config{
address: os.Getenv("YARR_ADDR"),
database: os.Getenv("YARR_DB"),
}
if c.address == "" {
c.address = "0.0.0.0:7070"
}
if c.database == "" {
log.Fatal("Error YARR_DB is not set. Please set env var YARR_DB to postgres URI.")
}
auth := os.Getenv("YARR_AUTH")
if auth != "" {
parts := strings.SplitN(auth, ":", 2)
if len(parts) != 2 {
log.Fatal("auth was set but with wrong syntax (expected `username:password`)")
}
c.username = parts[0]
c.password = parts[1]
}
return c
}