-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
81 lines (65 loc) · 1.33 KB
/
main.go
File metadata and controls
81 lines (65 loc) · 1.33 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
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
package main
import (
"fmt"
"github.com/kataras/iris"
"github.com/yuin/gopher-lua"
"strconv"
)
func main() {
app := iris.Default()
app.Post("/lua_script", luaHandler())
// listen and serve on http://0.0.0.0:8080.
app.Run(iris.Addr(":8080"))
}
type Pairs struct {
Key string `json:"key"`
Value string `json:"value"` //base64
}
type EvalRequest struct {
Script string `json:"script"`
NumKeys int `json:"numkeys"`
KVs []Pairs `json:"kvs"`
Argv []string `json:"argv"`
}
type EvalResponse struct {
KVs []Pairs `json:"kvs"`
}
func luaHandler() iris.Handler {
return func(ctx iris.Context) {
//run lua vm
L := lua.NewState()
defer L.Close()
//get req
var req EvalRequest
if err := ctx.ReadJSON(&req); err != nil {
panic(err)
}
// load file
if err := L.DoFile("./lua/p50.lua"); err != nil {
panic(err)
}
//init values
var valus []lua.LValue
//logic
kvs := req.KVs
for _, kv := range kvs {
vlaueInt, _ := strconv.ParseInt(kv.Value, 10, 64)
valus = append(valus, lua.LNumber(vlaueInt))
}
// run func
L.CallByParam(lua.P{
Fn: L.GetGlobal("p50"),
NRet: 1,
Protect: true,
}, valus...)
//get lua return
ret := L.Get(-1)
//remove value
L.Pop(1)
res, _ := ret.(lua.LNumber)
fmt.Println(int(res))
//resp
rsp := &EvalResponse{}
ctx.JSON(rsp)
}
}