From 4b6229b73e35300e17e56eaafa08399c9444f572 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20F=2E=20R=C3=B8dseth?= Date: Sat, 15 Jun 2024 15:58:42 +0200 Subject: [PATCH] Use a const for the default Lua data filename --- engine/config.go | 7 +++---- engine/dirhandler.go | 8 ++++---- engine/handlers.go | 20 +++++++++++--------- engine/servelua.go | 4 ++-- engine/static.go | 4 ++-- 5 files changed, 22 insertions(+), 21 deletions(-) diff --git a/engine/config.go b/engine/config.go index 5bcc3fb0..13919bd7 100644 --- a/engine/config.go +++ b/engine/config.go @@ -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. @@ -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) @@ -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"}, diff --git a/engine/dirhandler.go b/engine/dirhandler.go index c51b88e9..20d1ef0d 100644 --- a/engine/dirhandler.go +++ b/engine/dirhandler.go @@ -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) @@ -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 == "." { diff --git a/engine/handlers.go b/engine/handlers.go index 7a5e7400..286aedd9 100644 --- a/engine/handlers.go +++ b/engine/handlers.go @@ -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) @@ -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) } @@ -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) } @@ -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 @@ -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 @@ -575,7 +577,7 @@ 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 @@ -583,7 +585,7 @@ func (ac *Config) RegisterHandlers(mux *http.ServeMux, handlePath, servedir stri // 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 diff --git a/engine/servelua.go b/engine/servelua.go index 02d33f50..f35589ee 100644 --- a/engine/servelua.go +++ b/engine/servelua.go @@ -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)) @@ -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)) diff --git a/engine/static.go b/engine/static.go index f1549fba..731c5422 100644 --- a/engine/static.go +++ b/engine/static.go @@ -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) }) } } @@ -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)