From 37e177fbe0a98df19cb19766f4ab1d8be4a58a81 Mon Sep 17 00:00:00 2001 From: Solderpunk Date: Wed, 1 Jul 2020 23:05:49 +0200 Subject: [PATCH] Move .molly file parsing function into config.go. --- config.go | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ handler.go | 61 ----------------------------------------------------- 2 files changed, 62 insertions(+), 61 deletions(-) diff --git a/config.go b/config.go index a3893ae..5058a79 100644 --- a/config.go +++ b/config.go @@ -2,6 +2,8 @@ package main import ( "errors" + "log" + "os" "path/filepath" "github.com/BurntSushi/toml" ) @@ -93,3 +95,63 @@ func getConfig(filename string) (Config, error) { return config, nil } + +func parseMollyFiles(path string, config *Config, errorLog *log.Logger) { + // Build list of directories to check + var dirs []string + dirs = append(dirs, path) + for { + if path == filepath.Clean(config.DocBase) { + break + } + subpath := filepath.Dir(path) + dirs = append(dirs, subpath) + path = subpath + } + // Initialise MollyFile using main Config + var mollyFile MollyFile + mollyFile.GeminiExt = config.GeminiExt + mollyFile.DefaultLang = config.DefaultLang + mollyFile.DirectorySort = config.DirectorySort + mollyFile.DirectoryReverse = config.DirectoryReverse + mollyFile.DirectoryTitles = config.DirectoryTitles + // Parse files in reverse order + for i := len(dirs) - 1; i >= 0; i-- { + dir := dirs[i] + // Break out of the loop if a directory doesn't exist + _, err := os.Stat(dir) + if os.IsNotExist(err) { + break + } + // Construct path for a .molly file in this dir + mollyPath := filepath.Join(dir, ".molly") + _, err = os.Stat(mollyPath) + if err != nil { + continue + } + // If the file exists and we can read it, try to parse it + _, err = toml.DecodeFile(mollyPath, &mollyFile) + if err != nil { + errorLog.Println("Error parsing .molly file " + mollyPath + ": " + err.Error()) + continue + } + // Overwrite main Config using MollyFile + config.GeminiExt = mollyFile.GeminiExt + config.DefaultLang = mollyFile.DefaultLang + config.DirectorySort = mollyFile.DirectorySort + config.DirectoryReverse = mollyFile.DirectoryReverse + config.DirectoryTitles = mollyFile.DirectoryTitles + for key, value := range mollyFile.TempRedirects { + config.TempRedirects[key] = value + } + for key, value := range mollyFile.PermRedirects { + config.PermRedirects[key] = value + } + for key, value := range mollyFile.MimeOverrides { + config.MimeOverrides[key] = value + } + for key, value := range mollyFile.CertificateZones { + config.CertificateZones[key] = value + } + } +} diff --git a/handler.go b/handler.go index 13c9cd9..6db6b64 100644 --- a/handler.go +++ b/handler.go @@ -5,7 +5,6 @@ import ( "crypto/tls" "errors" "fmt" - "github.com/BurntSushi/toml" "io/ioutil" "log" "mime" @@ -180,66 +179,6 @@ func resolvePath(path string, config Config) string { return path } -func parseMollyFiles(path string, config *Config, errorLog *log.Logger) { - // Build list of directories to check - var dirs []string - dirs = append(dirs, path) - for { - if path == filepath.Clean(config.DocBase) { - break - } - subpath := filepath.Dir(path) - dirs = append(dirs, subpath) - path = subpath - } - // Initialise MollyFile using main Config - var mollyFile MollyFile - mollyFile.GeminiExt = config.GeminiExt - mollyFile.DefaultLang = config.DefaultLang - mollyFile.DirectorySort = config.DirectorySort - mollyFile.DirectoryReverse = config.DirectoryReverse - mollyFile.DirectoryTitles = config.DirectoryTitles - // Parse files in reverse order - for i := len(dirs) - 1; i >= 0; i-- { - dir := dirs[i] - // Break out of the loop if a directory doesn't exist - _, err := os.Stat(dir) - if os.IsNotExist(err) { - break - } - // Construct path for a .molly file in this dir - mollyPath := filepath.Join(dir, ".molly") - _, err = os.Stat(mollyPath) - if err != nil { - continue - } - // If the file exists and we can read it, try to parse it - _, err = toml.DecodeFile(mollyPath, &mollyFile) - if err != nil { - errorLog.Println("Error parsing .molly file " + mollyPath + ": " + err.Error()) - continue - } - // Overwrite main Config using MollyFile - config.GeminiExt = mollyFile.GeminiExt - config.DefaultLang = mollyFile.DefaultLang - config.DirectorySort = mollyFile.DirectorySort - config.DirectoryReverse = mollyFile.DirectoryReverse - config.DirectoryTitles = mollyFile.DirectoryTitles - for key, value := range mollyFile.TempRedirects { - config.TempRedirects[key] = value - } - for key, value := range mollyFile.PermRedirects { - config.PermRedirects[key] = value - } - for key, value := range mollyFile.MimeOverrides { - config.MimeOverrides[key] = value - } - for key, value := range mollyFile.CertificateZones { - config.CertificateZones[key] = value - } - } -} - func handleRedirects(URL *url.URL, config Config, conn net.Conn, log *LogEntry, errorLog *log.Logger) { handleRedirectsInner(URL, config.TempRedirects, 30, conn, log, errorLog) handleRedirectsInner(URL, config.PermRedirects, 31, conn, log, errorLog)