Skip to content

Commit

Permalink
refactor(server): make redis required module (toeverything#9121)
Browse files Browse the repository at this point in the history
  • Loading branch information
forehalo committed Dec 13, 2024
1 parent 81c6803 commit 0e73737
Show file tree
Hide file tree
Showing 40 changed files with 284 additions and 720 deletions.
5 changes: 5 additions & 0 deletions .devcontainer/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ services:
network_mode: service:db
environment:
DATABASE_URL: postgresql://affine:affine@db:5432/affine
REDIS_SERVER_HOST: redis

db:
image: postgres:latest
Expand All @@ -21,6 +22,10 @@ services:
POSTGRES_PASSWORD: affine
POSTGRES_USER: affine
POSTGRES_DB: affine
redis:
image: redis
ports:
- 6379:6379

volumes:
postgres-data:
4 changes: 4 additions & 0 deletions .docker/dev/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
DATABASE_LOCATION=./postgres
DB_PASSWORD=affine
DB_USERNAME=affine
DB_DATABASE_NAME=affine
3 changes: 3 additions & 0 deletions .docker/dev/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
postgres
.env
compose.yml
28 changes: 28 additions & 0 deletions .docker/dev/compose.yml.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: affine_dev_services
services:
postgres:
env_file:
- .env
image: postgres:16
ports:
- 5432:5432
environment:
POSTGRES_PASSWORD: ${DB_PASSWORD}
POSTGRES_USER: ${DB_USERNAME}
POSTGRES_DB: ${DB_DATABASE_NAME}
volumes:
- ${DATABASE_LOCATION}:/var/lib/postgresql/data

redis:
image: redis:latest
ports:
- 6379:6379

mailhog:
image: mailhog/mailhog:latest
ports:
- 1025:1025
- 8025:8025

networks:
dev:
20 changes: 20 additions & 0 deletions .github/workflows/build-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,7 @@ jobs:
NODE_ENV: test
DISTRIBUTION: web
DATABASE_URL: postgresql://affine:affine@localhost:5432/affine
REDIS_SERVER_HOST: localhost
services:
postgres:
image: postgres
Expand All @@ -316,6 +317,10 @@ jobs:
--health-retries 5
ports:
- 5432:5432
redis:
image: redis
ports:
- 6379:6379
mailer:
image: mailhog/mailhog
ports:
Expand Down Expand Up @@ -379,6 +384,7 @@ jobs:
NODE_ENV: test
DISTRIBUTION: web
DATABASE_URL: postgresql://affine:affine@localhost:5432/affine
REDIS_SERVER_HOST: localhost
services:
postgres:
image: postgres
Expand All @@ -391,6 +397,10 @@ jobs:
--health-retries 5
ports:
- 5432:5432
redis:
image: redis
ports:
- 6379:6379
mailer:
image: mailhog/mailhog
ports:
Expand Down Expand Up @@ -461,6 +471,7 @@ jobs:
DISTRIBUTION: web
DATABASE_URL: postgresql://affine:affine@localhost:5432/affine
IN_CI_TEST: true
REDIS_SERVER_HOST: localhost
strategy:
fail-fast: false
matrix:
Expand All @@ -480,6 +491,10 @@ jobs:
--health-retries 5
ports:
- 5432:5432
redis:
image: redis
ports:
- 6379:6379
steps:
- uses: actions/checkout@v4

Expand Down Expand Up @@ -532,6 +547,7 @@ jobs:
env:
DISTRIBUTION: web
DATABASE_URL: postgresql://affine:affine@localhost:5432/affine
REDIS_SERVER_HOST: localhost
IN_CI_TEST: true
strategy:
fail-fast: false
Expand Down Expand Up @@ -566,6 +582,10 @@ jobs:
--health-retries 5
ports:
- 5432:5432
redis:
image: redis
ports:
- 6379:6379
mailer:
image: mailhog/mailhog
ports:
Expand Down
10 changes: 10 additions & 0 deletions .github/workflows/copilot-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ jobs:
NODE_ENV: test
DISTRIBUTION: web
DATABASE_URL: postgresql://affine:affine@localhost:5432/affine
REDIS_SERVER_HOST: localhost
services:
postgres:
image: postgres
Expand All @@ -54,6 +55,10 @@ jobs:
--health-retries 5
ports:
- 5432:5432
redis:
image: redis
ports:
- 6379:6379
mailer:
image: mailhog/mailhog
ports:
Expand Down Expand Up @@ -100,6 +105,7 @@ jobs:
env:
DISTRIBUTION: web
DATABASE_URL: postgresql://affine:affine@localhost:5432/affine
REDIS_SERVER_HOST: localhost
IN_CI_TEST: true
strategy:
fail-fast: false
Expand All @@ -120,6 +126,10 @@ jobs:
--health-retries 5
ports:
- 5432:5432
redis:
image: redis
ports:
- 6379:6379
steps:
- uses: actions/checkout@v4

Expand Down
103 changes: 27 additions & 76 deletions docs/developing-server.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,109 +5,60 @@ This document explains how to start server (@affine/server) locally with Docker
> This document is not guaranteed to be up-to-date.
> If you find any outdated information, please feel free to open an issue or submit a PR.
## Run postgresql in docker
## Run required dev services in docker compose

```
docker pull postgres
docker run --rm --name affine-postgres -e POSTGRES_PASSWORD=affine -p 5432:5432 -v ~/Documents/postgres:/var/lib/postgresql/data postgres
```

### Optionally, use a dedicated volume
cp ./.docker/dev/compose.yml.example ./.docker/dev/compose.yml
cp ./.docker/dev/.env.example ./.docker/dev/.env
```
docker volume create affine-postgres
docker run --rm --name affine-postgres -e POSTGRES_PASSWORD=affine -p 5432:5432 -v affine-postgres:/var/lib/postgresql/data postgres
docker compose -f ./.docker/dev/compose.yml up -d
```

### mailhog (for local testing)
## Build native packages (you need to setup rust toolchain first)

```
docker run --rm --name mailhog -p 1025:1025 -p 8025:8025 mailhog/mailhog
# build native
yarn workspace @affine/server-native build
```

## prepare db
## Prepare dev environment

```
docker ps
docker exec -it affine-postgres psql -U postgres ## `affine-postgres` is the container name from the previous step
```
cd packages/backend/server
### in the terminal, following the example to user & table

```
psql (15.3 (Debian 15.3-1.pgdg120+1))
Type "help" for help.
postgres=# CREATE USER affine WITH PASSWORD 'affine';
CREATE ROLE
postgres=# ALTER USER affine WITH SUPERUSER;
ALTER ROLE
postgres=# CREATE DATABASE affine;
CREATE DATABASE
postgres=# \du
List of roles
Role name | Attributes | Member of
-----------+------------------------------------------------------------+-----------
affine | Superuser | {}
postgres | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
cp .env.example .env
yarn prisma db push
yarn data-migration run
```

### Set the following config to `packages/backend/server/.env`

In the following setup, we assume you have postgres server running at localhost:5432 and mailhog running at localhost:1025.

When logging in via email, you will see the mail arriving at localhost:8025 in a browser.
## Start server

```
DATABASE_URL="postgresql://affine:affine@localhost:5432/affine"
MAILER_SENDER="[email protected]"
MAILER_USER="auth"
MAILER_PASSWORD="auth"
MAILER_HOST="localhost"
MAILER_PORT="1025"
yarn dev
```

## Prepare prisma

```
yarn workspace @affine/server prisma db push
yarn workspace @affine/server data-migration run
```
when server started, it will created a default user for testing:

Note, you may need to do it again if db schema changed.
- email: [email protected]
- name: Dev User
- password: dev

### Enable prisma studio
## Start frontend

```
yarn workspace @affine/server prisma studio
```

## Build native packages (you need to setup rust toolchain first)

```
# build native
yarn workspace @affine/server-native build
yarn workspace @affine/native build
# at project root
yarn dev
```

## start server

```
yarn workspace @affine/server dev
```
## Done

when server started, it will created a default user:
Now you should be able to start developing affine with server enabled.

email: [email protected]
name: Dev User
password: dev
## Bonus

## start core (web)
### Enable prisma studio (Database GUI)

```
yarn dev
# available at http://localhost:5555
yarn prisma studio
```

## Done

Now you should be able to start developing affine with server enabled.
11 changes: 8 additions & 3 deletions packages/backend/server/.env.example
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
# AFFINE_SERVER_PORT=3010
# AFFINE_SERVER_HOST=app.affine.pro
# AFFINE_SERVER_HTTPS=true
# DATABASE_URL="postgres://affine:affine@localhost:5432/affine"
# REDIS_SERVER_HOST=localhost

# MAILER_HOST=localhost
# MAILER_PORT=1025
# MAILER_SENDER="[email protected]"
# MAILER_USER="[email protected]"
# MAILER_PASSWORD="affine"
# MAILER_SECURE=false
2 changes: 2 additions & 0 deletions packages/backend/server/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { MailModule } from './base/mailer';
import { MetricsModule } from './base/metrics';
import { MutexModule } from './base/mutex';
import { PrismaModule } from './base/prisma';
import { RedisModule } from './base/redis';
import { RuntimeModule } from './base/runtime';
import { StorageProviderModule } from './base/storage';
import { RateLimiterModule } from './base/throttler';
Expand All @@ -42,6 +43,7 @@ export const FunctionalityModules = [
ConfigModule.forRoot(),
RuntimeModule,
EventModule,
RedisModule,
CacheModule,
MutexModule,
PrismaModule,
Expand Down
10 changes: 1 addition & 9 deletions packages/backend/server/src/app.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { Type } from '@nestjs/common';
import { NestFactory } from '@nestjs/core';
import type { NestExpressApplication } from '@nestjs/platform-express';
import cookieParser from 'cookie-parser';
Expand All @@ -9,7 +8,7 @@ import {
CloudThrottlerGuard,
GlobalExceptionFilter,
} from './base';
import { SocketIoAdapter, SocketIoAdapterImpl } from './base/websocket';
import { SocketIoAdapter } from './base/websocket';
import { AuthGuard } from './core/auth';
import { ENABLED_FEATURES } from './core/config/server-feature';
import { serverTimingAndCache } from './middleware/timing';
Expand Down Expand Up @@ -44,13 +43,6 @@ export async function createApp() {
app.use(cookieParser());

if (AFFiNE.flavor.sync) {
const SocketIoAdapter = app.get<Type<SocketIoAdapter>>(
SocketIoAdapterImpl,
{
strict: false,
}
);

const adapter = new SocketIoAdapter(app);
app.useWebSocketAdapter(adapter);
}
Expand Down
Loading

0 comments on commit 0e73737

Please sign in to comment.