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

feat: integrate web into server docker image #30

Merged
merged 5 commits into from
Sep 27, 2023
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
1 change: 1 addition & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ jobs:
VITE_MOCK_WALLET_PRIVATE_KEY: '0x8b806ec6cc2cbc6f75e1e0a0b020dba4d02f1a8578b9a8e2fb1f53f5a1c83e99'
VITE_INFURA_PROJECT_ID: '25e33bb59b0b4803813c8f238cceb14e'
- uses: actions/upload-artifact@v3
if: always()
with:
name: screen-records
path: /home/runner/work/tw-did/tw-did/dist/cypress/apps/*/videos/*.mp4
Expand Down
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,26 @@ Once the servers are running, navigate to `http://localhost:4300/` in your brows

We use the `preview` subcommand for the `sample-verifier` server due to an issue with the `ethr-did-resolver` package, which causes errors on the Vite development server. An issue has already been filed to address this. Note that the service may operate more slowly than usual because the `preview` subcommand triggers a rebuild of `sample-verifier` whenever you save changes. This is a temporary workaround.

## Docker Commands

We have a few Docker-related commands that enable the system to run within a Docker environment. If you want to run the environment using `docker-compose`, you can use the following command:

```bash
nx up docker
```

This command leverages Nx's dependency management to build the necessary projects and then initiates `docker-compose up` to start MongoDB, the server, and the front-end website together. When you are done and wish to shut down the services, you can use the following command:

```bash
nx down docker
```

If you simply want to build a Docker image, you can use this command:

```bash
nx build docker
```

## E2E Tests Locally

If you are planning to run e2e tests locally, you'll also need to set a `VITE_MOCK_WALLET_PRIVATE_KEY` in your `.env.local` file. Due to security reasons, please reach out to @yurenju to obtain this key.
Expand Down
4 changes: 2 additions & 2 deletions docker-compose.yml → apps/docker/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ version: '3'
services:
server:
build:
context: .
context: ../..
dockerfile: apps/server/Dockerfile
ports:
- '3000:3000'
Expand All @@ -19,4 +19,4 @@ services:
ports:
- '27017:27017'
volumes:
- './apps/server/scripts/init-mongo.js:/docker-entrypoint-initdb.d/init-mongo.js:ro'
- '../server/scripts/init-mongo.js:/docker-entrypoint-initdb.d/init-mongo.js:ro'
29 changes: 29 additions & 0 deletions apps/docker/project.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "docker",
"$schema": "../../node_modules/nx/schemas/project-schema.json",
"sourceRoot": "apps/docker",
"projectType": "application",
"targets": {
"build": {
"dependsOn": [
{
"target": "copy-env-file",
"projects": ["server"]
},
{
"target": "build",
"projects": ["web", "server"]
}
],
"command": "docker compose -f apps/docker/docker-compose.yml --env-file=apps/server/.env.local build"
},
"up": {
"dependsOn": ["build"],
"command": "docker-compose -f apps/docker/docker-compose.yml --env-file=./apps/server/.env.local up --detach"
},
"down": {
"command": "docker-compose -f apps/docker/docker-compose.yml down"
}
},
"tags": []
}
7 changes: 6 additions & 1 deletion apps/sample-verifier-e2e/src/e2e/verifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@ Given('I am on the sample-verifier website', () => {
});

Given('I am logged into sample-verifier website', () => {
cy.get('[data-testid=connect-button]').click();
const dataAttr = '[data-testid=disconnect-button]';
cy.get(dataAttr).then((res) => {
if (res.length === 0) {
cy.get('[data-testid=connect-button]').click();
}
});
});

Given(
Expand Down
7 changes: 7 additions & 0 deletions apps/server/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
MONGO_INITDB_ROOT_USERNAME=initdbrootusername
MONGO_INITDB_ROOT_PASSWORD=initdbrootpassword
MONGO_DATABASE=dpassport
MONGO_DATABASE_USERNAME=dpassportusername
MONGO_DATABASE_PASSWORD=dpassportpassword
MONGO_HOST=localhost
MONGO_CONTAINER_NAME=dpassport-mongo
1 change: 1 addition & 0 deletions apps/server/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ COPY package.json package-lock.json .
RUN npm --omit=dev -f install

COPY dist/apps/server server
COPY dist/apps/web web
# TODO: this is only for developement, we should remove it for production
COPY apps/server/.env.local .

Expand Down
12 changes: 9 additions & 3 deletions apps/server/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,15 @@
}
}
},
"docker-build": {
"dependsOn": ["build"],
"command": "docker build -f apps/server/Dockerfile . -t server"
"setup-db": {
"command": "node apps/server/scripts/setup-db.js"
},
"start-db": {
"command": "node apps/server/scripts/start-db.js"
},
"copy-env-file": {
"dependsOn": ["setup-db"],
"command": "node apps/server/scripts/copy-env-file.js"
}
},
"tags": []
Expand Down
19 changes: 19 additions & 0 deletions apps/server/scripts/copy-env-file.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const { existsSync, copyFileSync } = require('fs');
const { join } = require('path');

function copyEnvFile() {
const serverPath = join(__dirname, '..');
const targetPath = join(serverPath, '.env.local');
const examplePath = join(serverPath, '.env.example');

if (!existsSync(targetPath)) {
console.log('.env.local not exists, copy from .env.example');
copyFileSync(examplePath, targetPath);
} else {
console.log('.env.local does exist, do nothing.');
}
}

if (require.main === module) {
copyEnvFile();
}
18 changes: 18 additions & 0 deletions apps/server/src/app/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import { Module } from '@nestjs/common';
import { MongooseModule } from '@nestjs/mongoose';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { ServeStaticModule } from '@nestjs/serve-static';

import { AppController } from './app.controller';
import { AppService } from './app.service';
import { UsersModule } from '../user/user.module';
import { Config, getConfig } from '../config/configuration';
import { AuthModule } from '../auth/auth.module';
import { join } from 'path';
import { existsSync } from 'fs';

@Module({
imports: [
Expand All @@ -27,6 +30,21 @@ import { AuthModule } from '../auth/auth.module';
};
},
}),
ServeStaticModule.forRootAsync({
useFactory: () => {
const clientPath = join(__dirname, '..', 'web');

if (existsSync(clientPath)) {
return [
{
rootPath: clientPath,
},
];
} else {
return [];
}
},
}),
UsersModule,
AuthModule,
],
Expand Down
32 changes: 32 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 2 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,7 @@
"license": "MIT",
"scripts": {
"start": "nx serve web",
"acceptance": "nx acceptance acceptance",
"setup:db": "node apps/server/scripts/setup-db.js",
"start:db": "node apps/server/scripts/start-db.js",
"predocker-compose:up": "nx build server && npm run setup:db",
"docker-compose:up": "docker-compose --env-file=./apps/server/.env.local up --build --detach",
"docker-compose:down": "docker-compose down"
"acceptance": "nx acceptance acceptance"
},
"private": true,
"devDependencies": {
Expand Down Expand Up @@ -76,6 +71,7 @@
"@nestjs/mongoose": "^10.0.1",
"@nestjs/passport": "^10.0.2",
"@nestjs/platform-express": "^10.0.2",
"@nestjs/serve-static": "^4.0.0",
"@semaphore-protocol/group": "^3.11.0",
"@semaphore-protocol/identity": "^3.11.0",
"@semaphore-protocol/proof": "^3.11.0",
Expand Down