-
Notifications
You must be signed in to change notification settings - Fork 0
/
router.go
78 lines (66 loc) · 1.74 KB
/
router.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
package main
import (
"log"
"net/http"
"github.com/go-chi/chi"
"github.com/go-chi/chi/middleware"
"github.com/go-chi/render"
lua "github.com/yuin/gopher-lua"
)
// API is the main REST API
type API struct {
ScriptEnabled bool `json:"enabled"`
Error error `json:"error,omitempty"`
luaState *lua.LState
handler http.Handler
}
type state struct {
ScriptEnabled bool `json:"enabled"`
}
// NewAPI instantiates a new REST API
func NewAPI(L *lua.LState) *API {
api := &API{
// TODO: add `RUN_SCRIPT_AT_LOAD` env config
ScriptEnabled: true,
luaState: L,
}
r := chi.NewRouter()
r.Use(middleware.RequestID)
r.Use(middleware.Recoverer)
r.Route("/automata", func(r chi.Router) {
r.Get("/state", func(w http.ResponseWriter, r *http.Request) {
render.JSON(w, r, api)
})
r.Get("/enable", func(w http.ResponseWriter, r *http.Request) {
api.ScriptEnabled = true
api.Error = api.callLuaMethod("onEnable")
render.JSON(w, r, api)
})
r.Get("/disable", func(w http.ResponseWriter, r *http.Request) {
api.ScriptEnabled = false
api.Error = api.callLuaMethod("onDisable")
render.JSON(w, r, api)
})
r.Post("/state", func(w http.ResponseWriter, r *http.Request) {
var s state
if err := render.DecodeJSON(r.Body, &s); err != nil {
log.Println(err)
return
}
api.ScriptEnabled = s.ScriptEnabled
render.JSON(w, r, api)
})
})
api.handler = r
return api
}
// Start starts API at address
func (api API) Start(addr string) {
http.ListenAndServe(addr, api.handler)
}
func (api API) callLuaMethod(method string) error {
return api.luaState.CallByParam(lua.P{
Fn: api.luaState.GetGlobal(method), // name of Lua function
Protect: true, // return err or panic
})
}