Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not extract embedded app on every execution #488

Merged
merged 4 commits into from
Jan 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@
!testdata/*.txt
!build-static.sh
!app.tar
!app_checksum.txt
Empty file added app_checksum.txt
Empty file.
2 changes: 2 additions & 0 deletions build-static.sh
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ cd ../..
# Embed PHP app, if any
if [ -n "${EMBED}" ] && [ -d "${EMBED}" ]; then
tar -cf app.tar -C "${EMBED}" .
md5 -q app.tar > app_checksum.txt
fi

if [ "${os}" = "linux" ]; then
Expand All @@ -139,6 +140,7 @@ cd ../..

if [ -d "${EMBED}" ]; then
truncate -s 0 app.tar
truncate -s 0 app_checksum.txt
fi

"dist/${bin}" version
Expand Down
19 changes: 9 additions & 10 deletions embed.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ package frankenphp
import (
"archive/tar"
"bytes"
"crypto/md5"
_ "embed"
"encoding/hex"
"errors"
"fmt"
"io"
Expand All @@ -25,21 +23,22 @@ var EmbeddedAppPath string
//go:embed app.tar
var embeddedApp []byte

//go:embed app_checksum.txt
var embeddedAppChecksum []byte

func init() {
if len(embeddedApp) == 0 {
// No embedded app
return
}

h := md5.Sum(embeddedApp)
appPath := filepath.Join(os.TempDir(), "frankenphp_"+hex.EncodeToString(h[:]))
appPath := filepath.Join(os.TempDir(), "frankenphp_"+strings.TrimSuffix(string(embeddedAppChecksum[:]), "\n"))

if err := os.RemoveAll(appPath); err != nil {
panic(err)
}
if err := untar(appPath); err != nil {
os.RemoveAll(appPath)
panic(err)
if _, err := os.Stat(appPath); os.IsNotExist(err) {
if err := untar(appPath); err != nil {
os.RemoveAll(appPath)
panic(err)
}
}

EmbeddedAppPath = appPath
Expand Down
Loading