-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
48 lines (38 loc) · 1.53 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
package main
import (
"flag"
"fmt"
"github.com/channingko-madden/pi-vitrine/db"
"github.com/channingko-madden/pi-vitrine/internal"
"log"
"net/http"
"strconv"
)
// DB is a handle, not the actual connection. It has a pool of
// DB connections in the background.
// Can use a global struct like this or pass around DB.
var Db db.Repository
// the init function is called automatically for every package!
// Sets up connection to the database (doesn't open it!)
// Opening occurs lazily
func init() {
connection := "user=pi-vitrine dbname=pi_vitrine password=pi-vitrine host=localhost"
Db = db.NewPostgresDeviceRepository(connection)
}
func main() {
var addressFlag = flag.String("address", "localhost", "IP address")
var portFlag = flag.Int("port", 9000, "Port number")
flag.Parse()
addr := *addressFlag + ":" + strconv.Itoa(*portFlag)
fmt.Printf("pi-vitrine server running on %s\n", addr)
http.HandleFunc("GET /", HomePageHandler)
http.Handle("GET /css/", http.StripPrefix("/css/", http.FileServer(http.FS(content))))
http.Handle("POST /system", internal.HostErrorHandler(CreateSystemDataHandler))
http.HandleFunc("GET /system", GetSystemDataHandler)
http.HandleFunc("GET /device", GetAllDevicesHandler)
http.HandleFunc("GET /device/{name}", GetDeviceHandler)
http.HandleFunc("POST /device", CreateDeviceHandler)
http.Handle("POST /indoor_climate", internal.HostErrorHandler(CreateIndoorClimateDataHandler))
http.HandleFunc("GET /indoor_climate/{device_name}", GetIndoorClimateChartHandler)
log.Fatal(http.ListenAndServe(addr, nil))
}