Skip to content

Commit a46e160

Browse files
committed
feat: hot reloading
1 parent 6c764ad commit a46e160

17 files changed

Lines changed: 494 additions & 265 deletions

caddy/app.go

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -150,25 +150,13 @@ func (f *FrankenPHPApp) Start() error {
150150
)
151151

152152
for _, w := range f.Workers {
153-
workerOpts := make([]frankenphp.WorkerOption, 0, len(w.requestOptions)+4)
154-
155-
if w.requestOptions == nil {
156-
workerOpts = append(workerOpts,
157-
frankenphp.WithWorkerEnv(w.Env),
158-
frankenphp.WithWorkerWatchMode(w.Watch),
159-
frankenphp.WithWorkerMaxFailures(w.MaxConsecutiveFailures),
160-
frankenphp.WithWorkerMaxThreads(w.MaxThreads),
161-
)
162-
} else {
163-
workerOpts = append(
164-
workerOpts,
165-
frankenphp.WithWorkerEnv(w.Env),
166-
frankenphp.WithWorkerWatchMode(w.Watch),
167-
frankenphp.WithWorkerMaxFailures(w.MaxConsecutiveFailures),
168-
frankenphp.WithWorkerMaxThreads(w.MaxThreads),
169-
frankenphp.WithWorkerRequestOptions(w.requestOptions...),
170-
)
171-
}
153+
workerOpts := w.appendMercureHubOption([]frankenphp.WorkerOption{
154+
frankenphp.WithWorkerEnv(w.Env),
155+
frankenphp.WithWorkerWatchMode(w.Watch),
156+
frankenphp.WithWorkerMaxFailures(w.MaxConsecutiveFailures),
157+
frankenphp.WithWorkerMaxThreads(w.MaxThreads),
158+
frankenphp.WithWorkerRequestOptions(w.requestOptions...),
159+
})
172160

173161
opts = append(opts, frankenphp.WithWorkers(w.Name, repl.ReplaceKnown(w.FileName, ""), w.Num, workerOpts...))
174162
}

caddy/mercure-skip.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
//go:build nomercure
2+
23
package caddy
34

