From 980badbe267476be626d35a736576d5d3b840645 Mon Sep 17 00:00:00 2001 From: Raggaer Date: Sat, 22 Dec 2018 18:34:54 +0100 Subject: [PATCH] Use 404 page for not found subtopic pages --- app/controllers/lua.go | 10 +++++++--- app/lua/state.go | 11 +++++++++++ 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/app/controllers/lua.go b/app/controllers/lua.go index 8b48dd3e..8662a2f4 100644 --- a/app/controllers/lua.go +++ b/app/controllers/lua.go @@ -146,10 +146,14 @@ func LuaPage(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { lua.SetI18nUserData(s, language) // Retrieve compiled proto - proto, err := lua.CompiledPageList.Get(filepath.Join("pages", pageName, r.Method+".lua")) + protoPath := filepath.Join("pages", pageName, r.Method+".lua") + if !lua.CompiledPageList.Exists(protoPath) { + protoPath = filepath.Join("pages", "404", r.Method+".lua") + } + proto, err := lua.CompiledPageList.Get(protoPath) if err != nil { w.WriteHeader(404) - util.Logger.Logger.Errorf("Cannot find lua proto, subtopic source: %v", pageName, err) + util.Logger.Logger.Errorf("Cannot find lua proto, subtopic source (%s) %v", pageName, err) return } @@ -159,7 +163,7 @@ func LuaPage(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { proto, ); err != nil { w.WriteHeader(404) - util.Logger.Logger.Errorf("Cannot get %v subtopic source: %v", pageName, err) + util.Logger.Logger.Errorf("Cannot get %v subtopic source (%s) %v", pageName, err) return } diff --git a/app/lua/state.go b/app/lua/state.go index 7ef5bc4d..fe13badf 100644 --- a/app/lua/state.go +++ b/app/lua/state.go @@ -45,6 +45,17 @@ type stateList struct { Type string } +// Exists checks if a proto path exists +func (s *compiledStateList) Exists(path string) bool { + path = strings.ToLower(path) + for p, _ := range s.List { + if strings.ToLower(p) == path { + return true + } + } + return false +} + // CompileFiles compiles all lua files into function protos func (s *compiledStateList) CompileFiles(dir string) error { s.rw.Lock()