Skip to content

Commit

Permalink
Add API for the stats
Browse files Browse the repository at this point in the history
Fixes #233
  • Loading branch information
arp242 committed Oct 21, 2022
1 parent e692e5e commit 0bffad8
Show file tree
Hide file tree
Showing 55 changed files with 3,483 additions and 788 deletions.
9 changes: 4 additions & 5 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# Mark as generated so it won't show up in diffs etc.
pack/*.go linguist-generated=true
tpl/_backend_sitecode.gohtml linguist-generated=true
/go.sum linguist-generated=true
/docs/api.html linguist-generated=true
/docs/api.json linguist-generated=true
/go.sum linguist-generated=true
/tpl/api.html linguist-generated=true
/tpl/api.json linguist-generated=true
/refspam.go linguist-generated=true
15 changes: 14 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,20 @@ latest master.

unreleased
----------
- Can now merge paths instead of just deleting them.
- Add a more fully-featured API that can also retrieve the dashboard statistics.
See https://www.goatcounter.com/help/api for documentation.

- Default API ratelimit is now 4 requests/second, rather than 4 requests/10
seconds. You can use the `-ratelimit` flag to configure this.

- Can now also merge paths instead of just deleting them.

- Add `goatcounter dashboard`, which uses the new API to display the dashboard
in the terminal (only a basic non-interactive overview for now).

- Add a "Show fewer numbers" user setting; this is intended to still give a
reasonably useful overview of what happens on your site but prevent an
“obsession” over the exact number of visitors and stats.

2022-10-17 v2.3.0
-----------------
Expand Down
38 changes: 20 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,16 +125,18 @@ Generally speaking only the latest release is supported, although critical fixes


### Building from source
You need Go 1.17 or newer and a C compiler (for SQLite). If you compile it with
You need Go 1.18 or newer and a C compiler (for SQLite). If you compile it with
`CGO_ENABLED=0` you don't need a C compiler but can only use PostgreSQL.

Compile from source with:
You can install from source to $GOBIN (`go env GOBIN`) with:

$ git clone -b release-2.3 https://github.com/arp242/goatcounter.git
$ cd goatcounter
$ go build -ldflags="-X zgo.at/goatcounter/v2.Version=$(git log -n1 --format='%h_%cI')" ./cmd/goatcounter
% go install zgo.at/goatcounter/v2@latest

You'll now have a `goatcounter` binary in the current directory.
Or from a git checkout, also setting the version string:

% go build -ldflags="-X zgo.at/goatcounter/v2.Version=$(git log -n1 --format='%h_%cI')" ./cmd/goatcounter

Which will produce a `goatcounter` binary in the current directory.

The `-ldflags=[..]` sets the version; this isn't *strictly* required as such,
but it's recommended as it's used to "bust" the cache for static files and may
Expand All @@ -144,7 +146,7 @@ follow any particular format, you can also set this to the current date or

To build a fully statically linked binary:

$ go build -tags osusergo,netgo,sqlite_omit_load_extension \
% go build -tags osusergo,netgo,sqlite_omit_load_extension \
-ldflags="-X zgo.at/goatcounter/v2.Version=$(git log -n1 --format='%h_%cI') -extldflags=-static" \
./cmd/goatcounter

Expand All @@ -156,7 +158,7 @@ to surprises.
You can compile goatcounter without cgo if you're planning to use PostgreSQL and
don't use SQLite:

$ CGO_ENABLED=0 go build \
% CGO_ENABLED=0 go build \
-ldflags="-X zgo.at/goatcounter.Version=$(git log -n1 --format='%h_%cI')" \
./cmd/goatcounter

Expand All @@ -166,7 +168,7 @@ faster as it won't require a C compiler.
### Running
You can start a server with:

$ goatcounter serve
% goatcounter serve

The default is to use an SQLite database at `./db/goatcounter.sqlite3`, which
will be created if it doesn't exist yet. See the `-db` flag and `goatcounter
Expand All @@ -180,14 +182,14 @@ expect from SQLite and PostgreSQL.
GoatCounter will listen on port `*:80` and `*:443` by default. You don't need
to run it as root and can grant the appropriate permissions on Linux with:

$ setcap 'cap_net_bind_service=+ep' goatcounter
% setcap 'cap_net_bind_service=+ep' goatcounter

Listening on a different port can be a bit tricky due to the ACME/Let's Encrypt
certificate generation; `goatcounter help listen` documents this in depth.

You can create new sites with the `db create site` command:

$ goatcounter db create site -vhost stats.example.com -user.email [email protected]
% goatcounter db create site -vhost stats.example.com -user.email [email protected]

This will ask for a password for your new account; you can also add a password
on the commandline with `-password`. You must also pass the `-db` flag here if
Expand All @@ -210,23 +212,23 @@ Use `goatcounter migrate pending` to get a list of pending migrations, or
### PostgreSQL
To use PostgreSQL run GoatCounter with a custom `-db` flag; for example:

$ goatcounter serve -db 'postgresql+dbname=goatcounter'
$ goatcounter serve -db 'postgresql+host=/run/postgresql dbname=goatcounter sslmode=disable'
% goatcounter serve -db 'postgresql+dbname=goatcounter'
% goatcounter serve -db 'postgresql+host=/run/postgresql dbname=goatcounter sslmode=disable'

This follows the format in the `psql` CLI; you can also use the `PG*`
environment variables:

$ PGDATABASE=goatcounter DBHOST=/run/postgresql goatcounter serve -db 'postgresql'
% PGDATABASE=goatcounter DBHOST=/run/postgresql goatcounter serve -db 'postgresql'

The database will be created automatically if possible; if you want to create it
for a specific user you can use:

$ createuser --interactive --pwprompt goatcounter
$ createdb --owner goatcounter goatcounter
% createuser --interactive --pwprompt goatcounter
% createdb --owner goatcounter goatcounter

You can manually import the schema with:

$ goatcounter db schema-pgsql | psql --user=goatcounter --dbname=goatcounter
% goatcounter db schema-pgsql | psql --user=goatcounter --dbname=goatcounter

See `goatcounter help db` and the [pq docs][pq] for more details.

Expand All @@ -235,7 +237,7 @@ See `goatcounter help db` and the [pq docs][pq] for more details.
### Development/testing
You can start a test/development server with:

$ goatcounter serve -dev
% goatcounter serve -dev

The `-dev` flag makes some small things a bit more convenient for development;
TLS is disabled by default, it will listen on localhost:8081, the application
Expand Down
6 changes: 6 additions & 0 deletions api_token.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const (
APIPermSiteRead // 8
APIPermSiteCreate // 16
APIPermSiteUpdate // 32
APIPermStats // 64
)

type APIToken struct {
Expand Down Expand Up @@ -59,6 +60,11 @@ func (t APIToken) PermissionFlags(only ...zint.Bitflag64) []PermissionFlag {
Help: "Record pageviews with /api/v0/count",
Flag: APIPermCount,
},
{
Label: "Read statistics",
Help: "Get statistics out of GoatCounter",
Flag: APIPermStats,
},
{
Label: "Export",
Help: "Export data with /api/v0/export",
Expand Down
2 changes: 1 addition & 1 deletion cmd/goatcounter/buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"os"
"time"

"github.com/monoculum/formam"
"github.com/monoculum/formam/v3"
"zgo.at/errors"
"zgo.at/goatcounter/v2"
"zgo.at/goatcounter/v2/handlers"
Expand Down
Loading

0 comments on commit 0bffad8

Please sign in to comment.