This is a RESTful API that manages notes. It can add, edit, delete, and show notes. It is based on notes-app-back-end.
The current configuration allows CORS for all origins, which can be insecure in production environments. To mitigate this, consider implementing authentication or configuring CORS with more restrictive settings.
Data example:
{
"id": "V1StGXR8_Z5jdHi6B",
"title": "Sejarah JavaScript",
"createdAt": "2020-12-23T23:00:09.686Z",
"updatedAt": "2020-12-23T23:00:09.686Z",
"tags": ["NodeJS", "JavaScript"],
"body": "JavaScript pertama kali dikembangkan oleh Brendan Eich dari Netscape di bawah nama Mocha, yang nantinya namanya diganti menjadi LiveScript, dan akhirnya menjadi JavaScript. Navigator sebelumnya telah mendukung Java untuk lebih bisa dimanfaatkan para pemrogram yang non-Java.",
}
The id
, createdAt
, and updatedAt
properties are managed by the server.
The other properties are input by the client.
-
PHP with a minimum version 8.1.2. You can install it using this command on the terminal:
sudo apt install php-common php-cli libapache2-mod-php php-mysql
Installing libapache2-mod-php also installs the
Apache2 HTTP Server
. -
Apache2 HTTP Server
If you have installed
PHP
using the command in step 1 above, Apache2 HTTP Server should already be installed. Otherwise, you can install it or check whether it is installed with this command:sudo apt install apache2
-
MariaDB Server with a minimum version 10.6
This is the database server where the persistent data are stored. You can install it or check whether it is installed with this command:
sudo apt install mariadb-server
After that, run this security script to restrict access to the server:
sudo mysql_secure_installation
-
Apache2 HTTP Server modules
mod_headers
andmod_rewrite
. You can activate these modules using the following commands:sudo a2enmod headers sudo a2enmod rewrite
-
Git (for cloning this github repository). You can skip this if you would like to download the repository manually.
Before installing, you have to clone this repository using one of the following commands in the terminal:
git clone https://github.com/gabrield-droid/notes-app-api.git
git clone [email protected]:gabrield-droid/notes-app-api.git
gh repo clone gabrield-droid/notes-app-api
Alternatively, you can download the ZIP file of the repository and extract it manually.
Place the repository folder into this directory /var/www/
.
- MySQL/MariaDB configuration
- Apache2 VirtualHost configuration
This guided setup does not configure a local domain (e. g. notes-app-api.local
) or HTTPS.
It's recommended to run this guided setup first. You may later customise configurations manually as needed.
- You are using a Debian-based distro (e.g., Debian, Ubuntu, Linux Mint)
- The above requirements are already installed.
- Your MySQL/MariaDB server is running in the
localhost
- Open your terminal and navigate to the project directory.
- Execute the installation script as the root user:
or
sudo ./INSTALL_Debian.sh
sudo bash ./INSTALL_Debian.sh
- Follow the on-screen instruction.
-
Configure the MySQL/MariaDB database and user credentials
Open MariaDB/MySQL by running this command on the terminal:
sudo mysql
Inside the MySQL/MariaDB run these command:
-- Substitute database_name, database_user, and database_password with the values you want. -- Create the database CREATE DATABASE IF NOT EXISTS `database_name`; -- Select the database USE `database_name`; -- Create the notes table CREATE TABLE IF NOT EXISTS `notes` ( `id` CHAR(16) PRIMARY KEY, `title` VARCHAR(255) DEFAULT NULL, `body` TEXT DEFAULT NULL, `tags` VARCHAR(255) DEFAULT NULL, `createdAt` CHAR(24) NOT NULL, `updatedAt` CHAR(24) DEFAULT NULL ); -- Create the MySQL/MariaDB user credentials CREATE USER IF NOT EXISTS 'database_user'@'localhost' IDENTIFIED BY 'database_password'; -- Grant specific privileges to the user GRANT SELECT, INSERT, UPDATE, DELETE ON `database_name`.`notes` TO 'database_user'@'localhost';
-
Create
db_config.php
fileInside the project directory, create
db_config.php
file insidemysql
folder:sudo touch mysql/db_config.php
To edit the file, run this command:
sudo nano mysql/db_config.php
In the nano editor, paste the following lines:
<?php define("DB_USER", "database_user"); define("DB_PASS", "database_password"); define("DB_NAME", "database_name"); ?>
Substitute
database_user
,database_password
, anddatabase_name
with the values you defined earlier in the previous step.To save, press
Ctrl+X
, thenY
, and thenenter
. -
Create the site configuration
Make a configuration file in the directory
/etc/apache2/sites-available/
. You could name it whatever you like but it is recommended you name it as the name of the repository:notes-app-api.conf
. To edit the file, open the terminal, go to/etc/apache2/sites-available
, and run this command on the terminal:sudo nano notes-app-api.conf
Replace
notes-app-api.conf
with your chosen filename if you named it differently.In the Nano editor, paste the following lines:
<VirtualHost *:80> #ServerName notes-app-api.local ServerAdmin webmaster@localhost DocumentRoot /var/www/notes-app-api Header set Access-Control-Allow-Origin "*" Header set Access-Control-Allow-Headers "*" Header set Access-Control-Allow-Methods "PUT, GET, POST, DELETE, OPTIONS" Header set Access-Control-Max-Age "300" <Directory /var/www/notes-app-api> RewriteEngine On RewriteRule ^([A-Za-z0-9\/_-]+)$ index.php </Directory> ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined </VirtualHost> # vim: syntax=apache ts=4 sw=4 sts=4 sr noet
To save, press
Ctrl+X
, thenY
, and thenenter
. -
Enable the site configuration file.
Run the following command to enable the site
sudo a2ensite notes-app-api.conf
-
Reload the Apache2 HTTP Server.
To activate the newly enabled configuration file, you need to reload the Apache2 HTTP Server. Run the following command to reload Apache2 HTTP Server:
sudo service apache2 reload
Verify if the application is running.
Run this command below:
curl -X GET http://localhost:80/notes
If the output matches the following, then the application is running.
{"status":"success","data":{"notes":[]}}
Request:
- Method: POST
- Endpoint: /notes
- Body Request:
{ "title": "Judul Catatan", "tags": ["Tag 1", "Tag 2"], "body": "Konten catatan" }
Response:
No. | Scenario | Status Code | Response Body |
---|---|---|---|
1 | The request succeeds | 201 (Created) |
|
2 | The request fails | 500 (Internal Server Error) |
|
Request:
- Method: GET
- Endpoint: /notes
Response:
No. | Scenario | Status Code | Response Body |
---|---|---|---|
1 | There are some notes | 200 (OK) |
|
2 | There are no notes |
|
Request:
- Method: GET
- Endpoint: /notes/{noteId}
Response:
No. | Scenario | Status Code | Response Body |
---|---|---|---|
1 | The note's id is found |
200 (OK) |
|
2 | The note's id is not found |
404 (Not Found) |
|
Request:
- Method: PUT
- Endpoint: /notes/{noteId}
- Body Request:
{ "title":"Judul Catatan Revisi", "tags":[ "Tag 1", "Tag 2" ], "body":"Konten catatan" }
Response:
No. | Scenario | Status Code | Response Body |
---|---|---|---|
1 | The note is successfully updated | 200 (OK) |
|
2 | The note's id is not found |
404 (Not found) |
|
Request:
- Method: DELETE
- Endpoint: /notes/{noteId}
Response:
No. | Scenario | Status Code | Response Body |
---|---|---|---|
1 | The note is successfully deleted | 200 (OK) |
|
2 | The note's id is not found |
404 (Not Found) |
|
curl -X {HTTP METHOD} -H "Content-Type: application/json" -d {BODY REQUEST} http://localhost:80/{ENDPOINT}
The -H "Content-Type: application/json" -d
can be omitted if there is no body request passed on the request.
curl -X POST -H "Content-Type: application/json" -d "{\"title\": \"Judul Catatan\", \"tags\": [\"Tag 1\", \"Tag 2\"], \"body\": \"Konten Catatan\"}" http://localhost:80/notes
curl -X GET http://localhost:80/notes