@@ -51,7 +51,7 @@ func init() {
5151}
5252
5353type workerConfig struct {
54- // Name for the worker. Default: the filename for FrankenPHPApp workers, filename + environment variables for FrankenPHPModule workers.
54+ // Name for the worker. Default: the filename for FrankenPHPApp workers, always prefixed with "m#" for FrankenPHPModule workers.
5555 Name string `json:"name,omitempty"`
5656 // FileName sets the path to the worker script.
5757 FileName string `json:"file_name,omitempty"`
@@ -148,6 +148,86 @@ func (f *FrankenPHPApp) Stop() error {
148148 return nil
149149}
150150
151+ func parseWorkerConfig (d * caddyfile.Dispenser ) (workerConfig , error ) {
152+ wc := workerConfig {}
153+ if d .NextArg () {
154+ wc .FileName = d .Val ()
155+ }
156+
157+ if d .NextArg () {
158+ if d .Val () == "watch" {
159+ wc .Watch = append (wc .Watch , "./**/*.{php,yaml,yml,twig,env}" )
160+ } else {
161+ v , err := strconv .Atoi (d .Val ())
162+ if err != nil {
163+ return wc , err
164+ }
165+
166+ wc .Num = v
167+ }
168+ }
169+
170+ if d .NextArg () {
171+ return wc , errors .New ("FrankenPHP: too many 'worker' arguments: " + d .Val ())
172+ }
173+
174+ for d .NextBlock (1 ) {
175+ v := d .Val ()
176+ switch v {
177+ case "name" :
178+ if ! d .NextArg () {
179+ return wc , d .ArgErr ()
180+ }
181+ wc .Name = d .Val ()
182+ case "file" :
183+ if ! d .NextArg () {
184+ return wc , d .ArgErr ()
185+ }
186+ wc .FileName = d .Val ()
187+ case "num" :
188+ if ! d .NextArg () {
189+ return wc , d .ArgErr ()
190+ }
191+
192+ v , err := strconv .Atoi (d .Val ())
193+ if err != nil {
194+ return wc , err
195+ }
196+
197+ wc .Num = v
198+ case "env" :
199+ args := d .RemainingArgs ()
200+ if len (args ) != 2 {
201+ return wc , d .ArgErr ()
202+ }
203+ if wc .Env == nil {
204+ wc .Env = make (map [string ]string )
205+ }
206+ wc .Env [args [0 ]] = args [1 ]
207+ case "watch" :
208+ if ! d .NextArg () {
209+ // the default if the watch directory is left empty:
210+ wc .Watch = append (wc .Watch , "./**/*.{php,yaml,yml,twig,env}" )
211+ } else {
212+ wc .Watch = append (wc .Watch , d .Val ())
213+ }
214+ default :
215+ allowedDirectives := "name, file, num, env, watch"
216+ return wc , wrongSubDirectiveError ("worker" , allowedDirectives , v )
217+ }
218+ }
219+
220+ if wc .FileName == "" {
221+ return wc , errors .New (`the "file" argument must be specified` )
222+ }
223+
224+ if frankenphp .EmbeddedAppPath != "" && filepath .IsLocal (wc .FileName ) {
225+ wc .FileName = filepath .Join (frankenphp .EmbeddedAppPath , wc .FileName )
226+ }
227+
228+ return wc , nil
229+ }
230+
151231// UnmarshalCaddyfile implements caddyfile.Unmarshaler.
152232func (f * FrankenPHPApp ) UnmarshalCaddyfile (d * caddyfile.Dispenser ) error {
153233 for d .Next () {
@@ -229,82 +309,10 @@ func (f *FrankenPHPApp) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
229309 }
230310
231311 case "worker" :
232- wc := workerConfig {}
233- if d .NextArg () {
234- wc .FileName = d .Val ()
235- }
236-
237- if d .NextArg () {
238- if d .Val () == "watch" {
239- wc .Watch = append (wc .Watch , "./**/*.{php,yaml,yml,twig,env}" )
240- } else {
241- v , err := strconv .Atoi (d .Val ())
242- if err != nil {
243- return err
244- }
245-
246- wc .Num = v
247- }
248- }
249-
250- if d .NextArg () {
251- return errors .New ("FrankenPHP: too many 'worker' arguments: " + d .Val ())
252- }
253-
254- for d .NextBlock (1 ) {
255- v := d .Val ()
256- switch v {
257- case "name" :
258- if ! d .NextArg () {
259- return d .ArgErr ()
260- }
261- wc .Name = d .Val ()
262- case "file" :
263- if ! d .NextArg () {
264- return d .ArgErr ()
265- }
266- wc .FileName = d .Val ()
267- case "num" :
268- if ! d .NextArg () {
269- return d .ArgErr ()
270- }
271-
272- v , err := strconv .Atoi (d .Val ())
273- if err != nil {
274- return err
275- }
276-
277- wc .Num = v
278- case "env" :
279- args := d .RemainingArgs ()
280- if len (args ) != 2 {
281- return d .ArgErr ()
282- }
283- if wc .Env == nil {
284- wc .Env = make (map [string ]string )
285- }
286- wc .Env [args [0 ]] = args [1 ]
287- case "watch" :
288- if ! d .NextArg () {
289- // the default if the watch directory is left empty:
290- wc .Watch = append (wc .Watch , "./**/*.{php,yaml,yml,twig,env}" )
291- } else {
292- wc .Watch = append (wc .Watch , d .Val ())
293- }
294- default :
295- allowedDirectives := "name, file, num, env, watch"
296- return wrongSubDirectiveError ("worker" , allowedDirectives , v )
297- }
298- }
299-
300- if wc .FileName == "" {
301- return errors .New (`the "file" argument must be specified` )
302- }
303-
304- if frankenphp .EmbeddedAppPath != "" && filepath .IsLocal (wc .FileName ) {
305- wc .FileName = filepath .Join (frankenphp .EmbeddedAppPath , wc .FileName )
312+ wc , err := parseWorkerConfig (d )
313+ if err != nil {
314+ return err
306315 }
307-
308316 if wc .Name == "" {
309317 // let worker initialization validate if the FileName is valid or not
310318 name , _ := fastabs .FastAbs (wc .FileName )
@@ -545,62 +553,9 @@ func (f *FrankenPHPModule) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
545553 for d .Next () {
546554 for d .NextBlock (0 ) {
547555 if d .Val () == "worker" {
548- wc := workerConfig {}
549- if d .NextArg () {
550- wc .FileName = d .Val ()
551- }
552-
553- if d .NextArg () {
554- v , err := strconv .Atoi (d .Val ())
555- if err != nil {
556- return err
557- }
558- wc .Num = v
559- }
560-
561- for d .NextBlock (1 ) {
562- switch d .Val () {
563- case "name" :
564- if ! d .NextArg () {
565- return d .ArgErr ()
566- }
567- wc .Name = d .Val ()
568- case "file" :
569- if ! d .NextArg () {
570- return d .ArgErr ()
571- }
572- wc .FileName = d .Val ()
573- case "num" :
574- if ! d .NextArg () {
575- return d .ArgErr ()
576- }
577- v , err := strconv .Atoi (d .Val ())
578- if err != nil {
579- return err
580- }
581- wc .Num = v
582- case "env" :
583- args := d .RemainingArgs ()
584- if len (args ) != 2 {
585- return d .ArgErr ()
586- }
587- if wc .Env == nil {
588- wc .Env = make (map [string ]string )
589- }
590- wc .Env [args [0 ]] = args [1 ]
591- case "watch" :
592- if ! d .NextArg () {
593- wc .Watch = append (wc .Watch , "./**/*.{php,yaml,yml,twig,env}" )
594- } else {
595- wc .Watch = append (wc .Watch , d .Val ())
596- }
597- default :
598- return fmt .Errorf ("unknown worker subdirective: %s" , d .Val ())
599- }
600- }
601-
602- if wc .FileName == "" {
603- return errors .New (`the "file" argument must be specified` )
556+ wc , err := parseWorkerConfig (d )
557+ if err != nil {
558+ return err
604559 }
605560
606561 // Inherit environment variables from the parent php_server directive
0 commit comments