aboutsummaryrefslogtreecommitdiff
path: root/cmd
diff options
context:
space:
mode:
authorFelix Hanley <felix@userspace.com.au>2020-03-16 11:24:04 +0000
committerFelix Hanley <felix@userspace.com.au>2020-03-16 11:24:04 +0000
commit2248b4d7e1d083a103e94985ee4b373d689ae0e8 (patch)
tree20a3f12a7d793ddadd5225e3e0cb44fa738682a5 /cmd
parentc16f2a68eeb6550743354245f95605a141c1d020 (diff)
downloadsws-2248b4d7e1d083a103e94985ee4b373d689ae0e8.tar.gz
sws-2248b4d7e1d083a103e94985ee4b373d689ae0e8.tar.bz2
Update graph displays and supporting helpers
Diffstat (limited to 'cmd')
-rw-r--r--cmd/server/helpers.go5
-rw-r--r--cmd/server/hits.go3
-rw-r--r--cmd/server/routes.go14
3 files changed, 22 insertions, 0 deletions
diff --git a/cmd/server/helpers.go b/cmd/server/helpers.go
index 01552b2..6c498e5 100644
--- a/cmd/server/helpers.go
+++ b/cmd/server/helpers.go
@@ -3,6 +3,7 @@ package main
import (
"fmt"
"html/template"
+ "math"
"net/http"
"strconv"
"time"
@@ -63,6 +64,10 @@ var funcMap = template.FuncMap{
"percentInv": func(a, b int) float64 {
return 100.0 - ((float64(a) / float64(b)) * 100)
},
+ "round": func(n int, a float64) float64 {
+ n = n * 10
+ return math.Round(a*float64(n)) / float64(n)
+ },
}
func httpError(w http.ResponseWriter, code int, msg string) {
diff --git a/cmd/server/hits.go b/cmd/server/hits.go
index 27bb11d..e5aaf73 100644
--- a/cmd/server/hits.go
+++ b/cmd/server/hits.go
@@ -99,12 +99,15 @@ func verifyHit(db sws.SiteGetter, h *sws.Hit) (*sws.Site, error) {
return nil, err
}
if site.Name == h.Host {
+ debug(h.Host, "equals site name:", site.Name)
return site, nil
}
if strings.Contains(site.Aliases, h.Host) {
+ debug(h.Host, "equals site alias:", site.Name)
return site, nil
}
if site.AcceptSubdomains && strings.HasSuffix(h.Host, site.Name) {
+ debug(h.Host, "is subdomain:", site.Name)
return site, nil
}
return nil, fmt.Errorf("invalid host")
diff --git a/cmd/server/routes.go b/cmd/server/routes.go
index 2b9aaec..5b93713 100644
--- a/cmd/server/routes.go
+++ b/cmd/server/routes.go
@@ -3,6 +3,8 @@ package main
import (
"bytes"
"context"
+ "crypto/sha1"
+ "fmt"
"net/http"
"path/filepath"
"strconv"
@@ -79,11 +81,23 @@ func createRouter(db sws.Store, mmdbPath string) (chi.Router, error) {
r.Post(loginURL, handleLogin(db, rndr))
+ // Static files
r.Get("/*", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
p := strings.TrimPrefix(r.URL.Path, "/")
debug("loading", p)
if b, err := loadTemplate(p); err == nil {
name := filepath.Base(p)
+ etag := fmt.Sprintf(`"%x"`, sha1.Sum(b))
+
+ if match := r.Header.Get("If-None-Match"); match != "" {
+ if strings.Contains(match, etag) {
+ w.WriteHeader(http.StatusNotModified)
+ return
+ }
+ }
+
+ w.Header().Set("Etag", etag)
+ w.Header().Set("Cache-Control", "no-cache")
http.ServeContent(w, r, name, time.Now(), bytes.NewReader(b))
}
}))