A SOLID hackernews-inspired forum site built with TypeScript using the clean architecture and DDD best practices.
DDDForum.com is the application that we build in solidbook.io - The Software Design and Architecture Handbook.
You may find a template for the project technical documentation in docs/readme.md You should follow this examples as a template for your own work.
- Install and start Docker if you haven't already.
- Copy the
.env
template file. Feel free to change passwords and app secrets.
cp .env.template .env
- Build and run the image to run the backend services.
docker-compose up
- Open up an additional console and then run:
npm run setup:dev
npm run start:both
You can visit the app by going to http://localhost:3000
.
Note
: It's currently deployed on free tier Heroku, which has some undesirable side-effects like shutting off the server during periods of inactivity. So if it's down for you, refresh a couple of times. Thinking about migrating this to a serverless architecture later on.
This project requires the following tools to be present in your system.
Use node version 12.22.12
The use of this node version is mandatory You an use nvm to manage several node version in your system (see https://github.com/nvm-sh/nvm)
Python You may have to install python v2.7 in your system (see https://www.python.org/)
Examples of using the API (without authentication, using postman)
-
Get popular posts: GET http://localhost:5001/api/v1/posts/popular
-
Get new posts: GET http://localhost:5001/api/v1/posts/recent
-
Get post by slug: GET http://localhost:5001/api/v1/posts/?slug=9526372-gosto-muito-de-dd
-
Get comments by slug: GET http://localhost:5001/api/v1/comments/?slug=9526372-gosto-muito-de-dd
-
Login POST http://localhost:5001/api/v1/users/login body: { "username": "user", "password": "password" } return: { "accessToken": "...", "refreshToken": "..." } You can then use the "accessToken" for actions that require authentication, by passing the value of the accessToken in a header named "Authentication". For example:
-
Get Me GET http://localhost:5001/api/v1/users/me Headers[ ... Authorization: accessToken ] Response: { "user": { "username": "atb", "isEmailVerified": false, "isAdminUser": false, "isDeleted": false } }
- open browser use localhost:8080
- use:
- system: MySQL
- server: mysql
- username: tonysoprano
- pass: 12345678
- db name: data_dev
To run tests type: npm run test
Existing unit tests:
- src/shared/core/Guard.spec.ts
- src/modules/forum/domain/postSlug.spec.ts
- src/modules/forum/domain/services/postService.spec.ts
The existing tests verify some services in the domain layer. The domain layer has no dependency on the other layers, so there is no need for mocking. The tests can be executed without any dependencies (e.g., database, etc.)
- start the services with docker-compose up
- To debug the backend in VSCode go to the tab "Run and Debug", select "Debug TypeScript in Node.js" from the dropdown list and click the play button (this will execute the configuration with the same nsame that is located in the launch.json file (.vscode/launch.json))
- start the front-end with npm run start:public
- open in browser: localhost:3000
These tests are not part of the base project. They were added by the team to test the API. The tests are in the folder src/api_test.
The tests are based on the project https://github.com/jmfiola/jest-api-test-typescript-example. The tests are executed against the running docker containers as well as the backend. These must be running. It is not necessary to execute the frontend to run these tests. The tests are executed in sequence, so they are not independent. The tests are executed in the order they are defined in the file (this is why we need to use the --runInBand jest option).
Before running API tests, you must "clear" all the existing data and run the containers and the backend. This will ensure that the system is always at the same state when the tests area executed (i.e., an empty database). The command that runs the API tests is npm run test:api and it already does all the necessary steps (see the command in package.json).
0. Add Test Report Support
npm install [email protected] --save-dev
1. Before Running Tests
Start the containers:
docker-compose up
Create the database:
npm run db:create:dev
npm run migrate:dev
And also run the backend:
npm run start:dev
2. Running Tests
a) To run only API tests (this will delete the database and recreate it, before running the tests; no need for step 3):
npm run test:api
A report will be generated in report.html. Coverage report will be generated in coverage/index.html. Note that coverage report only covers code of the tests, it does not cover code of teh backend executed because we called the API.
b) To run only unit tests:
npm run test -- --testPathIgnorePatterns=api
3. After Running Tests
Stop the backend by hitting Ctrl+C in the terminal where the backend is running.