diff --git a/caddy/caddy.go b/caddy/caddy.go index 330546aaf9..52eb874843 100644 --- a/caddy/caddy.go +++ b/caddy/caddy.go @@ -21,6 +21,8 @@ import ( "go.uber.org/zap" ) +const defaultDocumentRoot = "public" + func init() { caddy.RegisterModule(FrankenPHPApp{}) caddy.RegisterModule(FrankenPHPModule{}) @@ -220,13 +222,18 @@ func (f *FrankenPHPModule) Provision(ctx caddy.Context) error { f.logger = ctx.Logger(f) if f.Root == "" { - if frankenphp.EmbeddedDocumentRoot == "" { + if frankenphp.EmbeddedAppPath == "" { f.Root = "{http.vars.root}" } else { - f.Root = frankenphp.EmbeddedDocumentRoot + f.Root = frankenphp.EmbeddedAppPath + defaultDocumentRoot f.ResolveRootSymlink = false } + } else { + if frankenphp.EmbeddedAppPath != "" && filepath.IsLocal(f.Root) { + f.Root = filepath.Join(frankenphp.EmbeddedAppPath, f.Root) + } } + if len(f.SplitPath) == 0 { f.SplitPath = []string{".php"} } @@ -433,10 +440,15 @@ func parsePhpServer(h httpcaddyfile.Helper) ([]httpcaddyfile.ConfigValue, error) // unmarshaler can read it from the start dispenser.Reset() - if phpsrv.Root == "" && frankenphp.EmbeddedAppPath != "" { - phpsrv.Root = frankenphp.EmbeddedDocumentRoot - fsrv.Root = frankenphp.EmbeddedDocumentRoot - phpsrv.ResolveRootSymlink = false + if frankenphp.EmbeddedAppPath != "" { + if phpsrv.Root == "" { + phpsrv.Root = frankenphp.EmbeddedAppPath + defaultDocumentRoot + fsrv.Root = phpsrv.Root + phpsrv.ResolveRootSymlink = false + } else if filepath.IsLocal(fsrv.Root) { + phpsrv.Root = filepath.Join(frankenphp.EmbeddedAppPath, phpsrv.Root) + fsrv.Root = phpsrv.Root + } } // set up a route list that we'll append to diff --git a/caddy/php-server.go b/caddy/php-server.go index f173f8a21f..ebea1a2868 100644 --- a/caddy/php-server.go +++ b/caddy/php-server.go @@ -4,6 +4,7 @@ import ( "encoding/json" "log" "net/http" + "path/filepath" "strconv" "time" @@ -61,8 +62,12 @@ func cmdPHPServer(fs caddycmd.Flags) (int, error) { debug := fs.Bool("debug") compress := !fs.Bool("no-compress") - if root == "" && frankenphp.EmbeddedDocumentRoot != "" { - root = frankenphp.EmbeddedDocumentRoot + if frankenphp.EmbeddedAppPath != "" { + if root == "" { + root = frankenphp.EmbeddedAppPath + defaultDocumentRoot + } else if filepath.IsLocal(root) { + root = filepath.Join(frankenphp.EmbeddedAppPath, root) + } } const indexFile = "index.php" diff --git a/embed.go b/embed.go index 7eba1cde0a..3f54981c23 100644 --- a/embed.go +++ b/embed.go @@ -13,21 +13,21 @@ import ( ) const embedDir = "embed" -const publicDir = "public" // The path of the embedded PHP application (empty if none) var EmbeddedAppPath string -// The path of the document root of the embedded PHP application (empty if none) -var EmbeddedDocumentRoot string - //go:embed all:embed var embeddedApp embed.FS func init() { - _, err := embeddedApp.ReadDir(embedDir + "/" + publicDir) + entries, err := embeddedApp.ReadDir(embedDir) if err != nil { - // no embedded app + panic(err) + } + + if len(entries) == 1 && entries[0].Name() == embedDir+"/.gitignore" { + //no embedded app return } @@ -45,11 +45,6 @@ func init() { h := md5.Sum([]byte(e)) appPath := fmt.Sprintf("%sfrankenphp_%s/", os.TempDir(), hex.EncodeToString(h[:])) - entries, err := embeddedApp.ReadDir(embedDir) - if err != nil { - panic(err) - } - if err := os.RemoveAll(appPath); err != nil { panic(err) } @@ -59,7 +54,6 @@ func init() { } EmbeddedAppPath = appPath - EmbeddedDocumentRoot = appPath + publicDir } func copyToDisk(appPath string, currentDir string, entries []fs.DirEntry) error { diff --git a/frankenphp.go b/frankenphp.go index a0ba4fadb8..820d74cad8 100644 --- a/frankenphp.go +++ b/frankenphp.go @@ -321,7 +321,7 @@ func Init(options ...Option) error { logger.Info("FrankenPHP started 🐘", zap.String("php_version", Version().Version)) if EmbeddedAppPath != "" { - logger.Info("embedded PHP app 📦", zap.String("path", EmbeddedAppPath), zap.String("document_root", EmbeddedDocumentRoot)) + logger.Info("embedded PHP app 📦", zap.String("path", EmbeddedAppPath)) } return nil