-
Notifications
You must be signed in to change notification settings - Fork 2
/
api.go
101 lines (87 loc) · 1.84 KB
/
api.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
package main
import (
"net/http"
"github.com/labstack/echo"
"github.com/ww24/lirc-web-api/service"
)
func apiv1(g *echo.Group) {
g.GET("", func(c echo.Context) (err error) {
signals, err := service.FetchSignals("")
if err != nil {
return wrapError(err)
}
return &response{
code: http.StatusOK,
Status: "ok",
Signals: signals,
}
})
g.GET("/signals", func(c echo.Context) (err error) {
signals, err := service.FetchSignals("")
if err != nil {
return wrapError(err)
}
return &response{
code: http.StatusOK,
Status: "ok",
Signals: signals,
}
})
g.GET("/:remote", func(c echo.Context) (err error) {
signals, err := service.FetchSignals(c.Param("remote"))
if err != nil {
return wrapError(err)
}
return &response{
code: http.StatusOK,
Status: "ok",
Signals: signals,
}
})
g.POST("/:remote/:name", func(c echo.Context) (err error) {
sig := &service.Signal{
Remote: c.Param("remote"),
Name: c.Param("name"),
}
err = service.SendSignal(&service.SendSignalParam{Signal: sig})
if err != nil {
switch err {
case service.ErrBadSignal:
return &response{
code: http.StatusBadRequest,
Status: "ng",
Message: "invalid signal",
}
default:
return wrapError(err)
}
}
return &response{
code: http.StatusOK,
Status: "ok",
}
})
g.POST("", func(c echo.Context) (err error) {
sendParam := new(service.SendSignalParam)
if err = c.Bind(sendParam); err != nil {
return wrapError(err)
}
err = service.SendSignal(sendParam)
if err != nil {
switch err {
case service.ErrBadSignal:
return &response{
code: http.StatusBadRequest,
Status: "ng",
Message: "invalid signal",
}
default:
return wrapError(err)
}
}
return &response{
code: http.StatusOK,
Status: "ok",
}
})
}