To manage comments on posts of a blog or wiki page.
This is a Typescript version of the project DevMastery Comments Microservice API written in JavaScript.
The goal of this project is to be a starting point to show what Clean Architecture would look like in a real world application. This project tries to follow the rules of Uncle Bob's Clean Architecture. You will also find it named hexagonal, ports-and-adapters, or onion architecture.
The center of your application is not the database. Nor is it one or more of the frameworks you may be using. The center of your application is the use cases of your application - Unclebob (source)
- XSS Protection (via sanitize-html)
- Flags Spam (via Akismet)
- Flags rude or inappropriate language (English only via Content Moderator)
- Flags personally identifiable information (English only via Content Moderator)
- Dependency Injection done with the nice framework from Awilix.
- OpenAPI documentation done by Swagger UI Express.
- Simplify npm scripting using nps.
To build and run this service locally you have two options:
- Docker (Recommended) - Develop as close to production as you can
- Install Docker for Linux, Docker for Mac
or
Docker for Windows. For Windows, we recommend use WSL2 - Install docker-compose
- Install Docker for Linux, Docker for Mac
or
- Install the tools directly on your machine (traditional).
- Install Node.js - Required to set up the Server (Version: >=10 <=14).
- Install MongoDB Community Server - Required to set up the database (Version: >=4)
Additional
- Azure Content Moderator account (free)
- Akismet Developer account (free)
- IDE or Text Editor for example VS Code
- Tool for request API's for example Postman
- If you use Windows SO, use Windows Terminal or some bash emulation like Git BASH, AVOID use default cmd
git clone https://github.com/sagiomc/ts-comments-api <project_name>
Copy the content of sampledotenv
and fill the appropriate values.
-
Start up server and database. On the root app directory run the following command:
docker-compose up
This starts a local server using
nodemon
, which will watch for any file changes and will restart the server according to these changes. The command above will "freeze" your terminal window/tab. If you want to run the api in background you have to rundocker-compose up -d
.With default values, the server will be displayed to you as
http://localhost:3000
. The api use cases will be available inhttp://localhost:3000/api/comment
.NOTE: To change the default values running with Docker, you have to add the following environment variables to
.env
file:HOST_PORT=<PORT> # Port where you'll consume the api from your host machine Ej. HOST_PORT=8080 --> localhost:8080 HOST_INSPECT_PORT=<PORT> # Port where you'll inspect the api for debug purposes Ej. HOST_INSPECT_PORT=9229 --> Open socket in localhost:9229 HOST_DB_PORT=<PORT> # Port where you'll consume the database server from your host machine Ej HOST_DB_PORT=27029 --> mongodb://localhost:27029
-
Running commands inside docker containers like install/uninstall npm dependencies. To run commands inside containers you have following the next structure on the root api directory:
docker-compose exec app <my-command>
For example:
docker-compose exec app npm install <dependency> # Installing dependencies docker-compose exec app npm run test:watch # Run jest in watch mode
-
Start up the mongodb server
Usually this is just:
mongod
on the command line. -
On the root app directory, install the dependencies and set up the database, running:
npm start setup
-
To run in development mode where code is run by
nodemon
and re-transpiled any time:npm start serve
.
├── src
| ├── domain - Enterprise business rules
| | ├── entities - Core business rules
| | └── {feat-name} - Business features (add-comment, delete-comment, etc)
| ├── dataproviders - Interfaces data adapters implementations
| └── frameworks - Frameworks and drivers that exposes the app
├── test - Tests directory
| ├── fixtures - Accesories for tests
| └── unit - Unit tests, this directory must follow the same structure as "src" directory
├── commands - Development utilities like banner and set up the database
└── dist - Compiled javascript files
- dev-mastery/comments-api - To manage comments on various Dev Mastery properties.
- mattia-battiston/clean-architecture-example - Clean Architecture Example in Java
- ardalis/CleanArchitecture - A starting point for Clean Architecture with ASP.NET Core
- stemmlerjs/ddd-forum - Hacker news-inspired forum app built with TypeScript & DDD from solidbook.io