summary refs log tree commit diff stats
path: root/svc
diff options
context:
space:
mode:
authorBen Morrison <ben@gbmor.dev>2019-06-12 21:11:48 -0400
committerBen Morrison <ben@gbmor.dev>2019-06-12 21:20:04 -0400
commit979e1f93d563637bcca99bcc56a03199807b92d8 (patch)
treede41d25b8cc3ea5f2dbf56be3e593db8e68f37cf /svc
parent0544d1584899c5edc9ac161f90428b6c3febfc94 (diff)
downloadgetwtxt-979e1f93d563637bcca99bcc56a03199807b92d8.tar.gz
separate request and message log
Diffstat (limited to 'svc')
-rw-r--r--svc/cache.go35
-rw-r--r--svc/conf.go34
-rw-r--r--svc/handlers.go14
-rw-r--r--svc/http.go4
-rw-r--r--svc/init_test.go1
5 files changed, 40 insertions, 48 deletions
diff --git a/svc/cache.go b/svc/cache.go
index d3ac872..7bad078 100644
--- a/svc/cache.go
+++ b/svc/cache.go
@@ -41,10 +41,8 @@ type staticAssets struct {
 // file's "Instance" section.
 func initTemplates() *template.Template {
 	confObj.Mu.RLock()
-	assetsDir := confObj.AssetsDir
-	confObj.Mu.RUnlock()
-
-	return template.Must(template.ParseFiles(assetsDir + "/tmpl/index.html"))
+	defer confObj.Mu.RUnlock()
+	return template.Must(template.ParseFiles(confObj.AssetsDir + "/tmpl/index.html"))
 }
 
 func cacheUpdate() {
@@ -73,44 +71,31 @@ func cacheUpdate() {
 // pulled back into memory from disk.
 func pingAssets() {
 	confObj.Mu.RLock()
-	assetsDir := confObj.AssetsDir
-	confObj.Mu.RUnlock()
+	defer confObj.Mu.RUnlock()
+	staticCache.mu.Lock()
+	defer staticCache.mu.Unlock()
 
-	cssStat, err := os.Stat(assetsDir + "/style.css")
+	cssStat, err := os.Stat(confObj.AssetsDir + "/style.css")
 	errLog("", err)
-
-	indexStat, err := os.Stat(assetsDir + "/tmpl/index.html")
+	indexStat, err := os.Stat(confObj.AssetsDir + "/tmpl/index.html")
 	errLog("", err)
 
-	staticCache.mu.RLock()
-	indexMod := staticCache.indexMod
-	cssMod := staticCache.cssMod
-	staticCache.mu.RUnlock()
-
-	if !indexMod.Equal(indexStat.ModTime()) {
+	if !staticCache.indexMod.Equal(indexStat.ModTime()) {
 		tmpls = initTemplates()
 
 		var b []byte
 		buf := bytes.NewBuffer(b)
-
-		confObj.Mu.RLock()
 		errLog("", tmpls.ExecuteTemplate(buf, "index.html", confObj.Instance))
-		confObj.Mu.RUnlock()
 
-		staticCache.mu.Lock()
 		staticCache.index = buf.Bytes()
 		staticCache.indexMod = indexStat.ModTime()
-		staticCache.mu.Unlock()
 	}
 
-	if !cssMod.Equal(cssStat.ModTime()) {
-
-		css, err := ioutil.ReadFile(assetsDir + "/style.css")
+	if !staticCache.cssMod.Equal(cssStat.ModTime()) {
+		css, err := ioutil.ReadFile(confObj.AssetsDir + "/style.css")
 		errLog("", err)
 
-		staticCache.mu.Lock()
 		staticCache.css = css
 		staticCache.cssMod = cssStat.ModTime()
-		staticCache.mu.Unlock()
 	}
 }
diff --git a/svc/conf.go b/svc/conf.go
index 538b701..c1b9abb 100644
--- a/svc/conf.go
+++ b/svc/conf.go
@@ -11,13 +11,16 @@ import (
 	"github.com/spf13/viper"
onsole
   ( ) #38  10/01/16  searching in pager
   (X) #39  10/01/17  flushinput not always good
   (X) #42  10/01/17  memorize directory for `` when using :cd
   (X) #43  10/01/18  internally treat the bookmarks ` and ' the same
   ( ) #44  10/01/18  more error messages :P
   (X) #47  10/01/19  less restricive auto preview
   (X) #48  10/01/19  abbreviate commands with first unambiguous substring
   ( ) #50  10/01/19  add more unit tests
   ( ) #51  10/01/21  remove directory.marked_items ?
   (X) #55  10/01/24  allow change of filename when pasting
          you're given the choice between overwriting or appending a "_"
   ( ) #56  10/01/30  warn before deleting mount points
   ( ) #57  10/01/30  warn before deleting unseen marked files
   (X) #58  10/02/04  change the title of the terminal
   (X) #61  10/02/09  show sum of size of marked files


Bugs

   (X) #17  10/01/01  why do bookmarks disappear sometimes?
   (X) #18  10/01/01  fix notify widget (by adding a LogView?)
   (X) #19  10/01/01  resizing after pressing g
   (X) #23  10/01/04  stop dir loading with ^C -> wont load anymore
   (X) #25  10/01/06  directories sometimes dont reload correctly
   (X) #26  10/01/06  :delete on symlinks of directories fails
   (X) #31  10/01/06  ^C breaks cd-after-exit by stopping sourced shell script
   ( ) #40  10/01/17  freeze with unavailable sshfs
   (X) #41  10/01/17  capital file extensions are not recognized
   (X) #46  10/01/19  old username displayed after using su
   (X) #49  10/01/19  fix unit tests :'(
   ( ) #52  10/01/23  special characters in tab completion
   (X) #54  10/01/23  max_dirsize_for_autopreview not working
   ( ) #60  10/02/05  utf support improvable


Ideas

   ( ) #20  10/01/01  use inotify to monitor filesystem changes
   ( ) #24  10/01/06  progress bar
   (X) #27  10/01/06  hide bookmarks in list which contain hidden dir
   (X) #28  10/01/06  use regexp instead of string for searching
   ( ) #33  10/01/08  accelerate mousewheel speed
   ( ) #45  10/01/18  hooks for events like setting changes
   ( ) #53  10/01/23  merge fm and environment


Goals for next minor version

   (X) #54  10/01/23  max_dirsize_for_autopreview not working
   (X) #55  10/01/24  allow change of filename when pasting
          you're given the choice between overwriting or appending a "_"
   (X) #61  10/02/09  show sum of size of marked files
loaded. - go func(logfile *os.File) { + go func(msg *os.File, req *os.File) { <-closeLog - log.Printf("Closing log file ...\n\n") - errLog("Could not close log file: ", logfile.Close()) - }(logfile) + log.Printf("Closing log files ...\n\n") + errLog("Could not close log file: ", msg.Close()) + errLog("Could not close log file: ", req.Close()) + }(msgLog, reqLogFile) - log.SetOutput(logfile) + log.SetOutput(msgLog) + reqLog = log.New(reqLogFile, "", log.LstdFlags) } confObj.Mu.RUnlock() } @@ -102,7 +111,8 @@ func setConfigDefaults() { viper.SetDefault("TLSCert", "cert.pem") viper.SetDefault("TLSKey", "key.pem") viper.SetDefault("ListenPort", 9001) - viper.SetDefault("LogFile", "getwtxt.log") + viper.SetDefault("MessageLog", "logs/message.log") + viper.SetDefault("RequestLog", "logs/request.log") viper.SetDefault("DatabasePath", "getwtxt.db") viper.SetDefault("AssetsDirectory", "assets") viper.SetDefault("DatabaseType", "leveldb") @@ -148,7 +158,8 @@ func bindConfig() { confObj.IsProxied = viper.GetBool("BehindProxy") confObj.Port = viper.GetInt("ListenPort") - confObj.LogFile = viper.GetString("LogFile") + confObj.MsgLog = viper.GetString("MessageLog") + confObj.ReqLog = viper.GetString("RequestLog") confObj.DBType = strings.ToLower(viper.GetString("DatabaseType")) confObj.DBPath = viper.GetString("DatabasePath") confObj.AssetsDir = viper.GetString("AssetsDirectory") @@ -199,7 +210,8 @@ func announceConfig() { if confObj.StdoutLogging { log.Printf("Logging to: stdout\n") } else { - log.Printf("Logging to: %v\n", confObj.LogFile) + log.Printf("Logging messages to: %v\n", confObj.MsgLog) + log.Printf("Logging requests to: %v\n", confObj.ReqLog) } log.Printf("Using %v database: %v\n", confObj.DBType, confObj.DBPath) log.Printf("Database push interval: %v\n", confObj.DBInterval) diff --git a/svc/handlers.go b/svc/handlers.go index def01ad..924b487 100644 --- a/svc/handlers.go +++ b/svc/handlers.go @@ -42,12 +42,10 @@ func servStatic(w http.ResponseWriter, isCSS bool) error { // handles "/" and "/css" func staticHandler(w http.ResponseWriter, r *http.Request) { isCSS := strings.Contains(r.URL.Path, "css") - if err := servStatic(w, isCSS); err != nil { errHTTP(w, r, err, http.StatusInternalServerError) return } - log200(r) } @@ -69,17 +67,15 @@ func apiAllTweetsHandler(w http.ResponseWriter, r *http.Request) { } data := parseQueryOut(out) - etag := fmt.Sprintf("%x", sha256.Sum256(data)) + w.Header().Set("ETag", etag) w.Header().Set("Content-Type", txtutf8) - _, err = w.Write(data) if err != nil { errHTTP(w, r, err, http.StatusInternalServerError) return } - log200(r) } @@ -134,8 +130,8 @@ func apiEndpointHandler(w http.ResponseWriter, r *http.Request) { errLog("", err) data := parseQueryOut(out) - etag := fmt.Sprintf("%x", sha256.Sum256(data)) + w.Header().Set("ETag", etag) w.Header().Set("Content-Type", txtutf8) @@ -162,8 +158,8 @@ func apiTagsBaseHandler(w http.ResponseWriter, r *http.Request) { out = registry.ReduceToPage(1, out) data := parseQueryOut(out) - etag := fmt.Sprintf("%x", sha256.Sum256(data)) + w.Header().Set("ETag", etag) w.Header().Set("Content-Type", txtutf8) @@ -172,7 +168,6 @@ func apiTagsBaseHandler(w http.ResponseWriter, r *http.Request) { errHTTP(w, r, err, http.StatusInternalServerError) return } - log200(r) } @@ -184,8 +179,8 @@ func apiTagsHandler(w http.ResponseWriter, r *http.Request) { out := compositeStatusQuery("#"+tags, r) out = registry.ReduceToPage(1, out) data := parseQueryOut(out) - etag := fmt.Sprintf("%x", sha256.Sum256(data)) + w.Header().Set("ETag", etag) w.Header().Set("Content-Type", txtutf8) @@ -194,6 +189,5 @@ func apiTagsHandler(w http.ResponseWriter, r *http.Request) { errHTTP(w, r, err, http.StatusInternalServerError) return } - log200(r) } diff --git a/svc/http.go b/svc/http.go index d39ab4e..95cb613 100644 --- a/svc/http.go +++ b/svc/http.go @@ -62,12 +62,12 @@ func ipMiddleware(hop http.Handler) http.Handler { func log200(r *http.Request) { useragent := r.Header["User-Agent"] uip := getIPFromCtx(r.Context()) - log.Printf("*** %v :: 200 :: %v %v :: %v\n", uip, r.Method, r.URL, useragent) + reqLog.Printf("*** %v :: 200 :: %v %v :: %v\n", uip, r.Method, r.URL, useragent) } func errHTTP(w http.ResponseWriter, r *http.Request, err error, code int) { useragent := r.Header["User-Agent"] uip := getIPFromCtx(r.Context()) - log.Printf("*** %v :: %v :: %v %v :: %v :: %v\n", uip, code, r.Method, r.URL, useragent, err.Error()) + reqLog.Printf("*** %v :: %v :: %v %v :: %v :: %v\n", uip, code, r.Method, r.URL, useragent, err.Error()) http.Error(w, fmt.Sprintf("Error %v: %v", code, err.Error()), code) } diff --git a/svc/init_test.go b/svc/init_test.go index 0c417b4..bc77ddc 100644 --- a/svc/init_test.go +++ b/svc/init_test.go @@ -53,6 +53,7 @@ func logToNull() { log.Printf("%v\n", err) } log.SetOutput(hush) + reqLog = log.New(hush, "", log.LstdFlags) } func testConfig() {