6
6
7
7
"github.com/gin-gonic/contrib/static"
8
8
"github.com/gin-gonic/gin"
9
+ "github.com/google/uuid"
9
10
"github.com/yuin/gopher-lua"
10
11
"golang.org/x/time/rate"
11
12
@@ -14,6 +15,14 @@ import (
14
15
"sync"
15
16
)
16
17
18
+ type ScriptStatus struct {
19
+ Finished bool
20
+ Result string
21
+ Error error
22
+ }
23
+
24
+ var scriptStatuses = make (map [string ]* ScriptStatus )
25
+
17
26
// Cache for Lua scripts
18
27
var scriptCache = make (map [string ]string )
19
28
var scriptCacheMutex = & sync.RWMutex {}
@@ -126,6 +135,47 @@ func main() {
126
135
c .Next ()
127
136
})
128
137
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
+
129
179
router .POST ("/runLuaFile/:filename" , func (c * gin.Context ) {
130
180
// Get filename from the URL
131
181
filename := c .Param ("filename" )
0 commit comments