From db3764e3e21cc9464a4f9cad190d339f4304f8bc Mon Sep 17 00:00:00 2001 From: Auke Booij <164426+abooij@users.noreply.github.com> Date: Mon, 9 Oct 2023 09:55:02 +0200 Subject: [PATCH 01/12] starting to clean up server readme --- server/README.md | 43 +++++++++++++++++-------------------------- 1 file changed, 17 insertions(+), 26 deletions(-) diff --git a/server/README.md b/server/README.md index d13a4bce2..ddb4918c9 100644 --- a/server/README.md +++ b/server/README.md @@ -23,47 +23,38 @@ 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 +```bash +rustup default stable ``` Afterwards please install the following components: -``` -$ rustup component add clippy rust-src cargo rustfmt +```bash +rustup component add clippy rust-src cargo rustfmt ``` -Install SQLx CLI for database migrations -``` -$ cargo install sqlx-cli +Also build additional rust dependencies: +```bash +cargo install sqlx-cli cargo-watch ``` +(`cargo-watch` is used for automatic reload while developing and can be skipped) -For automatic reload while developing: -``` -$ cargo install cargo-watch +Finally, clone and build Svix: + +```bash +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: -``` -# Move to the inner svix-server directory. +```bash cd svix-server cargo watch -x run ``` From 3b40e9593f531ff0eeb7025df1282067b6461dbf Mon Sep 17 00:00:00 2001 From: Auke Booij <164426+abooij@users.noreply.github.com> Date: Mon, 9 Oct 2023 10:58:12 +0200 Subject: [PATCH 02/12] reorganize docker compose files --- server/README.md | 39 ++++++++++--------- ...er-compose.yml => docker-compose.base.yml} | 20 +++------- server/docker-compose.docker-image.yml | 17 ++++++++ ...compose.yml => docker-compose.testing.yml} | 20 +--------- 4 files changed, 45 insertions(+), 51 deletions(-) rename server/{docker-compose.yml => docker-compose.base.yml} (66%) create mode 100644 server/docker-compose.docker-image.yml rename server/{testing-docker-compose.yml => docker-compose.testing.yml} (87%) diff --git a/server/README.md b/server/README.md index ddb4918c9..5d40c4c34 100644 --- a/server/README.md +++ b/server/README.md @@ -26,24 +26,24 @@ The easiest way is to use [rustup](https://rustup.rs/). 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: -```bash +```sh rustup default stable ``` Afterwards please install the following components: -```bash +```sh rustup component add clippy rust-src cargo rustfmt ``` Also build additional rust dependencies: -```bash +```sh cargo install sqlx-cli cargo-watch ``` (`cargo-watch` is used for automatic reload while developing and can be skipped) Finally, clone and build Svix: -```bash +```sh git clone https://github.com/svix/svix-webhooks cd svix-webhooks/server/ cargo install --path svix-server @@ -53,29 +53,25 @@ cargo install --path svix-server ## Run the development server -To run the auto-reloading development server run: -```bash -cd svix-server -cargo watch -x run +Svix needs a few ancillary services. +```sh +ln -s docker-compose.base.yml docker-compose.yml +docker compose up -d ``` -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. +### setting an auth token Now generate an auth token, you can do it by running: ``` cargo run jwt generate ``` +TODO: add instruction for adding auth token to configuration See [the main README](../README.md) for instructions on how to generate it in production. @@ -96,6 +92,13 @@ cargo sqlx migrate info cargo sqlx migrate revert ``` +### Starting Svix + +To run the auto-reloading development server run: +```sh +cargo watch -x run +``` + ## Creating new SQL migration As you saw before you run/revert migrations. To generate new migrations you just run: @@ -121,7 +124,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` diff --git a/server/docker-compose.yml b/server/docker-compose.base.yml similarity index 66% rename from server/docker-compose.yml rename to server/docker-compose.base.yml index 2cec23b08..425d5326a 100644 --- a/server/docker-compose.yml +++ b/server/docker-compose.base.yml @@ -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 @@ -43,6 +31,8 @@ services: command: "--save 60 500 --appendonly yes --appendfsync everysec" volumes: - "redis-data:/data" + ports: + - "6379:6379" volumes: postgres-data: diff --git a/server/docker-compose.docker-image.yml b/server/docker-compose.docker-image.yml new file mode 100644 index 000000000..8ee03d442 --- /dev/null +++ b/server/docker-compose.docker-image.yml @@ -0,0 +1,17 @@ +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" + ports: + - "8071:8071" + depends_on: + - pgbouncer + - redis diff --git a/server/testing-docker-compose.yml b/server/docker-compose.testing.yml similarity index 87% rename from server/testing-docker-compose.yml rename to server/docker-compose.testing.yml index 9e473ec40..b65aead0e 100644 --- a/server/testing-docker-compose.yml +++ b/server/docker-compose.testing.yml @@ -1,20 +1,6 @@ -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" @@ -85,7 +71,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: From 41bcc6bfefd756e697b264eafcccd93e79e459a0 Mon Sep 17 00:00:00 2001 From: Auke Booij <164426+abooij@users.noreply.github.com> Date: Mon, 9 Oct 2023 10:59:30 +0200 Subject: [PATCH 03/12] make code into shell --- server/README.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/server/README.md b/server/README.md index 5d40c4c34..bfb12659e 100644 --- a/server/README.md +++ b/server/README.md @@ -68,7 +68,7 @@ cp development.env .env ### setting an auth token Now generate an auth token, you can do it by running: -``` +```sh cargo run jwt generate ``` TODO: add instruction for adding auth token to configuration @@ -80,12 +80,12 @@ See [the main README](../README.md) for instructions on how to generate it in pr 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 @@ -102,7 +102,7 @@ cargo watch -x run ## 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 ``` @@ -113,7 +113,7 @@ And fill up the created migration files. Please run these two commands before pushing code: -``` +```sh cargo clippy --fix cargo fmt ``` From 0cbb6400e905fe639771c0b6374c241ea7413a64 Mon Sep 17 00:00:00 2001 From: Auke Booij <164426+abooij@users.noreply.github.com> Date: Mon, 9 Oct 2023 11:04:45 +0200 Subject: [PATCH 04/12] wrap up readme changes --- server/README.md | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/server/README.md b/server/README.md index bfb12659e..d5672ce4c 100644 --- a/server/README.md +++ b/server/README.md @@ -25,13 +25,9 @@ You would need a working Rust complier in order to build Svix server. The easiest way is to use [rustup](https://rustup.rs/). 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: +Once rustup is installed, switch to the `stable` toolchain and install required components: ```sh rustup default stable -``` - -Afterwards please install the following components: -```sh rustup component add clippy rust-src cargo rustfmt ``` @@ -71,9 +67,9 @@ Now generate an auth token, you can do it by running: ```sh cargo run jwt generate ``` -TODO: add instruction for adding auth token to configuration +Copy-paste this value into the value of `SVIX_JWT_SECRET` in `.env`. -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 From 244df848d250da8772599c8f53a8cef10507b905 Mon Sep 17 00:00:00 2001 From: Auke Booij <164426+abooij@users.noreply.github.com> Date: Mon, 9 Oct 2023 11:04:54 +0200 Subject: [PATCH 05/12] fix default port numbers in development.env --- server/svix-server/development.env | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/server/svix-server/development.env b/server/svix-server/development.env index c8e8c7496..8db874bef 100644 --- a/server/svix-server/development.env +++ b/server/svix-server/development.env @@ -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:xivs@127.0.0.1:5672/%2f" -SVIX_REDIS_DSN=redis://localhost:8078 +SVIX_REDIS_DSN=redis://localhost:6379 From d5f6cc092cd4e5ba3fd95016c4c2194c45c5854d Mon Sep 17 00:00:00 2001 From: Auke Booij <164426+abooij@users.noreply.github.com> Date: Mon, 9 Oct 2023 11:06:34 +0200 Subject: [PATCH 06/12] improve heading title --- server/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/README.md b/server/README.md index d5672ce4c..47f1d6806 100644 --- a/server/README.md +++ b/server/README.md @@ -61,7 +61,7 @@ cd svix-server/ cp development.env .env ``` -### setting an auth token +### Generating an auth token Now generate an auth token, you can do it by running: ```sh From 40b264a6ba73ed4ea74a0211bc4e6ccf81cd07f2 Mon Sep 17 00:00:00 2001 From: Auke Booij <164426+abooij@users.noreply.github.com> Date: Mon, 9 Oct 2023 11:25:58 +0200 Subject: [PATCH 07/12] move env var to `docker-compose.docker-image.yml` --- server/docker-compose.docker-image.yml | 2 ++ server/docker-compose.override.yml | 11 ----------- 2 files changed, 2 insertions(+), 11 deletions(-) delete mode 100644 server/docker-compose.override.yml diff --git a/server/docker-compose.docker-image.yml b/server/docker-compose.docker-image.yml index 8ee03d442..e0d8fdd45 100644 --- a/server/docker-compose.docker-image.yml +++ b/server/docker-compose.docker-image.yml @@ -2,6 +2,8 @@ include: - docker-compose.base.yml services: backend: + environment: + SVIX_JWT_SECRET: "x" build: context: . dockerfile: Dockerfile diff --git a/server/docker-compose.override.yml b/server/docker-compose.override.yml deleted file mode 100644 index 2dc394506..000000000 --- a/server/docker-compose.override.yml +++ /dev/null @@ -1,11 +0,0 @@ -version: "3.7" -services: - backend: - environment: - SVIX_JWT_SECRET: "x" - pgbouncer: - ports: - - "8079:5432" # Needed for sqlx - redis: - ports: - - "8078:6379" # Needed for sqlx From 58ab76ea4ed6d52c3a037a2b0401f8dd7131d6fb Mon Sep 17 00:00:00 2001 From: Auke Booij <164426+abooij@users.noreply.github.com> Date: Mon, 9 Oct 2023 11:34:02 +0200 Subject: [PATCH 08/12] fix auth token instructions --- server/README.md | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/server/README.md b/server/README.md index 47f1d6806..d12e016a9 100644 --- a/server/README.md +++ b/server/README.md @@ -67,7 +67,7 @@ Now generate an auth token, you can do it by running: ```sh cargo run jwt generate ``` -Copy-paste this value into the value of `SVIX_JWT_SECRET` in `.env`. +This is the `Bearer` token that you can use to authenticate requests. See [the main README](../README.md) for instructions on how to generate the auth token in production. @@ -95,6 +95,15 @@ To run the auto-reloading development server run: cargo watch -x run ``` +Test the server: +```sh +curl -X 'GET' \ + 'http://localhost:8071/api/v1/app/' \ + -H 'Authorization: Bearer \ + -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: From 342b307b8d5953d353ec662d8d5acb0dbf26da48 Mon Sep 17 00:00:00 2001 From: Auke Booij <164426+abooij@users.noreply.github.com> Date: Mon, 9 Oct 2023 11:35:16 +0200 Subject: [PATCH 09/12] fix syntax --- server/README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/server/README.md b/server/README.md index d12e016a9..4af268983 100644 --- a/server/README.md +++ b/server/README.md @@ -99,10 +99,10 @@ Test the server: ```sh curl -X 'GET' \ 'http://localhost:8071/api/v1/app/' \ - -H 'Authorization: Bearer \ + -H 'Authorization: Bearer ' \ -H 'accept: application/json' \ -H 'Content-Type: application/json' - ``` +``` ## Creating new SQL migration From ac960d44ee30e291d28b99cb82a191f80f350ebe Mon Sep 17 00:00:00 2001 From: Auke Booij <164426+abooij@users.noreply.github.com> Date: Sat, 14 Oct 2023 13:29:42 +0200 Subject: [PATCH 10/12] dedupe `environment` key in docker yml --- server/docker-compose.docker-image.yml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/server/docker-compose.docker-image.yml b/server/docker-compose.docker-image.yml index e0d8fdd45..107f9c18b 100644 --- a/server/docker-compose.docker-image.yml +++ b/server/docker-compose.docker-image.yml @@ -2,8 +2,6 @@ include: - docker-compose.base.yml services: backend: - environment: - SVIX_JWT_SECRET: "x" build: context: . dockerfile: Dockerfile @@ -12,6 +10,7 @@ services: 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: From e46939303880695e98cb85a590b510bb4a19a937 Mon Sep 17 00:00:00 2001 From: Auke Booij <164426+abooij@users.noreply.github.com> Date: Sat, 14 Oct 2023 13:36:56 +0200 Subject: [PATCH 11/12] fix ci --- .github/workflows/server-ci.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/server-ci.yml b/.github/workflows/server-ci.yml index 883a24448..83fbaa514 100644 --- a/.github/workflows/server-ci.yml +++ b/.github/workflows/server-ci.yml @@ -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 @@ -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 From 36d1d67a3c043bcde890f2b5c80c9081df8b186e Mon Sep 17 00:00:00 2001 From: Auke Booij <164426+abooij@users.noreply.github.com> Date: Wed, 18 Oct 2023 23:00:30 +0200 Subject: [PATCH 12/12] set docker file version explicitly --- server/docker-compose.docker-image.yml | 1 + server/docker-compose.testing.yml | 1 + 2 files changed, 2 insertions(+) diff --git a/server/docker-compose.docker-image.yml b/server/docker-compose.docker-image.yml index 107f9c18b..026626a37 100644 --- a/server/docker-compose.docker-image.yml +++ b/server/docker-compose.docker-image.yml @@ -1,3 +1,4 @@ +version: "3.7" include: - docker-compose.base.yml services: diff --git a/server/docker-compose.testing.yml b/server/docker-compose.testing.yml index b65aead0e..6ac105462 100644 --- a/server/docker-compose.testing.yml +++ b/server/docker-compose.testing.yml @@ -1,3 +1,4 @@ +version: "3.7" include: - docker-compose.base.yml services: