Skip to content

Commit

Permalink
server-side tracking: add /stats
Browse files Browse the repository at this point in the history
  • Loading branch information
quantonganh committed Mar 23, 2024
1 parent d1ea552 commit 998b11d
Show file tree
Hide file tree
Showing 21 changed files with 950 additions and 52 deletions.
41 changes: 34 additions & 7 deletions cmd/blog/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,15 @@ import (

"github.com/getsentry/sentry-go"
"github.com/pkg/errors"
"github.com/rs/zerolog"
"github.com/spf13/viper"

"github.com/quantonganh/blog"
"github.com/quantonganh/blog/http"
"github.com/quantonganh/blog/kafka"
"github.com/quantonganh/blog/markdown"
"github.com/quantonganh/blog/rabbitmq"
"github.com/quantonganh/blog/sqlite"
)

func main() {
Expand Down Expand Up @@ -48,9 +51,13 @@ func main() {
log.Fatal(err)
}

a, err := newApp(config, posts)
logger := zerolog.New(os.Stdout).With().
Timestamp().
Logger()

a, err := newApp(logger, config, posts)
if err != nil {
log.Fatal(err)
logger.Error().Err(err).Msg("error creating new app")
}

ctx, cancel := context.WithCancel(context.Background())
Expand All @@ -76,30 +83,46 @@ func main() {
}

type app struct {
db *sqlite.DB
config *blog.Config
httpServer *http.Server
}

func newApp(config *blog.Config, posts []*blog.Post) (*app, error) {
mqService, err := rabbitmq.NewMessageQueueService(config.AMQP.URL)
func newApp(logger zerolog.Logger, config *blog.Config, posts []*blog.Post) (*app, error) {
httpServer, err := http.NewServer(logger, config, posts)
if err != nil {
logger.Error().Err(err).Msg("error creating new HTTP server")
return nil, err
}

httpServer, err := http.NewServer(config, posts)
queueService, err := rabbitmq.NewQueueService(config.AMQP.URL)
if err != nil {
log.Fatalf("%+v\n", err)
return nil, err
}
httpServer.MessageQueueService = mqService
httpServer.QueueService = queueService

eventService, err := kafka.NewEventService(config.Kafka.Broker)
if err != nil {
return nil, err
}
httpServer.EventService = eventService

db := sqlite.NewDB("db/stats.db")
statService := sqlite.NewStatService(logger, db)
httpServer.StatService = statService

return &app{
db: db,
config: config,
httpServer: httpServer,
}, nil
}

func (a *app) Run(ctx context.Context) error {
if err := a.db.Open(); err != nil {
return err
}

a.httpServer.Addr = a.config.HTTP.Addr
baseURL, err := url.Parse(a.config.Site.BaseURL)
if err != nil {
Expand All @@ -111,6 +134,10 @@ func (a *app) Run(ctx context.Context) error {
return err
}

if err := a.httpServer.ProcessActivityStream(ctx, a.config.IP2Location.Token); err != nil {
return err
}

return nil
}

Expand Down
8 changes: 8 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,14 @@ type Config struct {
AMQP struct {
URL string
}

Kafka struct {
Broker string
}

IP2Location struct {
Token string
}
}

// Item represents a navbar item
Expand Down
20 changes: 20 additions & 0 deletions event.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package blog

import "context"

type Event struct {
UserID string `json:"user_id"`
IP string `json:"ip"`
Country string `json:"country"`
UserAgent string `json:"user_agent"`
Browser string `json:"browser"`
OS string `json:"os"`
Referer string `json:"referer"`
URL string `json:"url"`
Time string `json:"time"`
}

type EventService interface {
SendMessage(topic, key string, value []byte) error
Consume(ctx context.Context, topic string) (<-chan *Event, error)
}
39 changes: 31 additions & 8 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,40 @@ require (
github.com/blevesearch/bleve v1.0.14
github.com/getsentry/sentry-go v0.9.0
github.com/gorilla/feeds v1.1.1
github.com/gorilla/mux v1.7.3
github.com/gorilla/mux v1.7.4
github.com/pkg/errors v0.9.1
github.com/quantonganh/httperror v0.0.5
github.com/rs/zerolog v1.23.0
github.com/russross/blackfriday/v2 v2.1.0
github.com/spf13/viper v1.3.2
github.com/stretchr/testify v1.8.0
golang.org/x/net v0.17.0
golang.org/x/sync v0.1.0
github.com/stretchr/testify v1.8.4
golang.org/x/net v0.21.0
golang.org/x/sync v0.6.0
gopkg.in/yaml.v3 v3.0.1
)

require (
github.com/eapache/go-resiliency v1.6.0 // indirect
github.com/eapache/go-xerial-snappy v0.0.0-20230731223053-c322873962e3 // indirect
github.com/eapache/queue v1.1.0 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/hashicorp/go-uuid v1.0.3 // indirect
github.com/jcmturner/aescts/v2 v2.0.0 // indirect
github.com/jcmturner/dnsutils/v2 v2.0.0 // indirect
github.com/jcmturner/gofork v1.7.6 // indirect
github.com/jcmturner/gokrb5/v8 v8.4.4 // indirect
github.com/jcmturner/rpc/v2 v2.0.3 // indirect
github.com/klauspost/compress v1.17.7 // indirect
github.com/pierrec/lz4/v4 v4.1.21 // indirect
github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475 // indirect
github.com/rogpeppe/go-internal v1.12.0 // indirect
go.uber.org/atomic v1.7.0 // indirect
golang.org/x/crypto v0.19.0 // indirect
)

require (
github.com/IBM/sarama v1.43.0
github.com/RoaringBitmap/roaring v0.4.23 // indirect
github.com/andybalholm/cascadia v1.0.0 // indirect
github.com/blevesearch/go-porterstemmer v1.0.3 // indirect
Expand All @@ -41,10 +62,12 @@ require (
github.com/fsnotify/fsnotify v1.4.7 // indirect
github.com/glycerine/go-unsnap-stream v0.0.0-20181221182339-f9677308dec2 // indirect
github.com/go-errors/errors v1.5.1 // indirect
github.com/golang/protobuf v1.5.0 // indirect
github.com/golang/snappy v0.0.1 // indirect
github.com/golang-migrate/migrate/v4 v4.17.0
github.com/golang/protobuf v1.5.3 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/magiconair/properties v1.8.0 // indirect
github.com/mattn/go-sqlite3 v2.0.3+incompatible
github.com/mitchellh/mapstructure v1.1.2 // indirect
github.com/mschoch/smat v0.2.0 // indirect
github.com/pelletier/go-toml v1.7.0 // indirect
Expand All @@ -60,8 +83,8 @@ require (
github.com/tinylib/msgp v1.1.0 // indirect
github.com/willf/bitset v1.1.10 // indirect
go.etcd.io/bbolt v1.3.5 // indirect
golang.org/x/sys v0.13.0 // indirect
golang.org/x/text v0.13.0 // indirect
golang.org/x/sys v0.17.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/time v0.5.0 // indirect
google.golang.org/protobuf v1.33.0 // indirect
gopkg.in/yaml.v2 v2.2.8 // indirect
Expand Down
Loading

0 comments on commit 998b11d

Please sign in to comment.