The purpose of creating this application is for the submission project in the "Belajar Fundamental Aplikasi Back-End" class at Dicoding and also to serve as a learning resource for developing a Restful API using the Hapi framework, utilizing the PostgreSql database management system, integrating a Message Broker with RabbitMq, and implementing server-side caching with Redis.
- Server : Nodejs with Hapi Framework, RabbitMQ, Redis/Memurai
Clone the project
git clone https://github.com/khalilannbiya/openmusic-app.git
Or Download ZIP
Go to the project directory
cd openmusic-app
Install the package using the following command:
pnpm i
Copy and paste the .env file using the command:
cp .env.example .env
Let's Configure the .env file
- For the PORT and HOST to run the Hapi server, here's an example: https://hapi.dev/tutorials/?lang=en_US#-creating-a-server
- For PGUSER to PGPORT, it's the PostgreSQL database configuration. Make sure PostgreSQL is installed on your local machine. You can follow these steps: https://www.w3schools.com/postgresql/postgresql_install.php. Here's an example:
PGUSER=developer // Make sure you have created the 'developer' user in PostgreSQL PGHOST=localhost // The host for PostgreSQL can vary depending on where the PostgreSQL server is hosted. If PostgreSQL is hosted on your local machine (localhost), you can use localhost or 127.0.0.1 as the host. However, if PostgreSQL is hosted on an external server or in the cloud, you should use the IP address or hostname of that server PGPASSWORD=veryhardpassword // Provide a strong password and make sure you remember it PGDATABASE=openmusic-app // Fill in the database you want to use and make sure the database has been created PGPORT=5432 // Also, make sure to check the port used by the PostgreSQL server (default is 5432)
- For ACCESS_TOKEN_KEY and REFRESH_TOKEN_KEY, there's something you need to know first. For the JWT token key values, never set them to easily guessable or weak security values. If someone manages to obtain these keys, they can create or manipulate valid tokens without going through the system. Since JWT stores user identities in the token, they could become anyone, even an admin. So, provide values that are not easily known and random. Also, for added security, make ACCESS_TOKEN_KEY and REFRESH_TOKEN_KEY different. For security, let's generate a random string using the following command in the node REPL:
require('crypto').randomBytes(64).toString('hex')
- For ACCESS_TOKEN_AGE, it represents the validity period of the access token. The faster it expires, the better. In my case, it is set to 30 minutes in seconds
- For RABBITMQ_SERVER, it is for configuring the Message Broker. In this case, Rabbit MQ is used. Make sure your machine already has Rabbit MQ installed. You can follow the installation guide here: link
- For REDIS_SERVER, it is for configuring Redis. Make sure Redis is installed. Note that Redis officially stopped supporting Windows. However, for Windows users, there is an alternative engine that can be used as a caching memory, namely Memurai. Even though we are not using Redis directly, Memurai uses the Redis source code that has been ported to support running on Windows. So, there is almost no difference in terms of using Memurai compared to Redis, both in terms of using the command-line and in application development using the SDK. We can even refer to the documentation provided by Redis even if we use Memurai. To download and install Memurai on Windows, please visit the Memurai download page: link and install it using the following steps: link. Alternatively, you can install Redis by enabling WSL with the following steps: link
Run the migration table with the command:
pnpm run migrate up
Since we are using a Message Broker, and this project is a producer, don't forget to clone the project https://github.com/khalilannbiya/openmusic-app-queue-consumer.git as a consumer, then follow the installation steps in the project's repository. And, remember to run them simultaneously after setting up the projects to avoid any errors.
Make sure that the PostgreSQL server, RabbitMQ, and Redis/Memurai servers are already running.
Run the server:
pnpm run start-dev
If you have any feedback, please reach out to us at [email protected]