From c99b6db5ae0876ce738a036dbd05e6f6dfe2300a Mon Sep 17 00:00:00 2001 From: tehmaestro Date: Sun, 22 Feb 2026 19:46:39 +0200 Subject: [PATCH] fix: nomercure build tag (#2212) - Fixes nomercure tag build - Conditionally addds the mercure route and mercure project import Fixes https://github.com/php/frankenphp/issues/2209 I am by no means a go developer, feel free to use this or not :) --------- Signed-off-by: tehmaestro Co-authored-by: Alexander Stecher <45872305+AlliBalliBaba@users.noreply.github.com> --- caddy/mercure-skip.go | 13 +++++++++---- caddy/mercure.go | 38 ++++++++++++++++++++++++++++++++++++++ caddy/php-server.go | 34 ++++------------------------------ 3 files changed, 51 insertions(+), 34 deletions(-) diff --git a/caddy/mercure-skip.go b/caddy/mercure-skip.go index 7aead098..04f2ff2b 100644 --- a/caddy/mercure-skip.go +++ b/caddy/mercure-skip.go @@ -2,12 +2,17 @@ package caddy -type mercureContext struct { -} +import ( + "github.com/caddyserver/caddy/v2" + "github.com/caddyserver/caddy/v2/modules/caddyhttp" +) -func (f *FrankenPHPModule) configureHotReload(_ *FrankenPHPApp) error { - return nil +type mercureContext struct { } func (f *FrankenPHPModule) assignMercureHub(_ caddy.Context) { } + +func createMercureRoute() (caddyhttp.Route, error) { + return caddyhttp.Route{}, nil +} diff --git a/caddy/mercure.go b/caddy/mercure.go index 9624f50e..2de94259 100644 --- a/caddy/mercure.go +++ b/caddy/mercure.go @@ -3,7 +3,12 @@ package caddy import ( + "encoding/json" + "errors" + "os" "github.com/caddyserver/caddy/v2" + "github.com/caddyserver/caddy/v2/caddyconfig" + "github.com/caddyserver/caddy/v2/modules/caddyhttp" "github.com/dunglas/frankenphp" "github.com/dunglas/mercure" mercureCaddy "github.com/dunglas/mercure/caddy" @@ -31,3 +36,36 @@ func (f *FrankenPHPModule) assignMercureHub(ctx caddy.Context) { f.Workers[i] = wc } } + +func createMercureRoute() (caddyhttp.Route, error) { + mercurePublisherJwtKey := os.Getenv("MERCURE_PUBLISHER_JWT_KEY") + if mercurePublisherJwtKey == "" { + return caddyhttp.Route{}, errors.New(`The "MERCURE_PUBLISHER_JWT_KEY" environment variable must be set to use the Mercure.rocks hub`) + } + + mercureSubscriberJwtKey := os.Getenv("MERCURE_SUBSCRIBER_JWT_KEY") + if mercureSubscriberJwtKey == "" { + return caddyhttp.Route{}, errors.New(`The "MERCURE_SUBSCRIBER_JWT_KEY" environment variable must be set to use the Mercure.rocks hub`) + } + + mercureRoute := caddyhttp.Route{ + HandlersRaw: []json.RawMessage{caddyconfig.JSONModuleObject( + mercureCaddy.Mercure{ + PublisherJWT: mercureCaddy.JWTConfig{ + Alg: os.Getenv("MERCURE_PUBLISHER_JWT_ALG"), + Key: mercurePublisherJwtKey, + }, + SubscriberJWT: mercureCaddy.JWTConfig{ + Alg: os.Getenv("MERCURE_SUBSCRIBER_JWT_ALG"), + Key: mercureSubscriberJwtKey, + }, + }, + "handler", + "mercure", + nil, + ), + }, + } + + return mercureRoute, nil; +} diff --git a/caddy/php-server.go b/caddy/php-server.go index 6b66756c..373eb4e0 100644 --- a/caddy/php-server.go +++ b/caddy/php-server.go @@ -10,8 +10,6 @@ import ( "strings" "time" - mercureModule "github.com/dunglas/mercure/caddy" - "github.com/caddyserver/caddy/v2" "github.com/caddyserver/caddy/v2/caddyconfig" caddycmd "github.com/caddyserver/caddy/v2/cmd" @@ -253,34 +251,10 @@ func cmdPHPServer(fs caddycmd.Flags) (int, error) { } if mercure { - mercurePublisherJwtKey := os.Getenv("MERCURE_PUBLISHER_JWT_KEY") - if mercurePublisherJwtKey == "" { - panic(`The "MERCURE_PUBLISHER_JWT_KEY" environment variable must be set to use the Mercure.rocks hub`) - } - - mercureSubscriberJwtKey := os.Getenv("MERCURE_SUBSCRIBER_JWT_KEY") - if mercureSubscriberJwtKey == "" { - panic(`The "MERCURE_SUBSCRIBER_JWT_KEY" environment variable must be set to use the Mercure.rocks hub`) - } - - mercureRoute := caddyhttp.Route{ - HandlersRaw: []json.RawMessage{caddyconfig.JSONModuleObject( - mercureModule.Mercure{ - PublisherJWT: mercureModule.JWTConfig{ - Alg: os.Getenv("MERCURE_PUBLISHER_JWT_ALG"), - Key: mercurePublisherJwtKey, - }, - SubscriberJWT: mercureModule.JWTConfig{ - Alg: os.Getenv("MERCURE_SUBSCRIBER_JWT_ALG"), - Key: mercureSubscriberJwtKey, - }, - }, - "handler", - "mercure", - nil, - ), - }, - } + mercureRoute, err := createMercureRoute() + if err != nil { + return caddy.ExitCodeFailedStartup, err + } subroute.Routes = append(caddyhttp.RouteList{mercureRoute}, subroute.Routes...) }