aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFelix Hanley <felix@userspace.com.au>2020-03-09 11:52:37 +0000
committerFelix Hanley <felix@userspace.com.au>2020-03-09 11:52:37 +0000
commitd333ad876ea06ae01ecf646424f9b0f02ad84419 (patch)
tree880391c2453c077a0635bc16784a63de0b3d30ab
parent88ead9d41072f3bec5b772b4d2fb0070995c09e8 (diff)
downloadsws-d333ad876ea06ae01ecf646424f9b0f02ad84419.tar.gz
sws-d333ad876ea06ae01ecf646424f9b0f02ad84419.tar.bz2
Fix overrides and start on styling
-rw-r--r--browser_set.go4
-rw-r--r--cmd/server/flags.go35
-rw-r--r--cmd/server/handlers.go4
-rw-r--r--cmd/server/main.go46
-rw-r--r--cmd/server/routes.go10
-rw-r--r--referrer.go4
-rw-r--r--static/default.css2
-rw-r--r--tmpl/layouts/base.tmpl4
-rw-r--r--tmpl/navbar.tmpl2
-rw-r--r--tmpl/site.tmpl83
-rw-r--r--tmpl/timerange.tmpl74
11 files changed, 141 insertions, 127 deletions
diff --git a/browser_set.go b/browser_set.go
index fdcdfc7..b53ddb7 100644
--- a/browser_set.go
+++ b/browser_set.go
@@ -63,6 +63,10 @@ func (b Browser) Count() int {
return b.hitSet.Count()
}
+func (bs BrowserSet) Count() int {
+ return len(bs)
+}
+
func (bs BrowserSet) YMax() int {
max := 0
for _, b := range bs {
diff --git a/cmd/server/flags.go b/cmd/server/flags.go
deleted file mode 100644
index b64bed1..0000000
--- a/cmd/server/flags.go
+++ /dev/null
@@ -1,35 +0,0 @@
-package main
-
-import (
- "flag"
- "os"
-)
-
-func stringFlag(long, short, def, envvar, desc string) *string {
- if envvar != "" {
- if v := os.Getenv(envvar); v != "" {
- def = v
- }
- }
- out := flag.String(long, def, desc)
- if short != "" {
- flag.StringVar(out, short, def, desc)
- }
- if out != nil && *out == "" {
- return nil
- }
- return out
-}
-
-func boolFlag(long, short string, def bool, envvar, desc string) *bool {
- if envvar != "" {
- if v := os.Getenv(envvar); v != "" {
- def = true
- }
- }
- out := flag.Bool(long, def, desc)
- if short != "" {
- flag.BoolVar(out, short, def, desc)
- }
- return out
-}
diff --git a/cmd/server/handlers.go b/cmd/server/handlers.go
index d13109d..0264726 100644
--- a/cmd/server/handlers.go
+++ b/cmd/server/handlers.go
@@ -26,8 +26,8 @@ type templateData struct {
func newTemplateData(r *http.Request) *templateData {
out := &templateData{
- Payload: "//" + *domain + "/sws.js",
- Endpoint: "//" + *domain + "/sws.gif",
+ Payload: "//" + domain + "/sws.js",
+ Endpoint: "//" + domain + "/sws.gif",
}
if r != nil {
flashes := flashGet(r)
diff --git a/cmd/server/main.go b/cmd/server/main.go
index 309e11b..c831031 100644
--- a/cmd/server/main.go
+++ b/cmd/server/main.go
@@ -23,23 +23,23 @@ var (
// Flags
var (
- verbose *bool
- addr *string
- dsn *string
- domain *string
- logFile *string
- override *string
- noMigrate *bool
+ verbose bool
+ addr string
+ dsn string
+ domain string
+ logFile string
+ override string
+ noMigrate bool
)
func init() {
- verbose = boolFlag("verbose", "v", false, "VERBOSE", "enable verbose output")
- addr = stringFlag("listen", "l", "localhost:5000", "LISTEN", "listen address")
- dsn = stringFlag("dsn", "", "file:sws.db?cache=shared", "DSN", "database password")
- domain = stringFlag("domain", "", "stats.userspace.com.au", "DOMAIN", "stats domain")
- logFile = stringFlag("log", "", "", "LOGFILE", "log to file")
- override = stringFlag("override", "", "", "OVERRIDE", "override path")
- noMigrate = boolFlag("no-migrate", "m", false, "NOMIGRATE", "disable migrations")
+ flag.BoolVar(&verbose, "verbose", false, "enable verbose output")
+ flag.StringVar(&addr, "listen", "localhost:5000", "listen address")
+ flag.StringVar(&dsn, "dsn", "file:sws.db?cache=shared", "database password")
+ flag.StringVar(&domain, "domain", "stats.userspace.com.au", "stats domain")
+ flag.StringVar(&logFile, "l", "", "log to file")
+ flag.StringVar(&override, "override", "", "override path")
+ flag.BoolVar(&noMigrate, "no-migrate", false, "disable migrations")
// Default to no log
log = func(v ...interface{}) {}
@@ -56,14 +56,14 @@ func main() {
flag.Parse()
var output io.Writer = os.Stdout
- if logFile != nil && *logFile != "" {
- if output, err = os.Create(*logFile); err != nil {
+ if logFile != "" {
+ if output, err = os.Create(logFile); err != nil {
fmt.Fprintf(os.Stderr, "failed to open log file: %s", err)
os.Exit(1)
}
}
- if *verbose {
+ if verbose {
log = func(v ...interface{}) {
fmt.Fprintf(output, "[%s] ", time.Now().Format(time.RFC3339))
fmt.Fprintln(output, v...)
@@ -77,13 +77,13 @@ func main() {
}
log("version", Version)
- driver := strings.SplitN(*dsn, ":", 2)[0]
+ driver := strings.SplitN(dsn, ":", 2)[0]
if driver == "file" {
driver = "sqlite3"
}
- if noMigrate == nil || !*noMigrate {
- v, err := migrateDatabase(driver, *dsn)
+ if !noMigrate {
+ v, err := migrateDatabase(driver, dsn)
if err != nil {
log("failed to migrate:", err)
os.Exit(2)
@@ -91,7 +91,7 @@ func main() {
log("database at version", v)
}
- db, err := sqlx.Open(driver, *dsn)
+ db, err := sqlx.Open(driver, dsn)
if err != nil {
log("failed to open database:", err)
os.Exit(1)
@@ -114,6 +114,6 @@ func main() {
os.Exit(1)
}
- log("listening at", *addr)
- http.ListenAndServe(*addr, r)
+ log("listening at", addr)
+ http.ListenAndServe(addr, r)
}
diff --git a/cmd/server/routes.go b/cmd/server/routes.go
index 7c6f37e..1828664 100644
--- a/cmd/server/routes.go
+++ b/cmd/server/routes.go
@@ -27,9 +27,10 @@ func createRouter(db sws.Store) (chi.Router, error) {
tmplsAuthed := append(tmplsCommon, []string{"layouts/base.tmpl", "charts.tmpl", "timerange.tmpl"}...)
tmplsPublic := append(tmplsCommon, "layouts/public.tmpl")
- if override != nil {
+ if override != "" {
+ log("using overrider", override)
loadOverrider = func(s string) string {
- return filepath.Join(*override, s)
+ return filepath.Join(override, s)
}
}
@@ -55,13 +56,13 @@ func createRouter(db sws.Store) (chi.Router, error) {
r.Use(middleware.RequestID)
compressor := middleware.NewCompressor(5, "text/html", "text/css")
r.Use(compressor.Handler())
- if *verbose {
+ if verbose {
r.Use(middleware.Logger)
}
r.Use(middleware.Recoverer)
// For counter
- r.Get("/sws.js", handleCounter(*addr))
+ r.Get("/sws.js", handleCounter(addr))
r.Get("/sws.gif", handleHitCounter(db))
//r.Get("/hits", handleHits(db))
@@ -167,7 +168,6 @@ func getUserCtx(db sws.UserStore) func(http.Handler) http.Handler {
log("missing user")
return
}
- debug("found user, adding to context")
ctx := context.WithValue(r.Context(), "user", user)
r = r.WithContext(ctx)
})
diff --git a/referrer.go b/referrer.go
index 89db9e8..800b98d 100644
--- a/referrer.go
+++ b/referrer.go
@@ -70,6 +70,10 @@ func (rs *ReferrerSet) SortByHits() {
})
}
+func (rs ReferrerSet) Count() int {
+ return len(rs)
+}
+
func (rs ReferrerSet) GetReferrer(s string) *Referrer {
for _, r := range rs {
if r.Name == s {
diff --git a/static/default.css b/static/default.css
index b0ba4a4..78898dd 100644
--- a/static/default.css
+++ b/static/default.css
@@ -29,8 +29,10 @@ ul {
outline: 1px solid red;
}
main {
+ background-color: #f7f7f7;
flex: 1;
outline: 1px solid red;
+ padding-left: 1em;
}
@media (min-width: 768px) {
diff --git a/tmpl/layouts/base.tmpl b/tmpl/layouts/base.tmpl
index 8986909..13f5247 100644
--- a/tmpl/layouts/base.tmpl
+++ b/tmpl/layouts/base.tmpl
@@ -10,8 +10,8 @@
<div class="page">
<div class="sidebar">
<nav>
- <a href="/sites">Sites</a>
- <a href="/sites/new">New site</a>
+ <a class="sitenav__link" href="/sites">Sites</a>
+ <a class="sitenav__link" href="/sites/new">New site</a>
</nav>
</div>
{{ template "flash" . }}
diff --git a/tmpl/navbar.tmpl b/tmpl/navbar.tmpl
index 99dba68..a67d0a3 100644
--- a/tmpl/navbar.tmpl
+++ b/tmpl/navbar.tmpl
@@ -1,5 +1,5 @@
{{ define "navbar" }}
- <header class="site">
+ <header class="navbar">
<a class="logo" href="/">Logo</a>
{{ if .User }}
<a href="/sites">Sites</a>
diff --git a/tmpl/site.tmpl b/tmpl/site.tmpl
index 3fcd5fb..0762b2b 100644
--- a/tmpl/site.tmpl
+++ b/tmpl/site.tmpl
@@ -1,15 +1,18 @@
{{ define "content" }}
- <main>
- <header>
+ <main class="site">
+ <header class="site__header">
{{ if .Site.ID }}
{{ with .Site }}
- <h1>{{ .Name }}</h1>
- <span>{{ .Description }}</span>
+ <h1 class="site__title">{{ .Name }}</h1>
+ <span class="site__description">{{ .Description }}</span>
{{ end }}
{{ else }}
- <h1>New Site</h1>
+ <h1 class="site__title site__title--new">New Site</h1>
{{ end }}
</header>
+
+ {{ template "siteSummary" . }}
+
{{ if .Site.ID }}
{{ template "timerange" . }}
{{ end }}
@@ -53,8 +56,10 @@
{{ end }}
{{ define "siteView" }}
- <div class="panel panel-wide">
- <h2>Hits</h2>
+ <section class="panel panel--wide">
+ <header class="panel__header">
+ <h3 class="panel__title">Hits</h3>
+ </header>
{{ if .Hits }}
<fig>
{{ template "timeBarChart" .Hits }}
@@ -62,10 +67,12 @@
{{ else }}
<p>No hits yet</p>
{{ end }}
- </div>
+ </section>
- <div class="panel">
- <h2>Popular pages</h2>
+ <section class="panel">
+ <header class="panel__header">
+ <h3 class="panel__title">Popular pages</h3>
+ </header>
{{ if .PageSet }}
<fig>
{{ template "barChartHorizontal" .PageSet }}
@@ -84,10 +91,12 @@
{{ else }}
<p>No page views yet</p>
{{ end }}
- </div>
+ </section>
- <div class="panel">
- <h2>Referrers</h2>
+ <section class="panel">
+ <header class="panel__header">
+ <h3 class="panel__title">Referrers</h3>
+ </header>
{{ if .ReferrerSet }}
<fig>
{{ template "barChart" .ReferrerSet }}
@@ -100,10 +109,12 @@
{{ else }}
<p>No referrers yet</p>
{{ end }}
- </div>
+ </section>
- <div class="panel">
- <h2>User Agents</h2>
+ <section class="panel">
+ <header class="panel__header">
+ <h3 class="panel__title">User agents</h3>
+ </header>
{{ if .Browsers }}
<fig>
{{ template "barChart" .Browsers }}
@@ -116,21 +127,47 @@
{{ else }}
<p>No browsers visits yet</p>
{{ end }}
- </div>
+ </section>
+{{ end }}
+
+{{ define "siteSummary" }}
+ <section class="site__summary">
+ {{ with .Hits }}
+ <div class="summary">
+ <span class="summary__title">Total Hits</span>
+ <span class="summary__count">{{ .Count }}</span>
+ <span class="summary__comparison">for this period</span>
+ </div>
+ {{ end }}
+ {{ with .ReferrerSet }}
+ <div class="summary">
+ <span class="summary__title">Total Referrers</span>
+ <span class="summary__count">{{ .Count }}</span>
+ <!-- <span class="summary__comparison"></span> -->
+ </div>
+ {{ end }}
+ {{ with .Browsers }}
+ <div class="summary">
+ <span class="summary__title">Total Browsers</span>
+ <span class="summary__count">{{ .Count }}</span>
+ <!-- <span class="summary__comparison"></span> -->
+ </div>
+ {{ end }}
+ </section>
{{ end }}
{{ define "siteEdit" }}
<div class="field">
- <label>Name</label>
- <input type="text" name="name" value="{{ .Name }}" placeholder="example.com" required />
+ <label class="field__label">Name</label>
+ <input class="field__input" type="text" name="name" value="{{ .Name }}" placeholder="example.com" required />
</div>
<div class="field">
- <label>Description</label>
- <input type="text" name="description" value="{{ .Description }}" placeholder="site description" />
+ <label class="field__label">Description</label>
+ <input class="field__input" type="text" name="description" value="{{ .Description }}" placeholder="site description" />
</div>
<div class="field">
- <label>Aliases</label>
- <input type="text" name="aliases" value="{{ .Aliases }}" placeholder="www.example.com" />
+ <label class="field__label">Aliases</label>
+ <input class="field__input" type="text" name="aliases" value="{{ .Aliases }}" placeholder="www.example.com" />
</div>
{{ end }}
diff --git a/tmpl/timerange.tmpl b/tmpl/timerange.tmpl
index b58fc36..09ea04f 100644
--- a/tmpl/timerange.tmpl
+++ b/tmpl/timerange.tmpl
@@ -1,38 +1,40 @@
{{ define "timerange" }}
- <!--
- <form>
- <input type="date" name="bdate" value="{{ .Begin|dateRFC }}" />
- <input type="time" name="btime" value="{{ .Begin|timeRFC }}" />
- <input type="date" name="edate" value="{{ .End|dateRFC }}" />
- <input type="time" name="etime" value="{{ .End|timeRFC }}" />
- <select name="timezone">
- <option value="-39600">-1100</option>
- <option value="-36000">-1000</option>
- <option value="-32400">-0900</option>
- <option value="-28800">-0800</option>
- <option value="-25200">-0700</option>
- <option value="-21600">-0600</option>
- <option value="-18000">-0500</option>
- <option value="-14400">-0400</option>
- <option value="-10800">-0300</option>
- <option value="-7200">-0200</option>
- <option value="-3600">-0100</option>
- <option value="0">UTC</option>
- <option value="3600">+0100</option>
- <option value="7200">+0200</option>
- <option value="10800">+0300</option>
- <option value="14400">+0400</option>
- <option value="18000">+0500</option>
- <option value="21600">+0600</option>
- <option value="25200">+0700</option>
- <option value="28800">+0800</option>
- <option value="32400">+0900</option>
- <option value="36000">+1000</option>
- <option value="39600">+1100</option>
- </select>
- </form>
- -->
- <a href="?begin={{ datetimeRelative "-24h"}}">last day</a>
- <a href="?begin={{ datetimeRelative "-168h"}}">last 7 days</a>
- <a href="?begin={{ datetimeRelative "-720h"}}">last 30 days</a>
+ <div class="timerange">
+ <!--
+ <form>
+ <input type="date" name="bdate" value="{{ .Begin|dateRFC }}" />
+ <input type="time" name="btime" value="{{ .Begin|timeRFC }}" />
+ <input type="date" name="edate" value="{{ .End|dateRFC }}" />
+ <input type="time" name="etime" value="{{ .End|timeRFC }}" />
+ <select name="timezone">
+ <option value="-39600">-1100</option>
+ <option value="-36000">-1000</option>
+ <option value="-32400">-0900</option>
+ <option value="-28800">-0800</option>
+ <option value="-25200">-0700</option>
+ <option value="-21600">-0600</option>
+ <option value="-18000">-0500</option>
+ <option value="-14400">-0400</option>
+ <option value="-10800">-0300</option>
+ <option value="-7200">-0200</option>
+ <option value="-3600">-0100</option>
+ <option value="0">UTC</option>
+ <option value="3600">+0100</option>
+ <option value="7200">+0200</option>
+ <option value="10800">+0300</option>
+ <option value="14400">+0400</option>
+ <option value="18000">+0500</option>
+ <option value="21600">+0600</option>
+ <option value="25200">+0700</option>
+ <option value="28800">+0800</option>
+ <option value="32400">+0900</option>
+ <option value="36000">+1000</option>
+ <option value="39600">+1100</option>
+ </select>
+ </form>
+ -->
+ <a href="?begin={{ datetimeRelative "-24h"}}">last day</a>
+ <a href="?begin={{ datetimeRelative "-168h"}}">last 7 days</a>
+ <a href="?begin={{ datetimeRelative "-720h"}}">last 30 days</a>
+ </div>
{{ end }}