Skip to content

Commit

Permalink
Merge pull request #70 from bennetrr/feat/rebrickable-optimizations
Browse files Browse the repository at this point in the history
Rebrickable API optimizations
  • Loading branch information
bennetrr authored Dec 22, 2024
2 parents 4bbfee1 + b180ef3 commit 1ff81f0
Show file tree
Hide file tree
Showing 75 changed files with 814 additions and 398 deletions.
35 changes: 0 additions & 35 deletions .editorconfig

This file was deleted.

2 changes: 1 addition & 1 deletion .github/workflows/build-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ jobs:
uses: ./.github/actions/containers/build
with:
push: 'false'
context: src/backend/Bennetr.BrickInv.Api/
context: src/backend/
docker-platform: ${{ matrix.platform.docker }}
dotnet-platform: ${{ matrix.platform.dotnet }}
dotnet-ef-platform: ${{ matrix.platform.dotnet-ef }}
10 changes: 0 additions & 10 deletions .github/workflows/lint.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,6 @@ on:
workflow_dispatch:

jobs:
editorconfig:
name: Lint with editorconfig
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Run eclint
run: |
npx eclint check .
prettier:
name: Lint frontend with prettier
runs-on: ubuntu-latest
Expand Down
93 changes: 93 additions & 0 deletions .github/workflows/prerelease.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
name: Pre-release

on:
push:
branches:
- main

jobs:
setup:
name: Setup
runs-on: ubuntu-latest
outputs:
next-version: ${{ steps.get-release-version.outputs.version }}
next-version-name: ${{ steps.get-release-version.outputs.version-name }}
steps:
- id: get-release-version
run: |
echo "version=${{ github.sha }}" >> $GITHUB_ENV
echo "version-name=pre-${{ github.sha }}" >> $GITHUB_ENV
build-backend:
name: Build backend containers
runs-on: ubuntu-latest
needs:
- setup
permissions:
packages: write
contents: read
strategy:
matrix:
platform:
- { docker: linux/amd64, dotnet: amd64, dotnet-ef: linux-x64 }
- { docker: linux/arm64, dotnet: arm64, dotnet-ef: linux-arm64 }
- { docker: linux/arm64/v8, dotnet: arm64, dotnet-ef: linux-arm64 }
steps:
- uses: actions/checkout@v4

- name: Build container
uses: ./.github/actions/containers/build
with:
registry-server: ghcr.io
registry-user: ${{ github.actor }}
registry-password: ${{ secrets.GITHUB_TOKEN }}
name: bennetrr/brickinv-backend
version: ${{ needs.setup.outputs.next-version-name }}
context: src/backend/
push: 'true'
docker-platform: ${{ matrix.platform.docker }}
dotnet-platform: ${{ matrix.platform.dotnet }}
dotnet-ef-platform: ${{ matrix.platform.dotnet-ef }}

merge-backend:
name: Merge backend containers
needs:
- setup
- build-backend
runs-on: ubuntu-latest
permissions:
packages: write
contents: read
steps:
- uses: actions/checkout@v4

- name: Merge and push container
uses: ./.github/actions/containers/merge
with:
registry-server: ghcr.io
registry-user: ${{ github.actor }}
registry-password: ${{ secrets.GITHUB_TOKEN }}
name: bennetrr/brickinv-backend
version: ${{ needs.setup.outputs.next-version-name }}

deploy:
# Thanks to https://www.programonaut.com/how-to-deploy-a-git-repository-to-a-server-using-github-actions/
name: Deploy to development
needs:
- setup
- merge-backend
runs-on: ubuntu-latest
steps:
- name: Install ssh keys
run: |
install -m 600 -D /dev/null ~/.ssh/id_rsa
echo "${{ secrets.DEPLOY_SSH_KEY }}" > ~/.ssh/id_rsa
ssh-keyscan -H ${{ secrets.DEPLOY_SSH_HOST }} > ~/.ssh/known_hosts
- name: Pull
run: |
ssh ${{ secrets.DEPLOY_SSH_USER }}@${{ secrets.DEPLOY_SSH_HOST }} "
cd ${{ secrets.DEPLOY_WORKDIR }}
sed -i -E 's/ghcr\.io\/bennetrr\/brickinv-(frontend|backend):v[0-9]+\.[0-9]+\.[0-9]+/ghcr.io\/bennetrr\/brickinv-\1:${{ needs.setup.outputs.next-version-name }}/g' docker-compose.yml
docker compose up -d
exit
"
16 changes: 11 additions & 5 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ jobs:

