Skip to content

Commit

Permalink
fix current visitors
Browse files Browse the repository at this point in the history
  • Loading branch information
gernest committed Feb 8, 2024
1 parent 6a12d44 commit 1ebb8cd
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 41 deletions.
23 changes: 16 additions & 7 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import (
"net/http"
"strings"

"github.com/bufbuild/protovalidate-go"
v1 "github.com/vinceanalytics/vince/gen/go/staples/v1"
"github.com/vinceanalytics/vince/guard"
"github.com/vinceanalytics/vince/logger"
"github.com/vinceanalytics/vince/request"
"github.com/vinceanalytics/vince/session"
"github.com/vinceanalytics/vince/stats"
Expand All @@ -27,13 +27,12 @@ func New(ctx context.Context, o *v1.Config) (*API, error) {
a := &API{
config: o,
}
valid, err := protovalidate.New()
if err != nil {
return nil, err
}
ctx = request.With(ctx, valid)
base := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
println("======?", r.URL.Path)
code := &responseCode{ResponseWriter: w}
defer func() {
logger.Get(r.Context()).Debug(r.URL.String(), "method", r.Method, "status", code.code)
}()
w = code
if a.config.AuthToken != "" && r.URL.Path != "/api/event" {
if subtle.ConstantTimeCompare([]byte(a.config.AuthToken), []byte(parseBearer(r.Header.Get("Authorization")))) != 1 {
request.Error(r.Context(), w, http.StatusUnauthorized, http.StatusText(http.StatusUnauthorized))
Expand Down Expand Up @@ -90,6 +89,16 @@ func New(ctx context.Context, o *v1.Config) (*API, error) {
return a, nil
}

type responseCode struct {
http.ResponseWriter
code int
}

func (r *responseCode) WriteHeader(code int) {
r.code = code
r.ResponseWriter.WriteHeader(code)
}

func parseBearer(auth string) (token string) {
const prefix = "Bearer "
// Case insensitive prefix match. See Issue 22736.
Expand Down
16 changes: 1 addition & 15 deletions load/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,6 @@ func CMD() *cli.Command {
},
Action: func(ctx context.Context, c *cli.Command) error {
vince := c.String("vince")
// check if it is reachable
req, err := http.NewRequest(http.MethodGet, vince+"/api/v1/version", nil)
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/json")
res, err := client.Do(req)
if err != nil {
return err
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
return fmt.Errorf("%s is not accessible", vince)
}
program := &Program{
Agents: Agents(),
Referrer: Referrer(),
Expand All @@ -64,7 +50,7 @@ func CMD() *cli.Command {
if a == "" {
return nil
}
data, err = os.ReadFile(a)
data, err := os.ReadFile(a)
if err != nil {
return fmt.Errorf("failed reading js file %q %v", a, err)
}
Expand Down
6 changes: 6 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/vinceanalytics/vince/load"
"github.com/vinceanalytics/vince/logger"
"github.com/vinceanalytics/vince/lsm"
"github.com/vinceanalytics/vince/request"
"github.com/vinceanalytics/vince/session"
"github.com/vinceanalytics/vince/staples"
"github.com/vinceanalytics/vince/version"
Expand Down Expand Up @@ -220,6 +221,11 @@ func app() *cli.Command {
ctx = guard.With(ctx, gd)

log.Info("Setup api")
validate, err := protovalidate.New()
if err != nil {
return err
}
ctx = request.With(ctx, validate)
a, err := api.New(ctx, base)
if err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion stats/aggregate.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (

func Aggregate(w http.ResponseWriter, r *http.Request) {
var req v1.Aggregate_Request
req.SiteId = r.URL.Query().Get("domain")
req.SiteId = r.URL.Query().Get("site_id")
if !request.Read(w, r, &req) {
return
}
Expand Down
8 changes: 5 additions & 3 deletions stats/current_visitors.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@ import (

func Realtime(w http.ResponseWriter, r *http.Request) {
var req v1.Realtime_Request
req.SiteId = r.URL.Query().Get("domain")
if !request.Read(w, r, &req) {
req.SiteId = r.URL.Query().Get("site_id")
ctx := r.Context()
if err := request.Get(r.Context()).Validate(&req); err != nil {
logger.Get(ctx).Error("Failed validating request body", "err", err)
request.Error(ctx, w, http.StatusBadRequest, err.Error())
return
}
ctx := r.Context()
now := time.Now().UTC()
firstTime := now.Add(-5 * time.Minute)
result, err := session.Get(ctx).Scan(ctx,
Expand Down
2 changes: 1 addition & 1 deletion stats/timeseries.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (

func TimeSeries(w http.ResponseWriter, r *http.Request) {
var req v1.Timeseries_Request
req.SiteId = r.URL.Query().Get("domain")
req.SiteId = r.URL.Query().Get("site_id")
if !request.Read(w, r, &req) {
return
}
Expand Down
16 changes: 3 additions & 13 deletions tools/curl/curl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@ import (
"testing"

"github.com/stretchr/testify/require"
v1 "github.com/vinceanalytics/vince/gen/go/staples/v1"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/proto"
)

var API = CMD("http://localhost:8080")

const siteId = "?site_id=vinceanalytics.com"

func TestVersion(t *testing.T) {
check(t, false, "version.sh", "/api/v1/version", http.MethodGet, nil, nil)
}
func TestVisitors(t *testing.T) {
check(t, false, "visitors.sh", "/api/v1/visitors/example.com", http.MethodGet, nil, nil)
check(t, true, "visitors.sh", "/api/v1/visitors"+siteId, http.MethodGet, nil, nil)
}

func check(t *testing.T, write bool, file string, path, method string, headers http.Header, body proto.Message) {
Expand All @@ -35,13 +35,3 @@ func check(t *testing.T, write bool, file string, path, method string, headers h
require.NoError(t, err)
require.Equal(t, string(want), b.String())
}

func TestConfig(t *testing.T) {
d, _ := protojson.Marshal(&v1.Config{
Acme: &v1.Acme{
Email: "[email protected]",
Domain: "example.org",
},
})
t.Error(string(d))
}
2 changes: 1 addition & 1 deletion tools/curl/testdata/visitors.sh
Original file line number Diff line number Diff line change
@@ -1 +1 @@
curl -X 'GET' 'http://localhost:8080/api/v1/visitors/example.com'
curl -X 'GET' 'http://localhost:8080/api/v1/visitors?site_id=vinceanalytics.com'

0 comments on commit 1ebb8cd

Please sign in to comment.