This is an experimental blogging API developed with Docker, Deno, Oak and MySQL.
The code from this API should not be deemed as ideal as all the functionalities are implemented in a very naive way. The goal was to test things out with this new runtime, not to build a production quality API. |
- Dockerization ✔️
- Blogs ✔️
- Authentication ✔️
- Documentation ✔️
- Development Tutorial ✔️
.
├── Dockerfile
├── app.ts
├── controllers
│ ├── auth.ts
│ └── blogs.ts
├── db
│ └── mysql.ts
├── deps.ts
├── docker-compose.yml
├── docker-entrypoint-initdb.d
│ ├── blogs.sql
│ └── users.sql
├── helpers
│ └── between.ts
├── middleware
│ ├── authorize.ts
│ ├── error.ts
│ ├── logger.ts
│ └── timer.ts
├── models
│ ├── Blog.ts
│ └── User.ts
└── routes
├── auth.ts
├── blogs.ts
└── home.ts
There are eight directories in the project -
api
containsserver.ts
, responsible for initiating the application instance. It also registers four universal middleware.controllers
directory contains logic for all the api endpoints. Logic for a certain endpoint is encapsulated inside relevantly named files.auth.ts
contains logic regarding registration of users and generation of JWT tokens.blogs.ts
contains logic regarding CRUD operations of blog posts.
db
directory contains necessary code for connecting to the database.docker-entrypoint-initdb.d
contains sql files for initializing the database.helpers
contains small helper functions for reusability.middleware
directory contains middleware functions for reusability.authorize.ts
handles validation of JWT tokens.error.ts
handles all errors centrally.logger.ts
logs all requests to the console.timer.ts
logs request times to the console.
models
contains classes containing functions for querying the database.routes
contains necessary code for registering the controller functions as middleware route endpoints.
There are four orphan files in the project root:
Dockerfile
for building the api container.app.ts
is responsible for registering all endpoints to the main app instance and firing up the server.deps.ts
imports and exports all the necessary dependencies.docker-compose.yml
file for building and running multi-container application.
Clone this repository anywhere you want. Open up terminal and use following command to build and run the application -
docker-compose up --build
There will be a wall of text but look for something like following -
db_1 | 2020-06-08T17:27:05.115386Z 0 [Note] Event Scheduler: Loaded 0 events
db_1 | 2020-06-08T17:27:05.116258Z 0 [Note] mysqld: ready for connections.
db_1 | Version: '5.7.30' socket: '/var/run/mysqld/mysqld.sock' port: 3306 MySQL Community Server (GPL)
The application should be running on http://127.0.0.1:3000 address. A postman collection containing all the routes can be found inside postman-collection
directory.
You can stop the application by pressing control + c
combination. If you want to stop and delete all built images issue following command -
docker-compose down
Use following command if you don't want the containers and images to be deleted -
docker-compose stop
You can restart the application with following command -
docker-compose up
You can learn more about docker-compose command line interface from Compose command-line reference
The postman-collection/deno-blog.postman_collection.json
file can be imported inside Postman for testing out the endpoints.