build-frontend:
name: Build frontend containers
needs: [ setup ]
needs:
- setup
runs-on: ubuntu-latest
permissions:
packages: write
Expand Down Expand Up @@ -58,7 +59,8 @@ jobs:

build-backend:
name: Build backend containers
needs: [ setup ]
needs:
- setup
runs-on: ubuntu-latest
permissions:
packages: write
Expand All @@ -80,15 +82,17 @@ jobs:
registry-password: ${{ secrets.GITHUB_TOKEN }}
name: bennetrr/brickinv-backend
version: ${{ needs.setup.outputs.next-version-name }}
context: src/backend/Bennetr.BrickInv.Api/
context: src/backend/
push: 'true'
docker-platform: ${{ matrix.platform.docker }}
dotnet-platform: ${{ matrix.platform.dotnet }}
dotnet-ef-platform: ${{ matrix.platform.dotnet-ef }}

merge-frontend:
name: Merge frontend containers
needs: [ setup, build-frontend ]
needs:
- setup
- build-frontend
runs-on: ubuntu-latest
permissions:
packages: write
Expand All @@ -107,7 +111,9 @@ jobs:

merge-backend:
name: Merge backend containers
needs: [ setup, build-backend ]
needs:
- setup
- build-backend
runs-on: ubuntu-latest
permissions:
packages: write
Expand Down
62 changes: 32 additions & 30 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,22 @@ The frontend is configurable with the `env.js` file.
| Name | Type | Description |
|-----------------------|----------|------------------------------------------------------------------------------------------|
| `apiBaseUrl` | `string` | Base URL of the BrickInv API, e.g. `https://api.brickinv.com` or `http://localhost:5105` |
| `apiBaseUrl` | `string` | Base URL of the BrickInv API, e.g. `https://api.brickinv.com` or `http://localhost:4003` |
| `clerkPublishableKey` | `string` | Publishable key of the Clerk application |

### Backend

The backend is configurable with everything supported by ASP.NET.
For development, the .NET user secret manager is recommended, for production a `.env` file.

| `.env`-Name | `.json`-Name | Type | Description |
|--------------------------------|-------------------------------|----------|--------------------------------------------------------------------------------------|
| `AppConfig__RebrickableApiKey` | `AppConfig.RebrickableApiKey` | `string` | API key for Rebrickable, used for retrieving information about Lego sets |
| `AppConfig__AppBaseUrl` | `AppConfig.AppBaseUrl` | `string` | Base URL of the BrickInv App, e.g. `https://brickinv.com` or `http://localhost:5137` |
| `Clerk__SecretKey` | `Clerk.SecretKey` | `string` | Secret key of the Clerk application |
| `Clerk__Authority` | `Clerk.Authority` | `string` | Instance URL of the Clerk application |
| Name | Type | Description |
|---------------------------------|----------|----------------------------------------------------------------------------------------------------------------------|
| `Authentication:ClerkSecretKey` | `string` | Secret key of the Clerk application |
| `Authentication:Authority` | `string` | Instance URL of the Clerk application |
| `Authentication:AppBaseUrl` | `string` | Base URL of the BrickInv App, e.g. `https://brickinv.com` or `http://localhost:4004` |
| `ConnectionStrings:Db` | `string` | Connection string for main database, e.g. `Server=localhost;Port=4001;User=root;Password=brickinv;Database=brickinv` |
| `ConnectionStrings:Redis` | `string` | Connection string for Redis cache, e.g. `localhost:4002` |
| `Rebrickable:ApiKey` | `string` | API key for Rebrickable, used for retrieving information about Lego sets |

## Development

Expand All @@ -50,7 +52,7 @@ pnpm install
Install backend dependencies:

```bash
# working directory: src/backend/Bennetr.BrickInv.Api/Bennetr.BrickInv.Api
# working directory: src/backend/Bennetr.BrickInv.Api
dotnet restore
```

Expand All @@ -60,28 +62,29 @@ If any changes where made to the database models, a migration script needs to be
The migration only needs to be created for the context that holds the changed models.

```bash
# working directory: src/backend/Bennetr.BrickInv.Api/Bennetr.BrickInv.Api
dotnet ef migrations add ${NAME} -c BrickInvContext -o ./Migrations/BrickInv
# working directory: src/backend/Bennetr.BrickInv.Api
dotnet ef migrations add {{NAME}} -c BrickInvContext -o ./Migrations/BrickInv
```

### Run development server against local API

Start database:
Start database and cache:

