-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
72 lines (61 loc) · 1.81 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
package main
import (
"fmt"
"os"
"strconv"
log "github.com/Sirupsen/logrus"
chromaticity "github.com/evq/chromaticity/lib"
"github.com/evq/chromaticity/servers/api"
"github.com/evq/chromaticity/servers/ssdp"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/postgres"
"github.com/pkg/errors"
"gopkg.in/alecthomas/kingpin.v1"
)
func onHelp(context *kingpin.ParseContext) error {
app.Usage(os.Stderr)
os.Exit(0)
return nil
}
func onVersion(context *kingpin.ParseContext) error {
fmt.Println("0.0.1")
os.Exit(0)
return nil
}
var (
app = kingpin.New("chromaticity", "A Hue-like REST API for your lights")
help = app.Flag("help", "Show help.").Short('h').Dispatch(onHelp).Hidden().Bool()
version = app.Flag("version", "Show application version.").Short('v').Dispatch(onVersion).Bool()
debug = app.Flag("debug", "Enable debug mode.").Short('d').Bool()
port = app.Flag("port", "Api server port.").Short('p').Default("80").Int()
configfile = app.Flag("config", "Config file.").Short('c').Default("").String()
)
func main() {
kingpin.MustParse(app.Parse(os.Args[1:]))
log.SetFormatter(&log.TextFormatter{
ForceColors: true,
DisableColors: false,
DisableTimestamp: false,
FullTimestamp: true,
TimestampFormat: "",
DisableSorting: false,
})
if *debug {
log.SetLevel(log.DebugLevel)
}
if *configfile == "" {
homeDir := os.Getenv("HOME")
if homeDir == "" {
log.Error("Failed to determine homedir")
}
*configfile = homeDir + "/.chromaticity/data.json"
}
db, err := gorm.Open("postgres", os.Getenv("DATABASE_URL"))
if err != nil {
panic(errors.Wrap(err, "failed to connect database"))
}
defer db.Close()
db.AutoMigrate(&chromaticity.ColorState{})
ssdp.StartServer(strconv.Itoa(*port))
api.StartServer(strconv.Itoa(*port), *configfile, db)
}