Skip to content

Commit

Permalink
add an ingestion server mode
Browse files Browse the repository at this point in the history
  • Loading branch information
negrel committed Jan 24, 2024
1 parent a414097 commit d54645f
Show file tree
Hide file tree
Showing 24 changed files with 367 additions and 170 deletions.
15 changes: 9 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,35 +9,38 @@ GENENV_FILE ?= ./config/genenv.local.sh
start: .env codegen
source ./.env && go build ./cmd/server
$(DOCKER_COMPOSE) \
-f ./docker-compose.yml \
up --wait -d
-f ./docker-compose.$${PRISME_MODE:-default}.yml \
up --wait
$(DOCKER_COMPOSE) \
-f ./docker-compose.dev.yml \
down
$(DOCKER_COMPOSE) \
-f ./docker-compose.dev.yml \
up --wait -d --force-recreate
up --wait --force-recreate
docker logs -f $(notdir $(CURDIR))-prisme-1 |& bunyan

.PHONY: stop
stop:
$(DOCKER_COMPOSE) \
-f ./docker-compose.dev.yml \
-f ./docker-compose.yml \
-f ./docker-compose.default.yml \
-f ./docker-compose.ingestion.yml \
stop

.PHONY: down
down:
$(DOCKER_COMPOSE) \
-f ./docker-compose.dev.yml \
-f ./docker-compose.yml \
-f ./docker-compose.default.yml \
-f ./docker-compose.ingestion.yml \
down

.PHONY: clean
clean:
$(DOCKER_COMPOSE) \
-f ./docker-compose.dev.yml \
-f ./docker-compose.yml \
-f ./docker-compose.default.yml \
-f ./docker-compose.ingestion.yml \
down --volumes --remove-orphans
rm -f .env

Expand Down
31 changes: 23 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,15 @@

# :gem: `prismeanalytics` - An Open Source, privacy-focused and progressive analytics service.

## Why is the repository empty ?

I've already built a closed source MVP of what Prisme Analytics may look like but
it's not until recently that I decided to open source it.
## Configuration

There will be a lot of changes in the following weeks, be sure to watch and star
the repository to get informed of the progress.
Prisme Analytics server comes with different mode:
* `default`: a full-featured server
* `ingestion`: an event ingestion only server.

## Configuration
Both server use environment variables for configuration.

Configuration of the go server is done through environment variables:
### `default` options

| Variable name | Default value | Description |
|:------------- | :------------ | :---------- |
Expand All @@ -41,6 +39,23 @@ Configuration of the go server is done through environment variables:
| `PRISME_EVENTSTORE_MAX_BATCH_SIZE`| `4096` | Maximum size of batch send to event store. |
| `PRISME_EVENTSTORE_MAX_BATCH_TIMEOUT`| `1m` | Maximum duration before a batch is sent to event store. |

### `ingestion` options

| Variable name | Default value | Description |
|:------------- | :------------ | :---------- |
| `PRISME_ACCESS_LOG`| `/dev/stdout` | Path to access log file. |
| `PRISME_DEBUG`| `false` | Sets log level to debug. |
| `PRISME_PORT`| `80` | Web server listening port. |
| `PRISME_TRUST_PROXY`| `false` | Whether `X-Forwarded-For`, `X-Request-Id` and other proxy related headers should be trusted. |
| `PRISME_CLICKHOUSE_TLS`| `false` | Use a TLS connection for clickhouse. |
| `PRISME_CLICKHOUSE_HOSTPORT`| **required** | A valid `<host>:<port>` string pointing to a clickhouse instance. |
| `PRISME_CLICKHOUSE_DB`| `prisme` | Clickhouse database to use. |
| `PRISME_CLICKHOUSE_USER`| **required** | Clickhouse user to use. |
| `PRISME_CLICKHOUSE_PASSWORD`| **required** | Clickhouse password to use. |
| `PRISME_SOURCE_REGISTRY_SOURCES`| **required** | Comma separated list (without space) of valid sources. |
| `PRISME_EVENTSTORE_MAX_BATCH_SIZE`| `4096` | Maximum size of batch send to event store. |
| `PRISME_EVENTSTORE_MAX_BATCH_TIMEOUT`| `1m` | Maximum duration before a batch is sent to event store. |

