Skip to content

Commit

Permalink
Extract Algernon web applications to memory, if possible, ref #132
Browse files Browse the repository at this point in the history
  • Loading branch information
xyproto committed Jun 3, 2023
1 parent 1d4b0f2 commit 43baea0
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
17 changes: 14 additions & 3 deletions engine/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
"github.com/xyproto/algernon/platformdep"
"github.com/xyproto/algernon/utils"
"github.com/xyproto/datablock"
"github.com/xyproto/env"
"github.com/xyproto/env/v2"
"github.com/xyproto/mime"
"github.com/xyproto/pinterface"
"github.com/xyproto/recwatch"
Expand Down Expand Up @@ -448,12 +448,23 @@ func (ac *Config) MustServe(mux *http.ServeMux) error {
}
return nil
case ".zip", ".alg":

// Assume this to be a compressed Algernon application
if extractErr := unzip.Extract(serverFile, ac.serverTempDir); extractErr != nil {
webApplicationExtractionDir := "/dev/shm" // extract to memory, if possible
testfile := filepath.Join(webApplicationExtractionDir, "canary")
if _, err := os.Create(testfile); err == nil { // success
os.Remove(testfile)
} else {
// Could not create the test file
// Use the server temp dir (typically /tmp) instead of /dev/shm
webApplicationExtractionDir = ac.serverTempDir
}
// Extract the web application
if extractErr := unzip.Extract(serverFile, webApplicationExtractionDir); extractErr != nil {
return extractErr
}
// Use the directory where the file was extracted as the server directory
ac.serverDirOrFilename = ac.serverTempDir
ac.serverDirOrFilename = webApplicationExtractionDir
// If there is only one directory there, assume it's the
// directory of the newly extracted ZIP file.
if filenames := utils.GetFilenames(ac.serverDirOrFilename); len(filenames) == 1 {
Expand Down
16 changes: 12 additions & 4 deletions engine/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,14 +243,22 @@ func (ac *Config) FilePage(w http.ResponseWriter, req *http.Request, filename, _

case ".alg":
// Assume this to be a compressed Algernon application
tempdir := ac.serverTempDir
if extractErr := unzip.Extract(filename, tempdir); extractErr == nil { // no error
webApplicationExtractionDir := "/dev/shm" // extract to memory, if possible
testfile := filepath.Join(webApplicationExtractionDir, "canary")
if _, err := os.Create(testfile); err == nil { // success
os.Remove(testfile)
} else {
// Could not create the test file
// Use the server temp dir (typically /tmp) instead of /dev/shm
webApplicationExtractionDir = ac.serverTempDir
}
if extractErr := unzip.Extract(filename, webApplicationExtractionDir); extractErr == nil { // no error
firstname := path.Base(filename)
if strings.HasSuffix(filename, ".alg") {
firstname = path.Base(filename[:len(filename)-4])
}
serveDir := path.Join(tempdir, firstname)
log.Warn(".alg apps must be given as an argument to algernon to be served correctly")
serveDir := path.Join(webApplicationExtractionDir, firstname)
log.Warn(".alg web applications must be given as an argument to algernon to be served correctly")
ac.DirPage(w, req, serveDir, serveDir, ac.defaultTheme)
}
return
Expand Down

0 comments on commit 43baea0

Please sign in to comment.