-
-
Notifications
You must be signed in to change notification settings - Fork 261
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: embed PHP apps into the FrankenPHP binary
- Loading branch information
Showing
5 changed files
with
137 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package frankenphp | ||
|
||
import ( | ||
"crypto/md5" | ||
"embed" | ||
_ "embed" | ||
"encoding/hex" | ||
"fmt" | ||
"io/fs" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
) | ||
|
||
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) | ||
if err != nil { | ||
// no embedded app | ||
return | ||
} | ||
|
||
e, err := os.Executable() | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
e, err = filepath.EvalSymlinks(e) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
// TODO: use XXH3 instead of MD5 | ||
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) | ||
} | ||
if err := copyToDisk(appPath, embedDir, entries); err != nil { | ||
os.RemoveAll(appPath) | ||
panic(err) | ||
} | ||
|
||
EmbeddedAppPath = appPath | ||
EmbeddedDocumentRoot = appPath + publicDir | ||
} | ||
|
||
func copyToDisk(appPath string, currentDir string, entries []fs.DirEntry) error { | ||
if err := os.Mkdir(appPath+strings.TrimPrefix(currentDir, embedDir), 0700); err != nil { | ||
return err | ||
} | ||
|
||
for _, entry := range entries { | ||
name := entry.Name() | ||
|
||
if entry.IsDir() { | ||
entries, err := embeddedApp.ReadDir(currentDir + "/" + name) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if err := copyToDisk(appPath, currentDir+"/"+name, entries); err != nil { | ||
return err | ||
} | ||
|
||
continue | ||
} | ||
|
||
data, err := embeddedApp.ReadFile(currentDir + "/" + name) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
f := appPath + "/" + strings.TrimPrefix(currentDir, embedDir) + "/" + name | ||
if err := os.WriteFile(f, data, 0500); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters