Skip to content

Commit 292ff1a

Browse files
committed
Add async option which requires the client to poll for status and retrieve result upon completion
1 parent ba3dca5 commit 292ff1a

File tree

3 files changed

+53
-0
lines changed

3 files changed

+53
-0
lines changed

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ require (
1313
github.com/go-playground/universal-translator v0.18.1 // indirect
1414
github.com/go-playground/validator/v10 v10.14.0 // indirect
1515
github.com/goccy/go-json v0.10.2 // indirect
16+
github.com/google/uuid v1.3.0 // indirect
1617
github.com/json-iterator/go v1.1.12 // indirect
1718
github.com/klauspost/cpuid/v2 v2.2.4 // indirect
1819
github.com/leodido/go-urn v1.2.4 // indirect

go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MG
2525
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
2626
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
2727
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
28+
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
29+
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
2830
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
2931
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
3032
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=

main.go

+50
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66

77
"github.com/gin-gonic/contrib/static"
88
"github.com/gin-gonic/gin"
9+
"github.com/google/uuid"
910
"github.com/yuin/gopher-lua"
1011
"golang.org/x/time/rate"
1112

@@ -14,6 +15,14 @@ import (
1415
"sync"
1516
)
1617

18+
type ScriptStatus struct {
19+
Finished bool
20+
Result string
21+
Error error
22+
}
23+
24+
var scriptStatuses = make(map[string]*ScriptStatus)
25+
1726
// Cache for Lua scripts
1827
var scriptCache = make(map[string]string)
1928
var scriptCacheMutex = &sync.RWMutex{}
@@ -126,6 +135,47 @@ func main() {
126135
c.Next()
127136
})
128137

138+
router.POST("/runLuaFileAsync/:filename", func(c *gin.Context) {
139+
// Get filename from the URL
140+
filename := c.Param("filename")
141+
142+
// Check if filename is safe
143+
if filepath.Base(filename) != filename {
144+
c.String(http.StatusBadRequest, "Invalid filename")
145+
return
146+
}
147+
148+
// Parse JSON from request body
149+
var jsonData map[string]interface{}
150+
err := c.ShouldBindJSON(&jsonData)
151+
if err != nil {
152+
c.String(http.StatusBadRequest, "Invalid JSON")
153+
return
154+
}
155+
156+
// Generate a unique ID for this script execution
157+
id := uuid.New().String()
158+
159+
// Create a new ScriptStatus
160+
scriptStatuses[id] = &ScriptStatus{
161+
Finished: false,
162+
}
163+
164+
// Start a goroutine to run the script
165+
go func() {
166+
result, err := runLuaScript(filename, jsonData)
167+
// Update the status when the script is done
168+
scriptStatuses[id].Finished = true
169+
scriptStatuses[id].Result = result
170+
if err != nil {
171+
scriptStatuses[id].Error = err
172+
}
173+
}()
174+
175+
// Return the ID to the client
176+
c.String(http.StatusOK, id)
177+
})
178+
129179
router.POST("/runLuaFile/:filename", func(c *gin.Context) {
130180
// Get filename from the URL
131181
filename := c.Param("filename")

0 commit comments

Comments
 (0)