Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve server onboarding #1095

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
Open
4 changes: 2 additions & 2 deletions .github/workflows/server-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ jobs:
run: ./generate-openapi.sh --check

- name: Start dependencies
run: docker-compose -f "server/testing-docker-compose.yml" up -d
run: docker-compose -f "server/docker-compose.testing.yml" up -d

- name: Clippy
uses: actions-rs/cargo@v1
Expand Down Expand Up @@ -81,7 +81,7 @@ jobs:
run: ./run-tests.sh

- name: Stop dependencies
run: docker-compose -f "server/testing-docker-compose.yml" down
run: docker-compose -f "server/docker-compose.testing.yml" down
# deny-check:
# name: cargo-deny check
# runs-on: ubuntu-latest
Expand Down
95 changes: 47 additions & 48 deletions server/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,92 +23,91 @@ For information on how to use this server please refer to the [running the serve

You would need a working Rust complier in order to build Svix server.
The easiest way is to use [rustup](https://rustup.rs/).

```
# Clone the repository
git clone https://github.com/svix/svix-webhooks
# Change to the source directory
cd svix-webhooks/server/
# Build
cargo install --path svix-server
```

# Development

## Setup your environment

Make sure you have a working Rust compiled (e.g. by using [rustup](https://rustup.rs/)).

Once rustup is installed make sure to set up the `stable` toolchain by running:
```
$ rustup default stable
Once rustup is installed, switch to the `stable` toolchain and install required components:
```sh
rustup default stable
rustup component add clippy rust-src cargo rustfmt
```

Afterwards please install the following components:
```
$ rustup component add clippy rust-src cargo rustfmt
Also build additional rust dependencies:
```sh
cargo install sqlx-cli cargo-watch
```
(`cargo-watch` is used for automatic reload while developing and can be skipped)

Install SQLx CLI for database migrations
```
$ cargo install sqlx-cli
```
Finally, clone and build Svix:

For automatic reload while developing:
```
$ cargo install cargo-watch
```sh
git clone https://github.com/svix/svix-webhooks
cd svix-webhooks/server/
cargo install --path svix-server
```

# Development

## Run the development server

To run the auto-reloading development server run:
Svix needs a few ancillary services.
```sh
ln -s docker-compose.base.yml docker-compose.yml
docker compose up -d
```
# Move to the inner svix-server directory.
cd svix-server
cargo watch -x run
```

This however will fail, as you also need to point the server to the database and setup a few other configurations.

The easiest way to achieve that is to use docker-compose to setup a dockerize development environment, and the related config.

```
# From the svix inner directory
Setting some server configuration:
```sh
cd svix-server/
cp development.env .env
# Set up docker (may need sudo depending on your setup)
docker-compose up
```

Now run `cargo watch -x run` again to start the development server against your local docker environment.
### Generating an auth token

Now generate an auth token, you can do it by running:
```
```sh
cargo run jwt generate
```
This is the `Bearer` token that you can use to authenticate requests.

See [the main README](../README.md) for instructions on how to generate it in production.
See [the main README](../README.md) for instructions on how to generate the auth token in production.

### Run the SQL migrations

One last missing piece to the puzzle is running the SQL migrations.

From the same directory as the `.env` file run:
```
```sh
cargo sqlx migrate run
```

More useful commands:
```
```sh
# View the migrations and their status
cargo sqlx migrate info
# Reverting the latest migration
cargo sqlx migrate revert
```

### Starting Svix

To run the auto-reloading development server run:
```sh
cargo watch -x run
```

Test the server:
```sh
curl -X 'GET' \
'http://localhost:8071/api/v1/app/' \
-H 'Authorization: Bearer <what you generated before>' \
-H 'accept: application/json' \
-H 'Content-Type: application/json'
```

## Creating new SQL migration

As you saw before you run/revert migrations. To generate new migrations you just run:
```
```sh
cargo sqlx migrate add -r MIGRATION_NAME
```

Expand All @@ -119,7 +118,7 @@ And fill up the created migration files.

Please run these two commands before pushing code:

```
```sh
cargo clippy --fix
cargo fmt
```
Expand All @@ -130,7 +129,7 @@ By default, `cargo test` will run the full test suite which assumes a running Po
These databases are configured with the same environment variables as with running the actual server.

The easiest way to get these tests to pass is to:
1. Use the `testing-docker-compose.yml` file with `docker-compose` to launch the databases on their default ports.
1. Start background services using `docker compose -f docker-compose.testing.yml up`
2. Create a `.env` file as you would when running the server for real.
3. Migrate the database with `cargo run -- migrate`.
4. Run `cargo test --all-targets`
Expand Down
20 changes: 5 additions & 15 deletions server/docker-compose.yml → server/docker-compose.base.yml
Original file line number Diff line number Diff line change
@@ -1,26 +1,14 @@
version: "3.7"
services:
backend:
build:
context: .
dockerfile: Dockerfile
image: svix/svix-server
environment:
WAIT_FOR: "true" # We want to wait for the default services
SVIX_REDIS_DSN: "redis://redis:6379"
SVIX_DB_DSN: "postgresql://postgres:postgres@pgbouncer/postgres"
ports:
- "8071:8071"
depends_on:
- pgbouncer
- redis

postgres:
image: postgres:13.4
command: postgres -c 'max_connections=500'
volumes:
- "postgres-data:/var/lib/postgresql/data/"
environment:
POSTGRES_PASSWORD: postgres
ports:
- "5432:5432"

pgbouncer:
image: edoburu/pgbouncer:1.15.0
Expand All @@ -43,6 +31,8 @@ services:
command: "--save 60 500 --appendonly yes --appendfsync everysec"
volumes:
- "redis-data:/data"
ports:
- "6379:6379"

volumes:
postgres-data:
Expand Down
19 changes: 19 additions & 0 deletions server/docker-compose.docker-image.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
version: "3.7"
include:
- docker-compose.base.yml
services:
backend:
build:
context: .
dockerfile: Dockerfile
image: svix/svix-server
environment:
WAIT_FOR: "true" # We want to wait for the default services
SVIX_REDIS_DSN: "redis://redis:6379"
SVIX_DB_DSN: "postgresql://postgres:postgres@pgbouncer/postgres"
SVIX_JWT_SECRET: "x"
ports:
- "8071:8071"
depends_on:
- pgbouncer
- redis
11 changes: 0 additions & 11 deletions server/docker-compose.override.yml

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,20 +1,7 @@
version: "3.7"
include:
- docker-compose.base.yml
services:
postgres:
image: postgres:13.4
command: postgres -c 'max_connections=500'
volumes:
- "postgres-data:/var/lib/postgresql/data/"
environment:
POSTGRES_PASSWORD: postgres
ports:
- "5432:5432"

redis:
image: "redis:7.0.10-alpine"
ports:
- "6379:6379"

# Redis cluster
redis-cluster:
image: "bitnami/redis-cluster:7.0.10"
Expand Down Expand Up @@ -85,7 +72,5 @@ services:
- rabbitmq_data:/var/lib/rabbitmq
- ./rabbit/enabled_plugins:/etc/rabbitmq/enabled_plugins
- ./rabbit/plugins:/usr/lib/rabbitmq/plugins

volumes:
postgres-data:
rabbitmq_data:
4 changes: 2 additions & 2 deletions server/svix-server/development.env
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Example .env file for development
DATABASE_URL="postgresql://postgres:postgres@localhost:8079/postgres" # For sqlx
DATABASE_URL="postgresql://postgres:postgres@localhost:5432/postgres" # For sqlx
SVIX_CACHE_TYPE=memory
SVIX_JWT_SECRET=x
SVIX_LOG_LEVEL=trace
SVIX_QUEUE_TYPE=redis
SVIX_RABBIT_DSN="amqp://xivs:[email protected]:5672/%2f"
SVIX_REDIS_DSN=redis://localhost:8078
SVIX_REDIS_DSN=redis://localhost:6379
Loading