@@ -53,30 +53,34 @@ func replyWithError(fields log.Fields, rf ReplyFunc, msg string, a ...any) {
5353// HandleMessage receives a message, matches it to a Lua script, and executes the script in a new goroutine
5454func (le * LuaExecutor ) HandleMessage (ctx context.Context , msg * Message , rf ReplyFunc ) {
5555 fields := log.Fields {
56- "subject" : msg .Subject ,
56+ "subject" : msg .Subject ,
5757 "executor" : "lua" ,
5858 }
5959
6060 // Look up the Lua script for the given subject
6161 scripts , err := le .store .GetScripts (ctx , msg .Subject )
62- if err != nil || scripts == nil {
63- replyWithError (fields , rf , "failed to get scripts for subject %s: %w " , msg .Subject , err )
62+ if err != nil {
63+ replyWithError (fields , rf , "failed to get scripts for subject %s: %v " , msg .Subject , err )
6464 return
6565 }
6666
67+ if scripts == nil {
68+ replyWithError (fields , rf , "no scripts found for subject %s" , msg .Subject )
69+ }
70+
6771 errs := make (chan error , len (scripts ))
6872 var wg sync.WaitGroup
6973 r := NewReply ()
7074 // Loop through each scripts attached to the subject as there might be more than one
71- for path , script := range scripts {
75+ for path , scr := range scripts {
7276 wg .Add (1 )
7377
7478 ss := strings .Split (path , "/" )
7579 name := ss [len (ss )- 1 ]
7680 fields ["path" ] = name
7781
7882 // Run the Lua script in a separate goroutine to handle the message for each script
79- go func (content [] byte ) {
83+ go func (scr * scriptLib. Script ) {
8084 defer wg .Done ()
8185
8286 tmp , err := os .MkdirTemp (os .TempDir (), "msgscript-lua-*s" )
@@ -92,14 +96,7 @@ func (le *LuaExecutor) HandleMessage(ctx context.Context, msg *Message, rf Reply
9296 return
9397 }
9498
95- // Read the script to get the headers (for the libraries for example)
96- s , err := scriptLib .ReadString (string (content ))
97- if err != nil {
98- errs <- fmt .Errorf ("failed to read script: %w" , err )
99- return
100- }
101-
102- libs , err := le .store .LoadLibrairies (ctx , s .LibKeys )
99+ libs , err := le .store .LoadLibrairies (ctx , scr .LibKeys )
103100 if err != nil {
104101 errs <- fmt .Errorf ("failed to read librairies: %w" , err )
105102 return
@@ -117,7 +114,7 @@ func (le *LuaExecutor) HandleMessage(ctx context.Context, msg *Message, rf Reply
117114 }
118115 defer le .store .ReleaseLock (ctx , name )
119116
120- log .WithFields (fields ).WithField ("isHTML" , s .HTML ).Debug ("executing script" )
117+ log .WithFields (fields ).WithField ("isHTML" , scr .HTML ).Debug ("executing script" )
121118
122119 L := lua .NewState ()
123120 tctx , tcan := context .WithTimeout (le .ctx , MAX_LUA_RUNNING_TIME )
@@ -147,11 +144,11 @@ func (le *LuaExecutor) HandleMessage(ctx context.Context, msg *Message, rf Reply
147144 sb .Write (l )
148145 sb .WriteString ("\n " )
149146 }
150- sb .Write (content )
147+ sb .Write (scr . Content )
151148 log .WithFields (fields ).Debugf ("script:\n %+s\n \n " , sb .String ())
152149
153150 res := & ScriptResult {
154- IsHTML : s .HTML ,
151+ IsHTML : scr .HTML ,
155152 Headers : make (map [string ]string ),
156153 }
157154 if err := L .DoString (sb .String ()); err != nil {
@@ -163,7 +160,7 @@ func (le *LuaExecutor) HandleMessage(ctx context.Context, msg *Message, rf Reply
163160 }
164161
165162 // Retrieve the result from the Lua state (assuming it's a string)
166- if s .HTML {
163+ if scr .HTML {
167164 // If the message is set to return HTML, we pass 2 things to the fonction named after the HTTP
168165 // method received ex: POST(), GET()...
169166 // The 2 things are:
@@ -177,7 +174,7 @@ func (le *LuaExecutor) HandleMessage(ctx context.Context, msg *Message, rf Reply
177174 // - The body of the message
178175 le .executeRawMessage (fields , L , r , msg , res , name )
179176 }
180- }(script )
177+ }(scr )
181178 }
182179 wg .Wait ()
183180 log .WithField ("subject" , msg .Subject ).Debugf ("finished running %d scripts" , len (scripts ))
0 commit comments