-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathhandler.go
More file actions
54 lines (44 loc) · 1.23 KB
/
handler.go
File metadata and controls
54 lines (44 loc) · 1.23 KB
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
package huejack
import (
"github.com/julienschmidt/httprouter"
"net/http"
"log"
"io"
"io/ioutil"
)
var handlerMap map[string]huestate
func init() {
log.SetOutput(ioutil.Discard)
handlerMap = make(map[string]huestate)
upnpTemplateInit()
}
func SetLogger(w io.Writer) {
log.SetOutput(w)
}
func ListenAndServe(addr string) error {
router := httprouter.New()
router.GET(upnp_uri, upnpSetup(addr))
router.GET("/api/:userId", getLightsList)
router.PUT("/api/:userId/lights/:lightId/state", setLightState)
router.GET("/api/:userId/lights/:lightId", getLightInfo)
go upnpResponder(addr, upnp_uri)
return http.ListenAndServe(addr, requestLogger(router))
}
// Handler:
// state is the state of the "light" after the handler function
// if error is set to true echo will reply with "sorry the device is not responding"
type Handler func(Request, *Response)
func Handle(deviceName string, h Handler) {
log.Println("[HANDLE]", deviceName)
handlerMap[deviceName] = huestate{
Handler:h,
OnState:false,
}
}
func requestLogger(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
log.Println("[WEB]", r.RemoteAddr, r.Method, r.URL)
// log.Printf("\t%+v\n", r)
h.ServeHTTP(w, r)
})
}