## Contributing

If you want to contribute to `prismeanalytics` to add a feature or improve the
Expand Down
17 changes: 0 additions & 17 deletions cmd/server/config.go

This file was deleted.

51 changes: 10 additions & 41 deletions cmd/server/fiber.go → cmd/server/full/fiber.go
Original file line number Diff line number Diff line change
@@ -1,57 +1,28 @@
package main
package full

import (
"github.com/gofiber/fiber/v2"
"github.com/prismelabs/prismeanalytics/internal/config"
"github.com/prismelabs/prismeanalytics/cmd/server/wired"
"github.com/prismelabs/prismeanalytics/internal/handlers"
"github.com/prismelabs/prismeanalytics/internal/middlewares"
)

// ProvideFiber is a wire provider for fiber.App.
func ProvideFiber(
cfg config.Config,
viewsEngine fiber.Views,
loggerMiddleware middlewares.Logger,
accessLogMiddleware middlewares.AccessLog,
requestIdMiddleware middlewares.RequestId,
staticMiddleware middlewares.Static,
withSessionMiddleware middlewares.WithSession,
eventsCorsMiddleware middlewares.EventsCors,
faviconMiddleware middlewares.Favicon,
eventsRateLimiterMiddleware middlewares.EventsRateLimiter,
getSignUpHandler handlers.GetSignUp,
postSignUpHander handlers.PostSignUp,
getSignInHandler handlers.GetSignIn,
postSignInHander handlers.PostSignIn,
faviconMiddleware middlewares.Favicon,
getIndexHander handlers.GetIndex,
getSignInHandler handlers.GetSignIn,
getSignUpHandler handlers.GetSignUp,
minimalFiber wired.MinimalFiber,
notFoundHandler handlers.NotFound,
postPageViewEventHandler handlers.PostPageViewEvent,
postSignInHander handlers.PostSignIn,
postSignUpHander handlers.PostSignUp,
withSessionMiddleware middlewares.WithSession,
) *fiber.App {
fiberCfg := fiber.Config{
ServerHeader: "prisme",
StrictRouting: true,
AppName: "Prisme Analytics",
DisableStartupMessage: true,
ErrorHandler: func(_ *fiber.Ctx, _ error) error {
// Errors are handled manually by a middleware.
return nil
},
Views: viewsEngine,
ViewsLayout: "layouts/empty",
}
if cfg.Server.TrustProxy {
fiberCfg.EnableIPValidation = false
fiberCfg.ProxyHeader = fiber.HeaderXForwardedFor
} else {
fiberCfg.EnableIPValidation = true
fiberCfg.ProxyHeader = ""
}

app := fiber.New(fiberCfg)

app.Use(fiber.Handler(requestIdMiddleware))
app.Use(fiber.Handler(accessLogMiddleware))
app.Use(fiber.Handler(loggerMiddleware))
app := (*fiber.App)(minimalFiber)

// Public endpoints.
app.Use(fiber.Handler(faviconMiddleware))
Expand All @@ -62,8 +33,6 @@ func ProvideFiber(
)
app.Post("/api/v1/events/pageviews", fiber.Handler(postPageViewEventHandler))

app.Use("/static", fiber.Handler(staticMiddleware))

app.Get("/sign_up", fiber.Handler(getSignUpHandler))
app.Post("/sign_up", fiber.Handler(postSignUpHander))

Expand Down
60 changes: 30 additions & 30 deletions cmd/server/wire.go → cmd/server/full/wire.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
//go:build wireinject
// +build wireinject

package main
package full

import (
"github.com/google/wire"
"github.com/prismelabs/prismeanalytics/cmd/server/wired"
"github.com/prismelabs/prismeanalytics/internal/clickhouse"
"github.com/prismelabs/prismeanalytics/internal/config"
"github.com/prismelabs/prismeanalytics/internal/handlers"
"github.com/prismelabs/prismeanalytics/internal/middlewares"
"github.com/prismelabs/prismeanalytics/internal/postgres"
Expand All @@ -18,39 +18,39 @@ import (
"github.com/prismelabs/prismeanalytics/internal/services/users"
)

func initialize(logger BootstrapLogger) App {
func Initialize(logger wired.BootstrapLogger) wired.App {
wire.Build(
ProvideConfig,
wire.FieldsOf(new(config.Config), "Server"),
wire.FieldsOf(new(config.Config), "Postgres"),
wire.FieldsOf(new(config.Config), "Clickhouse"),
postgres.ProvidePg,
clickhouse.ProvideCh,
sessions.ProvideService,
users.ProvideService,
ProvideFiber,
auth.ProvideService,
clickhouse.ProvideCh,
eventstore.ProvideClickhouseService,
sourceregistry.ProvideEnvVarService,
uaparser.ProvideService,
ProvideLogger,
ProvideFiberViewsEngine,
middlewares.ProvideStatic,
middlewares.ProvideRequestId,
middlewares.ProvideAccessLog,
middlewares.ProvideLogger,
middlewares.ProvideWithSession,
middlewares.ProvideFavicon,
middlewares.ProvideEventsCors,
middlewares.ProvideEventsRateLimiter,
handlers.ProvideGetSignUp,
handlers.ProvidePostSignUp,
handlers.ProvideGetSignIn,
handlers.ProvidePostSignIn,
handlers.ProvideGetIndex,
handlers.ProvideGetSignIn,
handlers.ProvideGetSignUp,
handlers.ProvideNotFound,
handlers.ProvidePostEventsPageViews,
ProvideFiber,
ProvideApp,
handlers.ProvidePostSignIn,
handlers.ProvidePostSignUp,
middlewares.ProvideAccessLog,
middlewares.ProvideEventsCors,
middlewares.ProvideEventsRateLimiter,
middlewares.ProvideFavicon,
middlewares.ProvideLogger,
middlewares.ProvideRequestId,
middlewares.ProvideStatic,
middlewares.ProvideWithSession,
postgres.ProvidePg,
sessions.ProvideService,
sourceregistry.ProvideEnvVarService,
uaparser.ProvideService,
users.ProvideService,
wired.ProvideApp,
wired.ProvideClickhouseConfig,
wired.ProvideFiberViewsEngine,
wired.ProvideLogger,
wired.ProvideMinimalFiber,
wired.ProvidePostgresConfig,
wired.ProvideServerConfig,
)
return App{}
return wired.App{}
}
53 changes: 27 additions & 26 deletions cmd/server/wire_gen.go → cmd/server/full/wire_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions cmd/server/ingestion/fiber.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package ingestion

import (
"github.com/gofiber/fiber/v2"
"github.com/prismelabs/prismeanalytics/cmd/server/wired"
"github.com/prismelabs/prismeanalytics/internal/handlers"
"github.com/prismelabs/prismeanalytics/internal/middlewares"
)

// ProvideFiber is a wire provider for fiber.App.
func ProvideFiber(
eventsCorsMiddleware middlewares.EventsCors,
eventsRateLimiterMiddleware middlewares.EventsRateLimiter,
minimalFiber wired.MinimalFiber,
postPageViewEventHandler handlers.PostPageViewEvent,
) *fiber.App {
app := (*fiber.App)(minimalFiber)

app.Use("/api/v1/events/*",
fiber.Handler(eventsCorsMiddleware),
fiber.Handler(eventsRateLimiterMiddleware),
)
app.Post("/api/v1/events/pageviews", fiber.Handler(postPageViewEventHandler))

return app
}
Loading

0 comments on commit d54645f

Please sign in to comment.