Skip to content

Commit

Permalink
simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
dunglas committed Dec 2, 2023
1 parent d598959 commit 3faab77
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 21 deletions.
24 changes: 18 additions & 6 deletions caddy/caddy.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import (
"go.uber.org/zap"
)

const defaultDocumentRoot = "public"

func init() {
caddy.RegisterModule(FrankenPHPApp{})
caddy.RegisterModule(FrankenPHPModule{})
Expand Down Expand Up @@ -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"}
}
Expand Down Expand Up @@ -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
Expand Down
9 changes: 7 additions & 2 deletions caddy/php-server.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"log"
"net/http"
"path/filepath"
"strconv"
"time"

Expand Down Expand Up @@ -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"
Expand Down
18 changes: 6 additions & 12 deletions embed.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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)
}
Expand All @@ -59,7 +54,6 @@ func init() {
}

EmbeddedAppPath = appPath
EmbeddedDocumentRoot = appPath + publicDir
}

func copyToDisk(appPath string, currentDir string, entries []fs.DirEntry) error {
Expand Down
2 changes: 1 addition & 1 deletion frankenphp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 3faab77

Please sign in to comment.