diff options
author | Ben Morrison <ben@gbmor.dev> | 2019-06-11 20:02:45 -0400 |
---|---|---|
committer | Ben Morrison <ben@gbmor.dev> | 2019-06-11 20:02:45 -0400 |
commit | adee7fa6333265028f67d7b8bb66ddc3ceef1d05 (patch) | |
tree | d6aeda5660263e27cf42e6180f65e9817a7754a8 /svc | |
parent | 3da400b21057054493feac798111cbbb3ed307b8 (diff) | |
download | getwtxt-adee7fa6333265028f67d7b8bb66ddc3ceef1d05.tar.gz |
runtime bugs in staticHandler
Diffstat (limited to 'svc')
-rw-r--r-- | svc/handlers.go | 31 |
1 files changed, 12 insertions, 19 deletions
diff --git a/svc/handlers.go b/svc/handlers.go index 38c8bfa..9ab9cdb 100644 --- a/svc/handlers.go +++ b/svc/handlers.go @@ -5,6 +5,7 @@ import ( "fmt" "net/http" "strconv" + "strings" "time" "github.com/getwtxt/registry" @@ -17,16 +18,19 @@ func getEtag(modtime time.Time) string { return fmt.Sprintf("%x", sha256.Sum256(shabytes)) } -func sendStaticEtag(w http.ResponseWriter, isCSS bool) { +func servStatic(w http.ResponseWriter, isCSS bool) error { if isCSS { etag := getEtag(staticCache.cssMod) w.Header().Set("ETag", "\""+etag+"\"") w.Header().Set("Content-Type", cssutf8) - return + _, err := w.Write(staticCache.css) + return err } etag := getEtag(staticCache.indexMod) w.Header().Set("ETag", "\""+etag+"\"") w.Header().Set("Content-Type", htmlutf8) + _, err := w.Write(staticCache.index) + return err } // handles "/" and "/css" @@ -37,23 +41,12 @@ func staticHandler(w http.ResponseWriter, r *http.Request) { // to send as an ETag. If an error occurred when grabbing the file info, // the ETag will be empty. staticCache.mu.RLock() - switch r.URL.Path { - case "/css": - sendStaticEtag(w, true) - _, err := w.Write(staticCache.css) - if err != nil { - staticCache.mu.RUnlock() - errHTTP(w, r, err, http.StatusInternalServerError) - return - } - default: - sendStaticEtag(w, false) - _, err := w.Write(staticCache.index) - if err != nil { - staticCache.mu.RUnlock() - errHTTP(w, r, err, http.StatusInternalServerError) - return - } + isCSS := strings.Contains(r.URL.Path, "css") + err := servStatic(w, isCSS) + if err != nil { + staticCache.mu.RUnlock() + errHTTP(w, r, err, http.StatusInternalServerError) + return } staticCache.mu.RUnlock() |