diff --git a/handlers.go b/handlers.go index c7a349b..bf8779c 100644 --- a/handlers.go +++ b/handlers.go @@ -67,9 +67,13 @@ func indexHandler(w http.ResponseWriter, r *http.Request) { // This is due to the default behavior of // not serving naked paths but virtual ones. func iconHandler(w http.ResponseWriter, r *http.Request) { + confVars.mu.RLock() + assetsDir := confVars.assetsDir + iconPath := confVars.iconPath + confVars.mu.RUnlock() // read the raw bytes of the image - longname := confVars.assetsDir + "/" + confVars.iconPath + longname := assetsDir + "/" + iconPath icon, err := ioutil.ReadFile(longname) if err != nil { if os.IsNotExist(err) { @@ -108,16 +112,20 @@ func iconHandler(w http.ResponseWriter, r *http.Request) { // not serving naked paths but virtual ones. func cssHandler(w http.ResponseWriter, r *http.Request) { + confVars.mu.RLock() + cssPath := confVars.cssPath + confVars.mu.RUnlock() + // check if using local or remote CSS. // if remote, don't bother doing anything // and redirect requests to / - if !cssLocal([]byte(confVars.cssPath)) { + if !cssLocal([]byte(cssPath)) { http.Redirect(w, r, "/", http.StatusFound) return } // read the raw bytes of the stylesheet - css, err := ioutil.ReadFile(confVars.cssPath) + css, err := ioutil.ReadFile(cssPath) if err != nil { if os.IsNotExist(err) { log.Printf("CSS file specified in config does not exist: /css request 404\n") @@ -130,7 +138,7 @@ func cssHandler(w http.ResponseWriter, r *http.Request) { } // stat to get the mod time for the etag header - stat, err := os.Stat(confVars.cssPath) + stat, err := os.Stat(cssPath) if err != nil { log.Printf("Couldn't stat CSS file to send ETag header: %v\n", err) } @@ -168,7 +176,9 @@ func validatePath(fn func(http.ResponseWriter, *http.Request, string)) http.Hand // if the markdown doc can't be read, default to // net/http's error handling func error500(w http.ResponseWriter, _ *http.Request) { + confVars.mu.RLock() e500 := confVars.assetsDir + "/500.md" + confVars.mu.RUnlock() file, err := ioutil.ReadFile(e500) if err != nil { @@ -190,7 +200,9 @@ func error500(w http.ResponseWriter, _ *http.Request) { // if the markdown doc can't be read, default to // net/http's error handling func error404(w http.ResponseWriter, r *http.Request) { + confVars.mu.RLock() e404 := confVars.assetsDir + "/404.md" + confVars.mu.RUnlock() file, err := ioutil.ReadFile(e404) if err != nil { diff --git a/handlers_test.go b/handlers_test.go index 4a19fa7..e686bff 100644 --- a/handlers_test.go +++ b/handlers_test.go @@ -68,7 +68,11 @@ func Test_indexHandler(t *testing.T) { func Test_iconHandler(t *testing.T) { name := "Icon Handler Test" initConfigParams() + + confVars.mu.RLock() icon, _ := ioutil.ReadFile(confVars.assetsDir + "/" + confVars.iconPath) + confVars.mu.RUnlock() + w := httptest.NewRecorder() r := httptest.NewRequest("GET", "localhost:8080/icon", nil) t.Run(name, func(t *testing.T) { diff --git a/init.go b/init.go index 47efe61..9fb145c 100644 --- a/init.go +++ b/init.go @@ -15,8 +15,13 @@ func init() { // set up logging if the config file params // are set - if confVars.fileLogging && !confVars.quietLogging { - if llogfile, err := os.OpenFile(confVars.logFile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0600); err == nil { + confVars.mu.RLock() + filog := confVars.fileLogging + qlog := confVars.quietLogging + logfi := confVars.logFile + confVars.mu.RUnlock() + if filog && !qlog { + if llogfile, err := os.OpenFile(logfi, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0600); err == nil { log.SetOutput(llogfile) go func() { @@ -34,7 +39,7 @@ func init() { } // Tell TildeWiki to be quiet, - if confVars.quietLogging { + if qlog { if llogfile, err := os.Open("/dev/null"); err == nil { log.SetOutput(llogfile) diff --git a/main.go b/main.go index 829d8dc..2613309 100644 --- a/main.go +++ b/main.go @@ -19,6 +19,13 @@ var closelog = make(chan bool, 1) func main() { + confVars.mu.RLock() + filog := confVars.fileLogging + qlog := confVars.quietLogging + reversed := confVars.reverseTally + portnum := confVars.port + confVars.mu.RUnlock() + // watch for SIGINT aka ^C // close the log file then exit c := make(chan os.Signal, 1) @@ -28,7 +35,7 @@ func main() { for sigint := range c { log.Printf("\n\nCaught %v. Cleaning up ...\n", sigint) - if confVars.fileLogging { + if filog { // signal to close the log file closelog <- true time.Sleep(50 * time.Millisecond) @@ -52,14 +59,13 @@ func main() { serv.HandleFunc("/500", error500) serv.HandleFunc("/404", error404) - log.Println("**NOTICE** Binding to " + confVars.port) - // let the user know if using reversed page listings - if confVars.reverseTally { + if reversed { log.Printf("**NOTICE** Using reversed page listings on index ... \n") } - portnum := confVars.port + log.Println("**NOTICE** Binding to " + portnum) + server := &http.Server{ Handler: handlers.CompressHandler(ipMiddleware(serv)), Addr: portnum, @@ -73,7 +79,7 @@ func main() { } // signal to close the log file - if confVars.fileLogging || confVars.quietLogging { + if filog || qlog { closelog <- true close(closelog) } diff --git a/md.go b/md.go index 1a3786f..74d6a9b 100644 --- a/md.go +++ b/md.go @@ -8,9 +8,11 @@ import ( func setupMarkdown(css, title string) *bf.HTMLRenderer { // if using local CSS file, use the virtually-served css // path rather than the actual file name + confVars.mu.RLock() if cssLocal([]byte(confVars.cssPath)) { css = "/css" } + confVars.mu.RUnlock() // return the parameters used for the rendering // of markdown to html. @@ -30,5 +32,8 @@ func setupMarkdown(css, title string) *bf.HTMLRenderer { // Wrapper function to generate the parameters above and // pass them to the blackfriday library's parsing function func render(data []byte, title string) []byte { - return bf.Run(data, bf.WithRenderer(setupMarkdown(confVars.cssPath, title))) + confVars.mu.RLock() + cssPath := confVars.cssPath + confVars.mu.RUnlock() + return bf.Run(data, bf.WithRenderer(setupMarkdown(cssPath, title))) } diff --git a/pages.go b/pages.go index 2c22637..2b6439e 100644 --- a/pages.go +++ b/pages.go @@ -56,14 +56,18 @@ func buildPage(filename string) (*Page, error) { title = shortname } if desc != "" { + confVars.mu.RLock() desc = confVars.descSep + " " + desc + confVars.mu.RUnlock() } if author != "" { author = "`by " + author + "`" } // longtitle is used in the