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

[recnet-api] Create README.md #165

Merged
merged 3 commits into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions apps/recnet-api/.env.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Usage: cp .env.sample .env

# Database
export RDS_HOSTNAME=localhost
export RDS_PORT=5432
export RDS_DB_NAME=postgres
export RDS_USERNAME=postgres
export RDS_PASSWORD=admin

# Prisma
export DB_MIGRATE=true
export PRISMA_SCHEMA=./apps/recnet-api/prisma/schema.prisma
export PRISMA_DATABASE_URL="postgresql://postgres:admin@localhost:5432/postgres"
84 changes: 84 additions & 0 deletions apps/recnet-api/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
# Recnet-api

Recnet-api is a backend API service for [Recnet](https://www.recnet.io/). Powered by Node.js and [NestJS](https://nestjs.com/), it offers scalable solutions for paper recommendation, news feed system and academic social media network.

## Requirements

- node >= 18
- PostgreSQL 16

## Documentation
- [Notion](https://www.notion.so/RecNet-f8440e23b4e54af4a9636e84ed101815)
- Swagger API Doc
- [dev](https://dev-api.recnet.io/api)
- prod

## Local Development

### Installation
```bash
pnpm install
```

### Set up a local DB
1. Use docker to create a PostgreSQL container.
```bash
# create and start
docker create --name recnet-postgres -p 5432:5432 -e POSTGRES_PASSWORD=admin postgres:16.2
docker start recnet-postgres

# stop and remove
docker stop recnet-postgres
docker rm recnet-postgres
```

2. Run migration
```bash
nx prisma deploy recnet-api
```

### Prepare environment variable
1. Copy from sample file
```bash
cp apps/recnet-api/.env.sample apps/recnet-api/.env
```
2. Modify environment variable
3. Source the .env file
```bash
source apps/recnet-api/.env
```

### Generate prisma client
```bash
nx prisma:generate recnet-api
```

### Run the server
```bash
nx serve recnet-api
```

## DB Migration
### How to generate migration files?
1. Prepare a local database. Same as [here](#set-up-a-local-db).
2. Modify `apps/recnet-api/prisma/prisma.schema`
3. Run migration script. The migration script will create a directory under `apps/recnet-api/prisma/migrations` with `migration.sql` and `down.sql`.
```bash
nx prisma:migrate recnet-api {migration_name}
```

### How does the migration script been deployed to dev or production environment?
If the environment variable `DB_MIGRATION=true`, the migration will be deployed when the server is up. It is executed by `prisma.connection.provider.ts`.

## Environment and Deployment
We currently maintain two environment - dev and prod. All of the services are deployed to AWS Elastic Beanstalk.

### API host
- dev: https://dev-api.recnet.io/
- prod: https://api.recnet.io/

### Deployment
We utilize AWS CodePipeline to build up the CI/CD for the services. Whenever the designated branch updates, the pipeline will deploy the code on that branch to the corresponding environment.

- dev branch: recnet-api-dev
- prod branch: master
6 changes: 5 additions & 1 deletion apps/recnet-api/scripts/dbMigrate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ perform_migration_down() {

move_down_sql_file() {
DIRECTORY=$(find $PRISMA_MIGRATIONS_DIR -type d -name "*$MIGRATION_NAME*" | tail -n 1)
mv down.sql $DIRECTORY/down.sql
if [ -s "down.sql" ]; then
mv down.sql $DIRECTORY/down.sql
else
rm down.sql
fi
}

echo "Migrating database to $MIGRATION_NAME"
Expand Down
Loading