45
import (
@@ -7,3 +8,10 @@ import (
78

89
func (f *FrankenPHPModule) assignMercureHubRequestOption(_ caddy.Context) {
910
}
11+
12+
type mercureContext struct {
13+
}
14+
15+
func (wc *workerConfig) appendMercureHubOption(opts []frankenphp.WorkerOption) []frankenphp.WorkerOption {
16+
return opts
17+
}

caddy/mercure.go

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,38 @@ package caddy
55
import (
66
"github.com/caddyserver/caddy/v2"
77
"github.com/dunglas/frankenphp"
8+
"github.com/dunglas/mercure"
89
mercureCaddy "github.com/dunglas/mercure/caddy"
910
)
1011

1112
func init() {
1213
mercureCaddy.AllowNoPublish = true
1314
}
1415

15-
func (f *FrankenPHPModule) assignMercureHubRequestOption(ctx caddy.Context) {
16-
if hub := mercureCaddy.FindHub(ctx.Modules()); hub != nil {
17-
opt := frankenphp.WithMercureHub(hub)
18-
f.mercureHubRequestOption = &opt
16+
type mercureContext struct {
17+
mercureHub *mercure.Hub
18+
}
19+
20+
func (f *FrankenPHPModule) assignMercureHub(ctx caddy.Context) {
21+
hub := mercureCaddy.FindHub(ctx.Modules())
22+
if hub == nil {
23+
return
24+
}
25+
26+
opt := frankenphp.WithMercureHub(hub)
27+
f.mercureHubRequestOption = &opt
28+
29+
for i, wc := range f.Workers {
30+
wc.mercureHub = hub
31+
32+
f.Workers[i] = wc
1933
}
2034
}
35+
36+
func (wc *workerConfig) appendMercureHubOption(opts []frankenphp.WorkerOption) []frankenphp.WorkerOption {
37+
if wc.mercureHub != nil {
38+
opts = append(opts, frankenphp.WithWorkerMercureHub(wc.mercureHub))
39+
}
40+
41+
return opts
42+
}

caddy/module.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ func (f *FrankenPHPModule) Provision(ctx caddy.Context) error {
7474
return fmt.Errorf(`expected ctx.App("frankenphp") to return *FrankenPHPApp, got nil`)
7575
}
7676

77-
f.assignMercureHubRequestOption(ctx)
77+
f.assignMercureHub(ctx)
7878

7979
for i, wc := range f.Workers {
8080
// make the file path absolute from the public directory
@@ -89,9 +89,6 @@ func (f *FrankenPHPModule) Provision(ctx caddy.Context) error {
8989
}
9090

9191
wc.requestOptions = []frankenphp.RequestOption{frankenphp.WithRequestLogger(f.logger)}
92-
if f.mercureHubRequestOption != nil {
93-
wc.requestOptions = append(wc.requestOptions, *f.mercureHubRequestOption)
94-
}
9592

9693
f.Workers[i] = wc
9794
}

caddy/workerconfig.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ import (
2222
// }
2323
// }
2424
type workerConfig struct {
25+
mercureContext
26+
2527
// Name for the worker. Default: the filename for FrankenPHPApp workers, always prefixed with "m#" for FrankenPHPModule workers.
2628
Name string `json:"name,omitempty"`
2729
// FileName sets the path to the worker script.
@@ -39,7 +41,7 @@ type workerConfig struct {
3941
// MaxConsecutiveFailures sets the maximum number of consecutive failures before panicking (defaults to 6, set to -1 to never panick)
4042
MaxConsecutiveFailures int `json:"max_consecutive_failures,omitempty"`
4143

42-
requestOptions []frankenphp.RequestOption
44+
requestOptions []frankenphp.RequestOption
4345
}
4446

4547
func parseWorkerConfig(d *caddyfile.Dispenser) (workerConfig, error) {

internal/watcher/types.go

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package watcher
2+
3+
import (
4+
"encoding/json"
5+
"time"
6+
)
7+
8+
type (
9+
EffectType int8
10+
PathType int8
11+
)
12+
13+
func (e EffectType) MarshalJSON() ([]byte, error) {
14+
var s string
15+
switch e {
16+
case EffectTypeRename:
17+
s = "rename"
18+
case EffectTypeModify:
19+
s = "modify"
20+
case EffectTypeCreate:
21+
s = "create"
22+
case EffectTypeDestroy:
23+
s = "destroy"
24+
case EffectTypeOwner:
25+
s = "owner"
26+
case EffectTypeOther:
27+
s = "other"
28+
}
29+
30+
return json.Marshal(s)
31+
}
32+
33+
func (e PathType) MarshalJSON() ([]byte, error) {
34+
var s string
35+
switch e {
36+
case PathTypeDir:
37+
s = "dir"
38+
case PathTypeFile:
39+
s = "file"
40+
case PathTypeHardLink:
41+
s = "hard_link"
42+
case PathTypeSymLink:
43+
s = "sym_link"
44+
case PathTypeWatcher:
45+
s = "watcher"
46+
case PathTypeOther:
47+
s = "other"
48+
}
49+
50+
return json.Marshal(s)
51+
}
52+
53+
type Event struct {
54+
EffectTime time.Time
55+
PathName string
56+
AssociatedPathName string
57+
EffectType EffectType
58+
PathType PathType
59+
}
60+
61+
const (
62+
EffectTypeRename EffectType = iota
63+
EffectTypeModify
64+
EffectTypeCreate
65+
EffectTypeDestroy
66+
EffectTypeOwner
67+
EffectTypeOther
68+
)
69+
70+
const (
71+
PathTypeDir PathType = iota
72+
PathTypeFile
73+
PathTypeHardLink
74+
PathTypeSymLink
75+
PathTypeWatcher
76+
PathTypeOther
77+
)
78+
79+
type callbackFunc func(events []*Event)
80+
81+
type PatternGroup struct {
82+
Patterns []string
83+
Callback callbackFunc
84+
}

0 commit comments

Comments
 (0)