-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
138 lines (112 loc) · 3.79 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package main
import (
"os"
"fmt"
"time"
"log"
"net/http"
"encoding/json"
curl "github.com/andelf/go-curl"
"github.com/gorilla/mux"
_ "github.com/lib/pq"
"database/sql"
)
type ConfigVars struct {
DATABASE_URL string `json:"DATABASE_URL"`
DevID string `json:"devID"`
Mac1 string `json:"mac1"`
Mac2 string `json:"mac2"`
Mac3 string `json:"mac3"`
Mac4 string `json:"mac4"`
Ticker_Key string `json:"ticker_key"`
}
var config_vars *ConfigVars
var IS_LIVE bool
func getConfigVarsLocal () bool {
easy := curl.EasyInit()
defer easy.Cleanup()
respSuccess := func (buf []byte, userdata interface{}) bool {
// println("DEBUG: size=>", len(buf))
// println("DEBUG: content=>", string(buf))
if err := json.Unmarshal(buf, config_vars); err != nil {
panic(err)
}
return true
}
easy.Setopt(curl.OPT_URL, "https://api.heroku.com/apps/feelingcolor/config-vars")
easy.Setopt(curl.OPT_NETRC, curl.NETRC_REQUIRED)
easy.Setopt(curl.OPT_HTTPHEADER, []string{"Accept: application/vnd.heroku+json; version=3"})
easy.Setopt(curl.OPT_WRITEFUNCTION, respSuccess)
if err := easy.Perform(); err != nil {
fmt.Printf("ERROR: %v\n", err)
return false
}
return true
}
func getConfigVarsProd () bool {
config_vars.DATABASE_URL = os.Getenv("DATABASE_URL")
config_vars.DevID = os.Getenv("devID")
config_vars.Mac1 = os.Getenv("mac1")
config_vars.Mac2 = os.Getenv("mac2")
config_vars.Mac3 = os.Getenv("mac3")
config_vars.Mac4 = os.Getenv("mac4")
config_vars.Ticker_Key = os.Getenv("ticker_key")
return true
}
func main() {
port := os.Getenv("PORT")
if port == "" {
port = "9090"
}
router := mux.NewRouter()
config_vars = &ConfigVars{}
// get config vars
db_url := os.Getenv("DATABASE_URL")
if db_url == "" {
getConfigVarsLocal()
} else {
getConfigVarsProd()
}
// fmt.Printf("%+v\n", *config_vars)
// new controller, log in
_remoteLogin()
// database
var sql_err error
db, sql_err = sql.Open("postgres", config_vars.DATABASE_URL)
if sql_err != nil {
log.Fatal(sql_err)
}
// API
router.HandleFunc("/send-mood/{color}", SendMoodCommand).Methods("GET")
router.HandleFunc("/bulb/color/{mac}/{color}", ChangeColorByMacCommand).Methods("GET")
router.HandleFunc("/ctrl/start/{key}", StartTickerCommand).Methods("GET")
router.HandleFunc("/ctrl/stop/{key}", StopTickerCommand).Methods("GET")
router.HandleFunc("/ctrl/status", StatusCommand).Methods("GET")
// only expose if running localhost
if os.Getenv("DATABASE_URL") == "" {
// also ticker_key is just "209" if running locally
config_vars.Ticker_Key = "209"
router.HandleFunc("/ctrl/login", RemoteLoginCommand).Methods("GET")
router.HandleFunc("/ctrl/get-devices", GetDevicesCommand).Methods("GET")
router.HandleFunc("/ctrl/register/{mac}", RegisterDeviceCommand).Methods("GET")
router.HandleFunc("/ctrl/deregister/{mac}", DeregisterDeviceCommand).Methods("GET")
}
// HTML
router.PathPrefix("/").Handler(http.FileServer(http.Dir("./static/")))
http.Handle("/", router)
// start off not live/live depending on time
curHour := time.Now().UTC().Hour()
fmt.Printf("curHour in UTC: %d\n", curHour)
if curHour >= 0 && curHour < 8 {
_setAllPower("ON")
startTicker()
} else {
_setAllPower("OFF")
stopTicker()
}
fmt.Printf("Attempting to run server running on port " + port + "\n")
err := http.ListenAndServe(":" + port, router)
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
}