The main goal of this project is to design and develop a social network application inspired on Twitter and Reddit.
This application is written using the Loose Coupling architechture pattern (also called system design).
Each component of the application (user service, post service) will have its own models, routes and business logic related files in a single directory.
Reusable logic, such as database connection is exposed at the root level of the project module tree.
.
├── server.ts
├── database.ts
├── service-name-1\
| ├── index.ts
| ├── routes.ts
| └── models.ts
├── service-name-2\
| ├── index.ts
| ├── routes.ts
| └── models.ts
We use NodeJS for the server implementation and Docker to run the external services such the database. If you have issues installing Docker, then you can take advantage of your local PostgreSQL instance instead. Its recommended to use Docker because we could introduce more external services in the future or dockerize the NodeJS application.
First we need to clone the project using Git, to accomplish this we run
git clone <repository.git>
as follows:
git clone https://github.com/EstebanBorai/whizzes-server.git
This will create a copy of the repository in our system and we will be able to edit files, commit changes and push them when changes are ready to be published.
Then we need to install project dependencies, this project makes use of yarn
to manage the dependencies, to install it you must have NodeJS installed in your system.
Run node -v
and expect a version tag to be printed to the terminal as follows:
$ node -v
v14.10.0
If the command is not availble, you must install NodeJS before going any further.
You can install NodeJS from the official website and follow the steps from the official docs.
When NodeJS is ready, you will be able to run npm install -g yarn
, this command
will install yarn as a global dependency. When the installation completes
you will be able to issue commands using yarn.
With both NodeJS and yarn installed in your system, we are able to install
project dependencies. For this you must run yarn
from the project root directory.
$ yarn
yarn install v1
[1/4] 🔍 Resolving packages...
[2/4] 🚚 Fetching packages...
[3/4] 🔗 Linking dependencies...
[4/4] 🔨 Building fresh packages...
✨ Done in 1.35s.
At this point we are able to run the server in development mode.
You will also need a PostgreSQL database instance, on initialization
our server will attempt to open a connection to the PostgreSQL instance
available. We set connection configurations and other settings using the
.env
file.
Check on the .env.sample
file available in the root directory of the
project for a sample of the required configurations, copy the contents of
this file into a new file in the same directory with the name .env
.
Details on each setting is available in the Environment Variables section below.
If you are using Docker and docker-compose
CLI is also installed in your
system you can run the following command:
bin/docker-start
# create a volume for the database
docker volume create --name whizzes-database
# run the docker-compose.yml file
docker-compose up --build
This command will build a container using the official PostgreSQL image
and will run it given the configurations from the .env
file.
Create a .env
file on the root directory with the following
environment variables.
A .env.sample
file is also available with predefined values for simplicity.
Name | Description |
---|---|
PORT |
HTTP Port for the server |
POSTGRES_USER |
PostgreSQL connection username |
POSTGRES_PASSWORD |
PostgreSQL connection password |
POSTGRES_DB |
PostgreSQL connection database |
JWT_SECRET |
Secret to sign JWT tokens with |
In order to debug the application, you must run the debug
script as follows:
yarn debug
When the output is printed to the stdout, you must attach to the NodeJS process, either using Google Chrome DevTools, Visual Studio Code, or your favorite debugging tool.
If you are using Visual Studio Code, you can create a .vscode/launch.json
file in
the project root directory (this directory), and paste the following bytes to the
file:
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Attach to Process",
"type": "node",
"request": "attach",
"port": 9229
}
]
}
Then after running yarn debug
successfully, you must initialize the debugger,
using the built-in Visual Studio Code debugger, by running the "Attach to process"
task.
Contributions of any kind are welcome and would be awesome, ideas, bug fixes, reports, feedback and improvements are always welcome!
Licensed under the MIT License.