-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
58 lines (47 loc) · 2.07 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
package main
import (
"flag"
"net/http"
"os"
"github.com/Girbons/effective-potato/pkg/config"
"github.com/Girbons/effective-potato/pkg/handler"
"github.com/Girbons/effective-potato/pkg/middleware"
"github.com/gorilla/mux"
log "github.com/sirupsen/logrus"
"github.com/stianeikeland/go-rpio"
)
func main() {
// used to create the admin user
createAdminUser := flag.Bool("create-admin-user", false, "Create Admin user")
adminPassword := flag.String("admin-password", "123456", "choose admin password")
flag.Parse()
// initialize database
db := config.InitDB()
defer db.Close()
userHandlers := handler.NewUserHandler(db)
if *createAdminUser {
userHandlers.CreateAdminUser(*adminPassword)
os.Exit(0)
}
if err := rpio.Open(); err != nil {
log.Error(err)
os.Exit(1)
}
defer rpio.Close()
lightHandler := handler.NewLightHandler(db)
router := mux.NewRouter()
// user endpoints
router.HandleFunc("/login/", userHandlers.Login).Methods("POST")
router.HandleFunc("/logout/", userHandlers.Logout).Methods("POST")
router.Handle("/user/profile/", middleware.AuthMiddleware(http.HandlerFunc(userHandlers.Profile))).Methods("GET")
// light endpoints
router.Handle("/api/lights/add/", middleware.AuthMiddleware(http.HandlerFunc(lightHandler.Add))).Methods("POST")
router.Handle("/api/lights/list/", middleware.AuthMiddleware(http.HandlerFunc(lightHandler.List))).Methods("GET")
router.Handle("/api/lights/{id}/detail/", middleware.AuthMiddleware(http.HandlerFunc(lightHandler.Detail))).Methods("GET", "POST", "DELETE")
// pin endpoints
router.Handle("/api/pin/on/{pin:[0-9]+}/", middleware.AuthMiddleware(http.HandlerFunc(handler.PinON))).Methods("GET")
router.Handle("/api/pin/off/{pin:[0-9]+}/", middleware.AuthMiddleware(http.HandlerFunc(handler.PinOFF))).Methods("GET")
router.Handle("/api/pin/status/{pin:[0-9]+}/", middleware.AuthMiddleware(http.HandlerFunc(handler.PinStatus))).Methods("GET")
router.Handle("/api/temperature-sensor/{pin:[0-9]+}/{dht}/", middleware.AuthMiddleware(http.HandlerFunc(handler.ReadTemperature))).Methods("GET")
http.ListenAndServe(":8080", router)
}