Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
uzairname committed Oct 20, 2024
1 parent f6cd891 commit 240035e
Show file tree
Hide file tree
Showing 98 changed files with 1,510 additions and 6,834 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/deploy-production.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
runs-on: ubuntu-latest
env:
MIGRATIONS_SCRIPT_PATH: 'migrations/migrate.ts'
INIT_APP_ENDPOINT: 'https://leaderboards.5r.workers.dev/init'
INIT_APP_ENDPOINT: 'https://leaderboards.5r.workers.dev/update'
steps:
- uses: actions/checkout@v2

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/deploy-staging.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
runs-on: ubuntu-latest
env:
MIGRATIONS_SCRIPT_PATH: 'migrations/migrate.ts'
INIT_APP_ENDPOINT: 'https://leaderboards-staging.5r.workers.dev/init'
INIT_APP_ENDPOINT: 'https://leaderboards-staging.5r.workers.dev/update'
steps:
- uses: actions/checkout@v2

Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -337,4 +337,5 @@ dist
# wrangler project

.dev.vars
.wrangler/
.wrangler/
deploy.sh
111 changes: 107 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,107 @@
# Leaderboards🏅

An open source elo rating, matchmaking, and leaderboards system for Discord communities.

# Leaderboards

A Discord bot that handles elo rating, matchmaking, and leaderboards.

# Developing

## Deployment Setup

### Cloudflare Workers

For each environment, create a cloudflare worker. Update the worker's name in `wrangler.toml`. The following variables should match in the worker's environment variables and in `wrangler.toml`

- `ENVIRONMENT`. e.g. "production"

- `BASE_URL` in the format `https://<subdomain>.workers.dev`

### Discord

For each environment, create a Discord app. Save the `DISCORD_TOKEN`, `APPLICATION_ID`, `PUBLIC_KEY`, and `CLIENT_SECRET` variables

In the dev portal, set the _interactions endpoint url_, _linked roles verification url_, and the _oauth redirect URI_ to `https://<subdomain>.workers.dev/<endpoint>` according to the endpoints in `src/main/router.ts`

### Neon Database

For each environment, create a Neon database. Save the pooled connection string as `POSTGRES_URL` or `POSTGRES_URL_TEST`

### Sentry

Create a "browser javascript" Sentry project. Save the `SENTRY_DSN` variable

## Deploying with GitHub Actions

### Environment secrets

For each environment, set these as environment secrets in GitHub Actions

- `DISCORD_TOKEN`, `APPLICATION_ID`, `PUBLIC_KEY`, `CLIENT_SECRET`, and `POSTGRES_URL`

### Repository Secrets

Set the following as repository secrets in GitHub Actions

- `SENTRY_DSN` to the Sentry DSN

- `APP_KEY` to a random string. It's used to authenticate requests to the deploy and test endpoints

- `CF_API_TOKEN` to your Cloudflare API token

### Workflow file

In the workflow file for each environment in `.github/workflows`, set the `INIT_APP_ENDPOINT` environment variable to `https://<subdomain>.workers.dev/init`

## Deploying

### 1. Source code

```bash
npm install
npm install -g wrangler
npx tsc
```

### 2. Migrate

Generate migrations

```
npx drizzle-kit generate:pg
```

Set `POSTGRES_URL` and optionally `POSTGRES_URL_TEST` in `.env`
Migrate database

```bash
tsx migrations/migrate.ts
```

### 3. Deploy worker

Set all of the environment variables in the Cloudflare worker with `wrangler secret put <secret>` for each of `DISCORD_TOKEN`, `APPLICATION_ID`, `PUBLIC_KEY`, `CLIENT_SECRET`, `POSTGRES_URL`, `SENTRY_DSN`, and `APP_KEY`

```bash
wrangler deploy
```

### Initialize app

Initialize the app's slash commands and role connections metadata:

```bash
curl.exe -X POST https://<subdomain>.workers.dev/init -H "Authorization:<APP_KEY>"
```

## Testing

Create a .dev.vars file with a testing `POSTGRES_URL`, `APP_KEY`, and `SENTRY_DSN`

```bash
wrangler dev
curl.exe -X POST <localhost url>/test -H "Authorization:<app key>"
```

<!--
current total gzip size: 339.32 KiB
-->
25 changes: 25 additions & 0 deletions deploy.example.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/bin/bash

# Run TypeScript compiler
npx tsc
if [ $? -ne 0 ]; then
exit $?
fi

# Run migrations
npx tsx migrations/migrate.ts
if [ $? -ne 0 ]; then
exit $?
fi

# Deploy to Cloudflare Workers
wrangler deploy
if [ $? -ne 0 ]; then
exit $?
fi

# Wait for deployment
sleep 3

# Call update endpoint
curl -X POST https://your-worker.your-subdomain.workers.dev/update -H "Authorization:your-app-key"
2 changes: 1 addition & 1 deletion drizzle.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as dotenv from 'dotenv'
dotenv.config()

export default {
schema: 'src/database/schema.ts',
schema: 'src/main/database/schema.ts',
out: 'migrations/migrations',
driver: 'pg',
dbCredentials: {
Expand Down
Loading

0 comments on commit 240035e

Please sign in to comment.