```bash
# working directory: repository root
docker run -d \
--name brickinv-mariadb-dev \
--publish 3306:3306 \
--env 'MARIADB_ROOT_PASSWORD=3gEju5UGRPbSbJ$r#wvYDn$g%6ryH5' \
--name brickinv-dev-mariadb \
--publish 4001:3306 \
--env 'MARIADB_ROOT_PASSWORD=brickinv' \
--volume brickinv-mariadb-dev:/var/lib/mysql \
--volume ./setup.sql:/docker-entrypoint-initdb.d/setup.sql \
mariadb:11.3.2-jammy
```
mariadb:11.6.2

> [!NOTE]
> If you change the password or the port in the command above,
> you need to update the `appsettings.Development.json` file!
docker run -d \
--name brickinv-dev-redis \
--publish 4002:6379 \
redis:7.4.1-alpine
```

To configure the backend, use
the [.NET User Secret Manager](https://learn.microsoft.com/en-us/aspnet/core/security/app-secrets?view=aspnetcore-8.0&tabs=windows#secret-manager)
Expand All @@ -90,18 +93,18 @@ with the options from the [Configuration](#backend) section.
Before starting the backend, you need to run the database migration scripts:

```bash
# working directory: src/backend/Bennetr.BrickInv.Api/Bennetr.BrickInv.Api
# working directory: src/backend/Bennetr.BrickInv.Api
dotnet ef database update --context BrickInvContext
```

Then, start the backend:

```bash
# working directory: src/backend/Bennetr.BrickInv.Api/Bennetr.BrickInv.Api
# working directory: src/backend/Bennetr.BrickInv.Api
dotnet run --launch-profile http
```

The backend is exposed at `http://localhost:5105`
The backend is exposed at `http://localhost:4003`

To configure the frontend, copy `src/frontend/public/env/env.template.js` to `src/frontend/public/env/env.local.js` and
replace the empty strings with your own values.
Expand All @@ -114,25 +117,24 @@ To start the frontend, run:
pnpm dev:local
```

To stop the database container and delete its data, run:
To stop the database and cache containers and delete its data, run:

```bash
docker stop brickinv-mariadb-dev
docker rm brickinv-mariadb-dev
docker volume rm brickinv-mariadb-dev
docker stop brickinv-dev-mariadb brickinv-dev-redis
docker rm -v brickinv-mariadb-dev brickinv-dev-redis
```

### Run development server with production API
### Run development server against development API

To configure the frontend, copy `src/frontend/public/env/env.template.js` to `src/frontend/public/env/env.prod.js` and
To configure the frontend, copy `src/frontend/public/env/env.template.js` to `src/frontend/public/env/env.dev.js` and
replace the empty strings with your own values.
The configuration fields are documented in the [Configuration](#frontend) section.

To start the frontend, run:

```bash
# working directory: src/frontend
pnpm dev:prod
pnpm dev:dev
```

## Production
Expand All @@ -159,7 +161,7 @@ docker compose up -d
### Connections

All services are exposed into the `reverse_proxy` network.
The frontend is available under `brickinv-frontend-1:80`, the backend under `brickinv-backend-1:80`.
The frontend is available under `brickinv-frontend-1:80`, the backend under `brickinv-backend-1:8080`.

The application data is saved in the named volume `brickinv_mariadb`.

Expand Down
10 changes: 6 additions & 4 deletions backend.env
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
AppConfig__RebrickableApiKey=""
AppConfig__AppBaseUrl=""
Clerk__SecretKey=""
Clerk__Authority=""
Authentication__ClerkSecretKey=""
Authentication__Authority=""
Authentication__AppBaseUrl=""
ConnectionStrings__Db=""
ConnectionStrings__Redis=""
Rebrickable__ApiKey=""
14 changes: 10 additions & 4 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,21 @@ name: brickinv

services:
mariadb:
image: mariadb:11.3.2-jammy
image: mariadb:11.6.2
restart: always
environment:
MARIADB_ROOT_PASSWORD: 'wN%F3doRGFnh25MJ*D^N*vp*oV9ybp^zAwMkZaS%B3!yhsxv!#Kn@PCpo76zLRyu6EL#2$n8BzXpM&u*@h*kiVwY3m!5nUmf2VmMUmSVYm*bv48q2&pUKsRVe*Pe*9Yp'
MARIADB_ROOT_PASSWORD: "brickinv"
volumes:
- "mariadb:/var/lib/mysql"
- "./setup.sql:/docker-entrypoint-initdb.d/setup.sql"
networks:
- internal
networks:
- internal

redis:
image: redis:7.4.1-alpine
restart: always
networks:
- internal

frontend:
image: ghcr.io/bennetrr/brickinv-frontend:0.0.0
Expand Down
Loading

0 comments on commit 1ff81f0

Please sign in to comment.