Skip to content

Commit ae2b245

Browse files
committed
V1 of vhosts
This is not currently backwards compatible for users who imports logs via combined-vhosts or something similar, it will differentiate between paths/hits from before this patch & migration as different from paths/hits after this change if vhost is sent on log imports. Maybe we default to domainlink vhost if none is found? This makes things less backwards compatible too, then makes all new imports be different hitcounts than old ones. Maybe we retroactively patch paths that match to have a new vhost? What this doesn't do/doesn't do yet: - send vhost with the javascript or tracking pixel - migrate existing hitcounts/paths to tag the domainlink as host - allow specifying multiple domainlinks to special-case referrers
1 parent bfddb80 commit ae2b245

File tree

8 files changed

+19
-7
lines changed

8 files changed

+19
-7
lines changed

cmd/goatcounter/import.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ List of format specifiers:
163163
status Status code sent to the client.
164164
http HTTP request protocol (i.e. HTTP/1.1).
165165
path Path (e.g. "/mypage.html"). May contain the query string.
166+
host Server name of the server serving the request.
166167
url Full URL (e.g. "https://example.com/mypage.html"). Takes
167168
precedence over $host and $path.
168169
query Query string; only needed if not included in $path or $url.
@@ -171,7 +172,6 @@ List of format specifiers:
171172
172173
Some format specifiers that are not (yet) used anywhere:
173174
174-
host Server name of the server serving the request.
175175
content_type Content-Type header of the response.
176176
timing_sec Time to serve the request in seconds, with possible decimal.
177177
timing_milli Time to serve the request in milliseconds.
@@ -388,6 +388,7 @@ func importLog(
388388
Ref: line.Referrer(),
389389
Query: line.Query(),
390390
UserAgent: line.UserAgent(),
391+
Host: line.Host(),
391392
}
392393

393394
hit.CreatedAt, err = scan.Datetime(line)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
alter table paths add column host varchar not null default '';

db/query/hit_list.List-counts.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ with x as (
99
order by total desc, path_id desc
1010
limit :limit
1111
)
12-
select path_id, paths.path, paths.title, paths.event from x
12+
select path_id, paths.path, paths.title, paths.host, paths.event from x
1313
join paths using (path_id)
1414
order by total desc, path_id desc

handlers/api.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -488,7 +488,7 @@ type APICountRequestHit struct {
488488
Session string `json:"session"`
489489

490490
// {omitdoc}
491-
Host string `json:"-"`
491+
Host string `json:"host" query:"h"`
492492

493493
// {omitdoc} Line when importing, for displaying errors.
494494
Line string `json:"-"`
@@ -574,6 +574,7 @@ func (h api) count(w http.ResponseWriter, r *http.Request) error {
574574
UserAgentHeader: a.UserAgent,
575575
Location: a.Location,
576576
RemoteAddr: a.IP,
577+
Host: a.Host,
577578
}
578579

579580
if a.UserAgent != "" {

hit.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ type Hit struct {
2626
Session zint.Uint128 `db:"session" json:"-"`
2727

2828
Path string `db:"-" json:"p,omitempty"`
29+
Host string `db:"-" json:"h,omitempty"`
2930
Title string `db:"-" json:"t,omitempty"`
3031
Ref string `db:"-" json:"r,omitempty"`
3132
Event zbool.Bool `db:"-" json:"e,omitempty"`
@@ -252,7 +253,7 @@ func (h *Hit) Defaults(ctx context.Context, initial bool) error {
252253
}
253254

254255
// Get or insert path.
255-
path := Path{Path: h.Path, Title: h.Title, Event: h.Event}
256+
path := Path{Path: h.Path, Host: h.Host, Title: h.Title, Event: h.Event}
256257
err := path.GetOrInsert(ctx)
257258
if err != nil {
258259
return errors.Wrap(err, "Hit.Defaults")

hit_list.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ type HitList struct {
2121
// Path ID
2222
PathID int64 `db:"path_id" json:"path_id"`
2323

24+
Host string `db:"host" json:"host"`
25+
2426
// Path name (e.g. /hello.html).
2527
Path string `db:"path" json:"path"`
2628

path.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ type Path struct {
1919
ID int64 `db:"path_id" json:"id"` // Path ID
2020
Site int64 `db:"site_id" json:"-"`
2121
Path string `db:"path" json:"path"` // Path name
22+
Host string `db:"host" json:"host"`
2223
Title string `db:"title" json:"title"` // Page title
2324
Event zbool.Bool `db:"event" json:"event"` // Is this an event?
2425
}
@@ -82,8 +83,8 @@ func (p *Path) GetOrInsert(ctx context.Context) error {
8283

8384
// Insert new row.
8485
p.ID, err = zdb.InsertID(ctx, "path_id",
85-
`insert into paths (site_id, path, title, event) values (?, ?, ?, ?)`,
86-
site.ID, p.Path, p.Title, p.Event)
86+
`insert into paths (site_id, path, title, event, host) values (?, ?, ?, ?, ?)`,
87+
site.ID, p.Path, p.Title, p.Event, p.Host)
8788
if err != nil {
8889
return errors.Wrap(err, "Path.GetOrInsert insert")
8990
}

tpl/_dashboard_pages_rows.gohtml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,25 @@
2020
</td>
2121
{{end}}
2222
<td class="col-path hide-mobile">
23-
<a class="load-refs rlink" title="{{$h.Path}}" href="#">{{$h.Path}}</a><br>
23+
<a class="load-refs rlink" title="{{$h.Host}}{{$h.Path}}" href="#">{{$h.Host}}{{$h.Path}}</a><br>
2424
<small class="page-title {{if not $h.Title}}no-title{{end}}">{{if $h.Title}}{{$h.Title}}{{else}}<em>({{t $.Context "no-title|no title"}})</em>{{end}}</small>
2525
{{if $h.Event}}
2626
<sup class="label-event">{{t $.Context "event|event"}}</sup>
27+
{{else if $h.Host}}
28+
<br><small class="go"><a target="_blank" rel="noopener" href="http://{{$h.Host}}{{$h.Path}}">{{t $.Context "link/goto-path|Go to %(path)" (printf `%s%s` $h.Host $h.Path)}}</a></small>
2729
{{else if $.Site.LinkDomain}}
2830
<br><small class="go"><a target="_blank" rel="noopener" href="{{$.Site.LinkDomainURL true $h.Path}}">{{t $.Context "link/goto-path|Go to %(path)" ($.Site.LinkDomainURL false $h.Path)}}</a></small>
2931
{{end}}
32+
3033
</td>
3134
<td>
3235
<div class="show-mobile">
3336
<a class="load-refs rlink" title="{{$h.Path}}" href="#">{{$h.Path}}</a>
3437
<small class="page-title {{if not $h.Title}}no-title{{end}}">| {{if $h.Title}}{{$h.Title}}{{else}}<em>(no title)</em>{{end}}</small>
3538
{{if $h.Event}}
3639
<sup class="label-event">{{t $.Context "event|event"}}</sup>
40+
{{else if $h.Host}}
41+
<br><small class="go"><a target="_blank" rel="noopener" href="http://{{$h.Host}}{{$h.Path}}">{{t $.Context "link/goto-path|Go to %(path)" $h.Path}}</a></small>
3742
{{else if $.Site.LinkDomain}}
3843
<br><small class="go"><a target="_blank" rel="noopener" href="{{$.Site.LinkDomainURL true $h.Path}}">{{t $.Context "link/goto-path|Go to %(path)" ($.Site.LinkDomainURL false $h.Path)}}</a></small>
3944
{{end}}

0 commit comments

Comments
 (0)