Skip to content

Commit c99b6db

Browse files
fix: nomercure build tag (#2212)
- Fixes nomercure tag build - Conditionally addds the mercure route and mercure project import Fixes #2209 I am by no means a go developer, feel free to use this or not :) --------- Signed-off-by: tehmaestro <alexnegrila89@gmail.com> Co-authored-by: Alexander Stecher <45872305+AlliBalliBaba@users.noreply.github.com>
1 parent 0dd485c commit c99b6db

3 files changed

Lines changed: 50 additions & 33 deletions

File tree

caddy/mercure-skip.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,17 @@
22

33
package caddy
44

5+
import (
6+
"github.com/caddyserver/caddy/v2"
7+
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
8+
)
9+
510
type mercureContext struct {
611
}
712

8-
func (f *FrankenPHPModule) configureHotReload(_ *FrankenPHPApp) error {
9-
return nil
13+
func (f *FrankenPHPModule) assignMercureHub(_ caddy.Context) {
1014
}
1115

12-
func (f *FrankenPHPModule) assignMercureHub(_ caddy.Context) {
16+
func createMercureRoute() (caddyhttp.Route, error) {
17+
return caddyhttp.Route{}, nil
1318
}

caddy/mercure.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,12 @@
33
package caddy
44

55
import (
6+
"encoding/json"
7+
"errors"
8+
"os"
69
"github.com/caddyserver/caddy/v2"
10+
"github.com/caddyserver/caddy/v2/caddyconfig"
11+
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
712
"github.com/dunglas/frankenphp"
813
"github.com/dunglas/mercure"
914
mercureCaddy "github.com/dunglas/mercure/caddy"
@@ -31,3 +36,36 @@ func (f *FrankenPHPModule) assignMercureHub(ctx caddy.Context) {
3136
f.Workers[i] = wc
3237
}
3338
}
39+
40+
func createMercureRoute() (caddyhttp.Route, error) {
41+
mercurePublisherJwtKey := os.Getenv("MERCURE_PUBLISHER_JWT_KEY")
42+
if mercurePublisherJwtKey == "" {
43+
return caddyhttp.Route{}, errors.New(`The "MERCURE_PUBLISHER_JWT_KEY" environment variable must be set to use the Mercure.rocks hub`)
44+
}
45+
46+
mercureSubscriberJwtKey := os.Getenv("MERCURE_SUBSCRIBER_JWT_KEY")
47+
if mercureSubscriberJwtKey == "" {
48+
return caddyhttp.Route{}, errors.New(`The "MERCURE_SUBSCRIBER_JWT_KEY" environment variable must be set to use the Mercure.rocks hub`)
49+
}
50+
51+
mercureRoute := caddyhttp.Route{
52+
HandlersRaw: []json.RawMessage{caddyconfig.JSONModuleObject(
53+
mercureCaddy.Mercure{
54+
PublisherJWT: mercureCaddy.JWTConfig{
55+
Alg: os.Getenv("MERCURE_PUBLISHER_JWT_ALG"),
56+
Key: mercurePublisherJwtKey,
57+
},
58+
SubscriberJWT: mercureCaddy.JWTConfig{
59+
Alg: os.Getenv("MERCURE_SUBSCRIBER_JWT_ALG"),
60+
Key: mercureSubscriberJwtKey,
61+
},
62+
},
63+
"handler",
64+
"mercure",
65+
nil,
66+
),
67+
},
68+
}
69+
70+
return mercureRoute, nil;
71+
}

caddy/php-server.go

Lines changed: 4 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ import (
1010
"strings"
1111
"time"
1212

13-
mercureModule "github.com/dunglas/mercure/caddy"
14-
1513
"github.com/caddyserver/caddy/v2"
1614
"github.com/caddyserver/caddy/v2/caddyconfig"
1715
caddycmd "github.com/caddyserver/caddy/v2/cmd"
@@ -253,34 +251,10 @@ func cmdPHPServer(fs caddycmd.Flags) (int, error) {
253251
}
254252

255253
if mercure {
256-
mercurePublisherJwtKey := os.Getenv("MERCURE_PUBLISHER_JWT_KEY")
257-
if mercurePublisherJwtKey == "" {
258-
panic(`The "MERCURE_PUBLISHER_JWT_KEY" environment variable must be set to use the Mercure.rocks hub`)
259-
}
260-
261-
mercureSubscriberJwtKey := os.Getenv("MERCURE_SUBSCRIBER_JWT_KEY")
262-
if mercureSubscriberJwtKey == "" {
263-
panic(`The "MERCURE_SUBSCRIBER_JWT_KEY" environment variable must be set to use the Mercure.rocks hub`)
264-
}
265-
266-
mercureRoute := caddyhttp.Route{
267-
HandlersRaw: []json.RawMessage{caddyconfig.JSONModuleObject(
268-
mercureModule.Mercure{
269-
PublisherJWT: mercureModule.JWTConfig{
270-
Alg: os.Getenv("MERCURE_PUBLISHER_JWT_ALG"),
271-
Key: mercurePublisherJwtKey,
272-
},
273-
SubscriberJWT: mercureModule.JWTConfig{
274-
Alg: os.Getenv("MERCURE_SUBSCRIBER_JWT_ALG"),
275-
Key: mercureSubscriberJwtKey,
276-
},
277-
},
278-
"handler",
279-
"mercure",
280-
nil,
281-
),
282-
},
283-
}
254+
mercureRoute, err := createMercureRoute()
255+
if err != nil {
256+
return caddy.ExitCodeFailedStartup, err
257+
}
284258

285259
subroute.Routes = append(caddyhttp.RouteList{mercureRoute}, subroute.Routes...)
286260
}

0 commit comments

Comments
 (0)