Skip to content

Commit

Permalink
Add CI, some staticcheck fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
arp242 committed Aug 19, 2024
1 parent 7a7f98e commit d2f6d8a
Show file tree
Hide file tree
Showing 24 changed files with 100 additions and 76 deletions.
44 changes: 44 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
name: 'test backend'
on:
push:
paths: ['**.go', 'go.mod', '.github/workflows/*']
pr:
paths: ['**.go', 'go.mod', '.github/workflows/*']

jobs:
# Uncomment to get a debug shell.
# debug: {runs-on: 'ubuntu-latest', steps: [{uses: 'actions/checkout@v4'}, {uses: 'mxschmitt/action-tmate@v3'}]}

test:
name: 'test (linux)'
runs-on: 'ubuntu-latest'
steps:
- uses: 'actions/checkout@v4'
- name: 'test (linux)'
run: |
# Quick exit on compile errors.
go build -race ./cmd/goatcounter || exit 1
# Make sure it at least compiles on macOS, Windows, and arm64
GOARCH=arm64 go build ./cmd/goatcounter
GOARCH=arm64 GOOS=darwin go build ./cmd/goatcounter
GOOS=windows go build ./cmd/goatcounter
go test -race -timeout=3m ./...
docker compose up -d --wait
export PGHOST=localhost
export PGPORT=5433
export PGDATABASE=goatcounter
export PGUSER=goatcounter
export PGPASSWORD=goatcounter
export PGSSLMODE=disable
go test -race -timeout=3m -tags pgsql ./...
staticcheck:
name: 'staticcheck'
runs-on: 'ubuntu-latest'
steps:
- uses: 'actions/checkout@v4'
- uses: 'dominikh/[email protected]'
with: {version: '2024.1'}
2 changes: 1 addition & 1 deletion acme/acme_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func TestSetup(t *testing.T) {
haveACME := acmeH != nil

if tlsC != nil {
t.Logf(zruntime.FuncName(tlsC.GetCertificate))
t.Log(zruntime.FuncName(tlsC.GetCertificate))
}
if haveTLS != tt.wantTLS {
t.Errorf("have TLS %t; want %t", haveTLS, tt.wantTLS)
Expand Down
4 changes: 2 additions & 2 deletions api_token.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ func (t *APIToken) Insert(ctx context.Context) error {

t.ID, err = zdb.InsertID(ctx, "api_token_id",
`insert into api_tokens (site_id, user_id, name, token, permissions, created_at) values (?)`,
zdb.L{t.SiteID, GetUser(ctx).ID, t.Name, t.Token, t.Permissions, t.CreatedAt})
[]any{t.SiteID, GetUser(ctx).ID, t.Name, t.Token, t.Permissions, t.CreatedAt})
return errors.Wrap(err, "APIToken.Insert")
}

Expand Down Expand Up @@ -226,7 +226,7 @@ func (t *APITokens) Find(ctx context.Context, ident []string) error {
{{:ids api_token_id in (:ids) or}}
{{:strs! 0=1}}
{{:strs token in (:strs)}}`,
zdb.P{"ids": ids, "strs": strs})
map[string]any{"ids": ids, "strs": strs})
return errors.Wrap(err, "APITokens.Find")
}

Expand Down
14 changes: 0 additions & 14 deletions cmd/goatcounter/old.go

This file was deleted.

1 change: 0 additions & 1 deletion cmd/goatcounter/pg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// in the LICENSE file or at https://license.goatcounter.com

//go:build testpg
// +build testpg

package main

Expand Down
2 changes: 1 addition & 1 deletion cmd/goatcounter/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,7 @@ func flagErrors(errors string, v *zvalidate.Validator) {
subject = subject[i+7:]
}

err := blackmail.Send(fmt.Sprintf(subject),
err := blackmail.Send(subject,
blackmail.From("", from),
blackmail.To(to),
blackmail.BodyText([]byte(msg)))
Expand Down
14 changes: 14 additions & 0 deletions compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
name: 'goatcounter'

volumes:
postgres-data: {}

services:
postgres:
image: 'postgres:16-alpine'
ports: ['127.0.0.1:5433:5432'] # 5433 instead of default 5432
volumes: ['postgres-data:/var/lib/postgresql/data']
environment:
'POSTGRES_USER': 'goatcounter'
'POSTGRES_PASSWORD': 'goatcounter'
'POSTGRES_DATABASE': 'goatcounter'
1 change: 0 additions & 1 deletion gctest/pg.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// in the LICENSE file or at https://license.goatcounter.com

//go:build testpg
// +build testpg

package gctest

Expand Down
1 change: 0 additions & 1 deletion gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// in the LICENSE file or at https://license.goatcounter.com

//go:build go_run_only
// +build go_run_only

package main

Expand Down
2 changes: 1 addition & 1 deletion handlers/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ func TestAPICount(t *testing.T) {
}

if d := ztest.Diff(have, tt.want); d != "" {
t.Errorf(d)
t.Error(d)
}
})
}
Expand Down
2 changes: 1 addition & 1 deletion handlers/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ func (h user) requestLogin(w http.ResponseWriter, r *http.Request) error {
return err
}

if user.Password == nil || len(user.Password) == 0 {
if len(user.Password) == 0 {
zhttp.FlashError(w, T(r.Context(), "error/login-no-password|There is no password set for %(email); please reset it", args.Email))
return zhttp.SeeOther(w, "/user/forgot?email="+url.QueryEscape(args.Email))
}
Expand Down
2 changes: 1 addition & 1 deletion helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func NewBufferKey(ctx context.Context) (string, error) {
return err
}

err = zdb.Exec(ctx, `insert into store (key, value) values ('buffer-secret', :s)`, zdb.P{"s": secret})
err = zdb.Exec(ctx, `insert into store (key, value) values ('buffer-secret', :s)`, map[string]any{"s": secret})
return err
})
if err != nil {
Expand Down
1 change: 0 additions & 1 deletion helper_cgo.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// in the LICENSE file or at https://license.goatcounter.com

//go:build cgo
// +build cgo

package goatcounter

Expand Down
2 changes: 1 addition & 1 deletion hit.go
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ func (h *Hits) TestList(ctx context.Context, siteOnly bool) error {
left join sizes using (size_id)
{{:site_only where hits.site_id = :site}}
order by hit_id asc`,
zdb.P{
map[string]any{
"site": MustGetSite(ctx).ID,
"site_only": siteOnly,
})
Expand Down
16 changes: 8 additions & 8 deletions hit_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ type HitListStat struct {

// PathCount gets the visit count for one path.
func (h *HitList) PathCount(ctx context.Context, path string, rng ztime.Range) error {
err := zdb.Get(ctx, h, "load:hit_list.PathCount", zdb.P{
err := zdb.Get(ctx, h, "load:hit_list.PathCount", map[string]any{
"site": MustGetSite(ctx).ID,
"path": path,
"start": rng.Start,
Expand All @@ -76,7 +76,7 @@ func (h *HitList) SiteTotalUTC(ctx context.Context, rng ztime.Range) error {
where site_id = :site
{{:start and hour >= :start}}
{{:end and hour <= :end}}
`, zdb.P{
`, map[string]any{
"site": MustGetSite(ctx).ID,
"start": rng.Start,
"end": rng.End,
Expand All @@ -88,7 +88,7 @@ type HitLists []HitList

// ListPathsLike lists all paths matching the like pattern.
func (h *HitLists) ListPathsLike(ctx context.Context, search string, matchTitle, matchCase bool) error {
err := zdb.Select(ctx, h, "load:hit_list.ListPathsLike", zdb.P{
err := zdb.Select(ctx, h, "load:hit_list.ListPathsLike", map[string]any{
"site": MustGetSite(ctx).ID,
"search": search,
"match_title": matchTitle,
Expand All @@ -109,7 +109,7 @@ func (h *HitLists) List(
// List the pages for this time period; this gets the path_id, path, title.
var more bool
{
err := zdb.Select(ctx, h, "load:hit_list.List-counts", zdb.P{
err := zdb.Select(ctx, h, "load:hit_list.List-counts", map[string]any{
"site": site.ID,
"start": rng.Start,
"end": rng.End,
Expand Down Expand Up @@ -147,7 +147,7 @@ func (h *HitLists) List(
paths[i] = hh[i].PathID
}

err := zdb.Select(ctx, &st, "load:hit_list.List-stats", zdb.P{
err := zdb.Select(ctx, &st, "load:hit_list.List-stats", map[string]any{
"site": site.ID,
"start": rng.Start.Format("2006-01-02"),
"end": rng.End.Format("2006-01-02"),
Expand Down Expand Up @@ -198,7 +198,7 @@ func (h *HitList) Totals(ctx context.Context, rng ztime.Range, pathFilter []int6
Hour time.Time `db:"hour"`
Total int `db:"total"`
}
err := zdb.Select(ctx, &tc, "load:hit_list.Totals", zdb.P{
err := zdb.Select(ctx, &tc, "load:hit_list.Totals", map[string]any{
"site": site.ID,
"start": rng.Start,
"end": rng.End,
Expand Down Expand Up @@ -429,7 +429,7 @@ func GetTotalCount(ctx context.Context, rng ztime.Range, pathFilter []int64, noE
user := MustGetUser(ctx)

var t TotalCount
err := zdb.Get(ctx, &t, "load:hit_list.GetTotalCount", zdb.P{
err := zdb.Get(ctx, &t, "load:hit_list.GetTotalCount", map[string]any{
"site": site.ID,
"start": rng.Start,
"end": rng.End,
Expand Down Expand Up @@ -462,7 +462,7 @@ func (h HitLists) Diff(ctx context.Context, rng, prev ztime.Range) ([]float64, e
}

var diffs []float64
err := zdb.Select(ctx, &diffs, "load:hit_list.DiffTotal", zdb.P{
err := zdb.Select(ctx, &diffs, "load:hit_list.DiffTotal", map[string]any{
"site": MustGetSite(ctx).ID,
"start": rng.Start,
"end": rng.End,
Expand Down
26 changes: 13 additions & 13 deletions hit_stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func asUTCDate(u *User, t time.Time) string {
// total number of hits.
func (h *HitStats) ListTopRefs(ctx context.Context, rng ztime.Range, pathFilter []int64, limit, offset int) error {
site := MustGetSite(ctx)
err := zdb.Select(ctx, &h.Stats, "load:ref.ListTopRefs.sql", zdb.P{
err := zdb.Select(ctx, &h.Stats, "load:ref.ListTopRefs.sql", map[string]any{
"site": site.ID,
"start": rng.Start,
"end": rng.End,
Expand All @@ -71,7 +71,7 @@ func (h *HitStats) ListTopRefs(ctx context.Context, rng ztime.Range, pathFilter

// ListTopRef lists all paths by referrer.
func (h *HitStats) ListTopRef(ctx context.Context, ref string, rng ztime.Range, pathFilter []int64, limit, offset int) error {
err := zdb.Select(ctx, &h.Stats, "load:hit_stats.ByRef", zdb.P{
err := zdb.Select(ctx, &h.Stats, "load:hit_stats.ByRef", map[string]any{
"site": MustGetSite(ctx).ID,
"start": rng.Start,
"end": rng.End,
Expand All @@ -90,7 +90,7 @@ func (h *HitStats) ListTopRef(ctx context.Context, ref string, rng ztime.Range,
// ListBrowsers lists all browser statistics for the given time period.
func (h *HitStats) ListBrowsers(ctx context.Context, rng ztime.Range, pathFilter []int64, limit, offset int) error {
user := MustGetUser(ctx)
err := zdb.Select(ctx, &h.Stats, "load:hit_stats.ListBrowsers", zdb.P{
err := zdb.Select(ctx, &h.Stats, "load:hit_stats.ListBrowsers", map[string]any{
"site": MustGetSite(ctx).ID,
"start": asUTCDate(user, rng.Start),
"end": asUTCDate(user, rng.End),
Expand All @@ -108,7 +108,7 @@ func (h *HitStats) ListBrowsers(ctx context.Context, rng ztime.Range, pathFilter
// ListBrowser lists all the versions for one browser.
func (h *HitStats) ListBrowser(ctx context.Context, browser string, rng ztime.Range, pathFilter []int64, limit, offset int) error {
user := MustGetUser(ctx)
err := zdb.Select(ctx, &h.Stats, "load:hit_stats.ListBrowser", zdb.P{
err := zdb.Select(ctx, &h.Stats, "load:hit_stats.ListBrowser", map[string]any{
"site": MustGetSite(ctx).ID,
"start": asUTCDate(user, rng.Start),
"end": asUTCDate(user, rng.End),
Expand All @@ -127,7 +127,7 @@ func (h *HitStats) ListBrowser(ctx context.Context, browser string, rng ztime.Ra
// ListSystems lists OS statistics for the given time period.
func (h *HitStats) ListSystems(ctx context.Context, rng ztime.Range, pathFilter []int64, limit, offset int) error {
user := MustGetUser(ctx)
err := zdb.Select(ctx, &h.Stats, "load:hit_stats.ListSystems", zdb.P{
err := zdb.Select(ctx, &h.Stats, "load:hit_stats.ListSystems", map[string]any{
"site": MustGetSite(ctx).ID,
"start": asUTCDate(user, rng.Start),
"end": asUTCDate(user, rng.End),
Expand All @@ -145,7 +145,7 @@ func (h *HitStats) ListSystems(ctx context.Context, rng ztime.Range, pathFilter
// ListSystem lists all the versions for one system.
func (h *HitStats) ListSystem(ctx context.Context, system string, rng ztime.Range, pathFilter []int64, limit, offset int) error {
user := MustGetUser(ctx)
err := zdb.Select(ctx, &h.Stats, "load:hit_stats.ListSystem", zdb.P{
err := zdb.Select(ctx, &h.Stats, "load:hit_stats.ListSystem", map[string]any{
"site": MustGetSite(ctx).ID,
"start": asUTCDate(user, rng.Start),
"end": asUTCDate(user, rng.End),
Expand Down Expand Up @@ -173,7 +173,7 @@ const (
// ListSizes lists all device sizes.
func (h *HitStats) ListSizes(ctx context.Context, rng ztime.Range, pathFilter []int64) error {
user := MustGetUser(ctx)
err := zdb.Select(ctx, &h.Stats, "load:hit_stats.ListSizes", zdb.P{
err := zdb.Select(ctx, &h.Stats, "load:hit_stats.ListSizes", map[string]any{
"site": MustGetSite(ctx).ID,
"start": asUTCDate(user, rng.Start),
"end": asUTCDate(user, rng.End),
Expand Down Expand Up @@ -238,7 +238,7 @@ func (h *HitStats) ListSize(ctx context.Context, id string, rng ztime.Range, pat
}

user := MustGetUser(ctx)
err := zdb.Select(ctx, &h.Stats, "load:hit_stats.ListSize", zdb.P{
err := zdb.Select(ctx, &h.Stats, "load:hit_stats.ListSize", map[string]any{
"site": MustGetSite(ctx).ID,
"start": asUTCDate(user, rng.Start),
"end": asUTCDate(user, rng.End),
Expand All @@ -265,7 +265,7 @@ func (h *HitStats) ListSize(ctx context.Context, id string, rng ztime.Range, pat
// ListLocations lists all location statistics for the given time period.
func (h *HitStats) ListLocations(ctx context.Context, rng ztime.Range, pathFilter []int64, limit, offset int) error {
user := MustGetUser(ctx)
err := zdb.Select(ctx, &h.Stats, "load:hit_stats.ListLocations", zdb.P{
err := zdb.Select(ctx, &h.Stats, "load:hit_stats.ListLocations", map[string]any{
"site": MustGetSite(ctx).ID,
"start": asUTCDate(user, rng.Start),
"end": asUTCDate(user, rng.End),
Expand All @@ -283,7 +283,7 @@ func (h *HitStats) ListLocations(ctx context.Context, rng ztime.Range, pathFilte
// ListLocation lists all divisions for a location
func (h *HitStats) ListLocation(ctx context.Context, country string, rng ztime.Range, pathFilter []int64, limit, offset int) error {
user := MustGetUser(ctx)
err := zdb.Select(ctx, &h.Stats, "load:hit_stats.ListLocation", zdb.P{
err := zdb.Select(ctx, &h.Stats, "load:hit_stats.ListLocation", map[string]any{
"site": MustGetSite(ctx).ID,
"start": asUTCDate(user, rng.Start),
"end": asUTCDate(user, rng.End),
Expand All @@ -302,7 +302,7 @@ func (h *HitStats) ListLocation(ctx context.Context, country string, rng ztime.R
// ListLanguages lists all language statistics for the given time period.
func (h *HitStats) ListLanguages(ctx context.Context, rng ztime.Range, pathFilter []int64, limit, offset int) error {
user := MustGetUser(ctx)
err := zdb.Select(ctx, &h.Stats, "load:hit_stats.ListLanguages", zdb.P{
err := zdb.Select(ctx, &h.Stats, "load:hit_stats.ListLanguages", map[string]any{
"site": MustGetSite(ctx).ID,
"start": asUTCDate(user, rng.Start),
"end": asUTCDate(user, rng.End),
Expand All @@ -320,7 +320,7 @@ func (h *HitStats) ListLanguages(ctx context.Context, rng ztime.Range, pathFilte
// ListCampaigns lists all campaigns statistics for the given time period.
func (h *HitStats) ListCampaigns(ctx context.Context, rng ztime.Range, pathFilter []int64, limit, offset int) error {
user := MustGetUser(ctx)
err := zdb.Select(ctx, &h.Stats, "load:hit_stats.ListCampaigns", zdb.P{
err := zdb.Select(ctx, &h.Stats, "load:hit_stats.ListCampaigns", map[string]any{
"site": MustGetSite(ctx).ID,
"start": asUTCDate(user, rng.Start),
"end": asUTCDate(user, rng.End),
Expand All @@ -338,7 +338,7 @@ func (h *HitStats) ListCampaigns(ctx context.Context, rng ztime.Range, pathFilte
// ListCampaign lists all statistics for a campaign.
func (h *HitStats) ListCampaign(ctx context.Context, campaign int64, rng ztime.Range, pathFilter []int64, limit, offset int) error {
user := MustGetUser(ctx)
err := zdb.Select(ctx, &h.Stats, "load:hit_stats.ListCampaign", zdb.P{
err := zdb.Select(ctx, &h.Stats, "load:hit_stats.ListCampaign", map[string]any{
"site": MustGetSite(ctx).ID,
"start": asUTCDate(user, rng.Start),
"end": asUTCDate(user, rng.End),
Expand Down
2 changes: 1 addition & 1 deletion memstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ func (m *ms) processHit(ctx context.Context, h *Hit) bool {
return true
}

// Maximum length of sessions; exported here for tests.
// SessionTime is the maximum length of sessions; exported here for tests.
var SessionTime = 8 * time.Hour

// For 10k sessions this takes about 5ms on my laptop; that's a small enough
Expand Down
4 changes: 2 additions & 2 deletions path.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ type Paths []Path

// List all paths for a site.
func (p *Paths) List(ctx context.Context, siteID, after int64, limit int) (bool, error) {
err := zdb.Select(ctx, p, "load:paths.List", zdb.P{
err := zdb.Select(ctx, p, "load:paths.List", map[string]any{
"site": siteID,
"after": after,
"limit": limit + 1,
Expand All @@ -164,7 +164,7 @@ func (p *Paths) List(ctx context.Context, siteID, after int64, limit int) (bool,
// if matchTitle is true it will match the title as well.
func PathFilter(ctx context.Context, filter string, matchTitle bool) ([]int64, error) {
var paths []int64
err := zdb.Select(ctx, &paths, "load:paths.PathFilter", zdb.P{
err := zdb.Select(ctx, &paths, "load:paths.PathFilter", map[string]any{
"site": MustGetSite(ctx).ID,
"filter": "%" + filter + "%",
"match_title": matchTitle,
Expand Down
Loading

0 comments on commit d2f6d8a

Please sign in to comment.