Skip to content

Commit

Permalink
Use a const for the default Lua data filename
Browse files Browse the repository at this point in the history
  • Loading branch information
xyproto committed Jun 15, 2024
1 parent 5c624db commit 4b6229b
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 21 deletions.
7 changes: 3 additions & 4 deletions engine/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ import (
const (
// Version number. Stable API within major version numbers.
Version = 2.0

// The default supporting filename for a Lua script that provides data to a template
defaultLuaDataFilename = "data.lua"
)

// Config is the main structure for the Algernon server.
Expand All @@ -60,7 +63,6 @@ type Config struct {
limitRequestsString string // store the request limit as a string for faster HTTP header creation later on
defaultBoltFilename string // default bolt database file, for some operating systems
defaultLogFile string // default log file, for some operating systems
defaultLuaDataFilename string // default filename for a Lua script that provides data to a template
defaultRedisColonPort string
serverDirOrFilename string // exposed to the server configuration scripts(s)
serverAddr string // exposed to the server configuration scripts(s)
Expand Down Expand Up @@ -183,9 +185,6 @@ func New(versionString, description string) (*Config, error) {
// Default log file, for some operating systems
defaultLogFile: filepath.Join(tmpdir, "algernon.log"),

// Default filename for a Lua script that provides data to a template
defaultLuaDataFilename: "data.lua",

// List of configuration filenames to check
serverConfigurationFilenames: []string{"/etc/algernon/serverconf.lua", "/etc/algernon/server.lua"},

Expand Down
8 changes: 4 additions & 4 deletions engine/dirhandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func (ac *Config) DirectoryListing(w http.ResponseWriter, req *http.Request, roo
// The directory must exist.
// rootdir is the base directory (can be ".")
// dirname is the specific directory that is to be served (should never be ".")
func (ac *Config) DirPage(w http.ResponseWriter, req *http.Request, rootdir, dirname, theme string) {
func (ac *Config) DirPage(w http.ResponseWriter, req *http.Request, rootdir, dirname, theme, luaDataFilename string) {
// Check if we are instructed to quit after serving the first file
if ac.quitAfterFirstRequest {
go ac.quitSoon("Quit after first request", defaultSoonDuration)
Expand All @@ -158,18 +158,18 @@ func (ac *Config) DirPage(w http.ResponseWriter, req *http.Request, rootdir, dir
for _, indexfile := range indexFilenames {
filename = filepath.Join(dirname, indexfile)
if ac.fs.Exists(filename) {
ac.FilePage(w, req, filename, ac.defaultLuaDataFilename)
ac.FilePage(w, req, filename, luaDataFilename)
return
}
}

// Serve handler.lua, if found in ancestors
// Serve handler.lua, if found in parent directories
var ancestor string
ancestor = filepath.Dir(dirname)
for x := 0; x < 100; x++ { // a maximum of 100 directories deep
filename = filepath.Join(ancestor, "handler.lua")
if ac.fs.Exists(filename) {
ac.FilePage(w, req, filename, ac.defaultLuaDataFilename)
ac.FilePage(w, req, filename, luaDataFilename)
return
}
if ancestor == "." {
Expand Down
20 changes: 11 additions & 9 deletions engine/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,11 @@ func (ac *Config) PongoHandler(w http.ResponseWriter, req *http.Request, filenam

// Make the functions in luaDataFilename available for the Pongo2 template

luafilename := filepath.Join(filepath.Dir(filename), ac.defaultLuaDataFilename)
if ac.fs.Exists(ac.defaultLuaDataFilename) {
luafilename = ac.defaultLuaDataFilename
luafilename := defaultLuaDataFilename
if !ac.fs.Exists(luafilename) {
luafilename = filepath.Join(filepath.Dir(filename), defaultLuaDataFilename)
}

if ac.fs.Exists(luafilename) {
// Extract the function map from luaDataFilenname in a goroutine
errChan := make(chan error)
Expand Down Expand Up @@ -99,7 +100,8 @@ func (ac *Config) PongoHandler(w http.ResponseWriter, req *http.Request, filenam
}

// Output a warning if something different from default has been given
if !strings.HasSuffix(luafilename, ac.defaultLuaDataFilename) {
// TODO: Do not only check for a suffix, check for the filename
if !strings.HasSuffix(luafilename, defaultLuaDataFilename) {
log.Warn("Could not read ", luafilename)
}

Expand All @@ -124,7 +126,7 @@ func (ac *Config) ReadAndLogErrors(w http.ResponseWriter, filename, ext string)
}

// FilePage tries to serve a single file. The file must exist. Must be given a full filename.
func (ac *Config) FilePage(w http.ResponseWriter, req *http.Request, filename, _ string) {
func (ac *Config) FilePage(w http.ResponseWriter, req *http.Request, filename, luaDataFilename string) {
if ac.quitAfterFirstRequest {
go ac.quitSoon("Quit after first request", defaultSoonDuration)
}
Expand Down Expand Up @@ -199,7 +201,7 @@ func (ac *Config) FilePage(w http.ResponseWriter, req *http.Request, filename, _
}

// Try reading luaDataFilename as well, if possible
luafilename := filepath.Join(filepath.Dir(filename), ac.defaultLuaDataFilename)
luafilename := filepath.Join(filepath.Dir(filename), luaDataFilename)
luablock, err := ac.cache.Read(luafilename, ac.shouldCache(ext))
if err != nil {
// Could not find and/or read luaDataFilename
Expand Down Expand Up @@ -262,7 +264,7 @@ func (ac *Config) FilePage(w http.ResponseWriter, req *http.Request, filename, _
}
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)
ac.DirPage(w, req, serveDir, serveDir, ac.defaultTheme, luaDataFilename)
}
return

Expand Down Expand Up @@ -575,15 +577,15 @@ func (ac *Config) RegisterHandlers(mux *http.ServeMux, handlePath, servedir stri
// Prepare to count bytes written
sc := sheepcounter.New(w)
// Get the directory page
ac.DirPage(sc, req, servedir, dirname, theme)
ac.DirPage(sc, req, servedir, dirname, theme, defaultLuaDataFilename)
// Log the access
ac.LogAccess(req, http.StatusOK, sc.Counter())
return
} else if !hasdir && hasfile {
// Prepare to count bytes written
sc := sheepcounter.New(w)
// Share a single file instead of a directory
ac.FilePage(sc, req, noslash, ac.defaultLuaDataFilename)
ac.FilePage(sc, req, noslash, defaultLuaDataFilename)
// Log the access
ac.LogAccess(req, http.StatusOK, sc.Counter())
return
Expand Down
4 changes: 2 additions & 2 deletions engine/servelua.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func (ac *Config) LoadServeFile(w http.ResponseWriter, req *http.Request, L *lua
L.SetGlobal("serve", L.NewFunction(func(L *lua.LState) int {
scriptdir := filepath.Dir(filename)
serveFilename := filepath.Join(scriptdir, L.ToString(1))
dataFilename := filepath.Join(scriptdir, ac.defaultLuaDataFilename)
dataFilename := filepath.Join(scriptdir, defaultLuaDataFilename)
if L.GetTop() >= 2 {
// Optional argument for using a different file than "data.lua"
dataFilename = filepath.Join(scriptdir, L.ToString(2))
Expand Down Expand Up @@ -115,7 +115,7 @@ func (ac *Config) LoadServeFile(w http.ResponseWriter, req *http.Request, L *lua
L.SetGlobal("render", L.NewFunction(func(L *lua.LState) int {
scriptdir := filepath.Dir(filename)
serveFilename := filepath.Join(scriptdir, L.ToString(1))
dataFilename := filepath.Join(scriptdir, ac.defaultLuaDataFilename)
dataFilename := filepath.Join(scriptdir, defaultLuaDataFilename)
if L.GetTop() >= 2 {
// Optional argument for using a different file than "data.lua"
dataFilename = filepath.Join(scriptdir, L.ToString(2))
Expand Down
4 changes: 2 additions & 2 deletions engine/static.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func (ac *Config) ServeStaticFile(filename, colonPort string) error {
for _, localImage := range localImages {
mux.HandleFunc("/"+localImage, func(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Server", ac.versionString)
ac.FilePage(w, req, localImage, ac.defaultLuaDataFilename)
ac.FilePage(w, req, localImage, defaultLuaDataFilename)
})
}
}
Expand All @@ -111,7 +111,7 @@ func (ac *Config) ServeStaticFile(filename, colonPort string) error {

mux.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Server", ac.versionString)
ac.FilePage(w, req, filename, ac.defaultLuaDataFilename)
ac.FilePage(w, req, filename, defaultLuaDataFilename)
})

HTTPserver := ac.NewGracefulServer(mux, false, ac.serverHost+colonPort)
Expand Down

0 comments on commit 4b6229b

Please sign in to comment.