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 diff --git a/server/README.md b/server/README.md index d13a4bce2..4af268983 100644 --- a/server/README.md +++ b/server/README.md @@ -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 ' \ + -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 ``` @@ -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 ``` @@ -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` 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..026626a37 --- /dev/null +++ b/server/docker-compose.docker-image.yml @@ -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 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 diff --git a/server/testing-docker-compose.yml b/server/docker-compose.testing.yml similarity index 88% rename from server/testing-docker-compose.yml rename to server/docker-compose.testing.yml index 9e473ec40..6ac105462 100644 --- a/server/testing-docker-compose.yml +++ b/server/docker-compose.testing.yml @@ -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" @@ -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: 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