fix: do not extract embedded app on every execution (#488)

* Do not extract embedded app on every execution

* Add app_checksum.txt to .dockerignore

* Rename embeddedAppHash to embeddedAppChecksum

* Remove check for empty directory
This commit is contained in:
Pierre du Plessis
2024-01-21 19:13:08 +02:00
committed by GitHub
parent 3bdd6fd072
commit 6f108a4203
4 changed files with 12 additions and 10 deletions

View File

@@ -11,3 +11,4 @@
!testdata/*.txt !testdata/*.txt
!build-static.sh !build-static.sh
!app.tar !app.tar
!app_checksum.txt

0
app_checksum.txt Normal file
View File

View File

@@ -122,6 +122,7 @@ cd ../..
# Embed PHP app, if any # Embed PHP app, if any
if [ -n "${EMBED}" ] && [ -d "${EMBED}" ]; then if [ -n "${EMBED}" ] && [ -d "${EMBED}" ]; then
tar -cf app.tar -C "${EMBED}" . tar -cf app.tar -C "${EMBED}" .
md5 -q app.tar > app_checksum.txt
fi fi
if [ "${os}" = "linux" ]; then if [ "${os}" = "linux" ]; then
@@ -139,6 +140,7 @@ cd ../..
if [ -d "${EMBED}" ]; then if [ -d "${EMBED}" ]; then
truncate -s 0 app.tar truncate -s 0 app.tar
truncate -s 0 app_checksum.txt
fi fi
"dist/${bin}" version "dist/${bin}" version

View File

@@ -3,9 +3,7 @@ package frankenphp
import ( import (
"archive/tar" "archive/tar"
"bytes" "bytes"
"crypto/md5"
_ "embed" _ "embed"
"encoding/hex"
"errors" "errors"
"fmt" "fmt"
"io" "io"
@@ -25,21 +23,22 @@ var EmbeddedAppPath string
//go:embed app.tar //go:embed app.tar
var embeddedApp []byte var embeddedApp []byte
//go:embed app_checksum.txt
var embeddedAppChecksum []byte
func init() { func init() {
if len(embeddedApp) == 0 { if len(embeddedApp) == 0 {
// No embedded app // No embedded app
return return
} }
h := md5.Sum(embeddedApp) appPath := filepath.Join(os.TempDir(), "frankenphp_"+strings.TrimSuffix(string(embeddedAppChecksum[:]), "\n"))
appPath := filepath.Join(os.TempDir(), "frankenphp_"+hex.EncodeToString(h[:]))
if err := os.RemoveAll(appPath); err != nil { if _, err := os.Stat(appPath); os.IsNotExist(err) {
panic(err) if err := untar(appPath); err != nil {
} os.RemoveAll(appPath)
if err := untar(appPath); err != nil { panic(err)
os.RemoveAll(appPath) }
panic(err)
} }
EmbeddedAppPath = appPath EmbeddedAppPath = appPath