Fakultas Ilmu Komputer UI

Skip to content
Snippets Groups Projects
Commit 87147035 authored by Ichlasul Affan's avatar Ichlasul Affan
Browse files

Initial commit.

parents
Branches
No related tags found
No related merge requests found
# Created by https://www.toptal.com/developers/gitignore/api/rust,linux,windows,database,rust-analyzer,visualstudiocode,venv,dotenv,virtualenv,direnv,jenv,git
# Edit at https://www.toptal.com/developers/gitignore?templates=rust,linux,windows,database,rust-analyzer,visualstudiocode,venv,dotenv,virtualenv,direnv,jenv,git
### Database ###
*.accdb
*.db
*.dbf
*.mdb
*.pdb
*.sqlite3
*.db-shm
*.db-wal
### direnv ###
.direnv
.envrc
### dotenv ###
.env
### Git ###
# Created by git for backups. To disable backups in Git:
# $ git config --global mergetool.keepBackup false
*.orig
# Created by git when using merge tools for conflicts
*.BACKUP.*
*.BASE.*
*.LOCAL.*
*.REMOTE.*
*_BACKUP_*.txt
*_BASE_*.txt
*_LOCAL_*.txt
*_REMOTE_*.txt
### JEnv ###
# JEnv local Java version configuration file
.java-version
# Used by previous versions of JEnv
.jenv-version
### Linux ###
*~
# temporary files which can be created if a process still has a handle open of a deleted file
.fuse_hidden*
# KDE directory preferences
.directory
# Linux trash folder which might appear on any partition or disk
.Trash-*
# .nfs files are created when an open file is removed but is still being accessed
.nfs*
### Rust ###
# Generated by Cargo
# will have compiled files and executables
debug/
target/
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
Cargo.lock
# These are backup files generated by rustfmt
**/*.rs.bk
# MSVC Windows builds of rustc generate these, which store debugging information
### rust-analyzer ###
# Can be generated by other build systems other than cargo (ex: bazelbuild/rust_rules)
rust-project.json
### venv ###
# Virtualenv
# http://iamzed.com/2009/05/07/a-primer-on-virtualenv/
.Python
[Bb]in
[Ii]nclude
[Ll]ib
[Ll]ib64
[Ll]ocal
[Ss]cripts
pyvenv.cfg
.venv
pip-selfcheck.json
### VirtualEnv ###
# Virtualenv
# http://iamzed.com/2009/05/07/a-primer-on-virtualenv/
### VisualStudioCode ###
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json
!.vscode/*.code-snippets
# Local History for Visual Studio Code
.history/
# Built Visual Studio Code Extensions
*.vsix
### VisualStudioCode Patch ###
# Ignore all local history of files
.history
.ionide
### Windows ###
# Windows thumbnail cache files
Thumbs.db
Thumbs.db:encryptable
ehthumbs.db
ehthumbs_vista.db
# Dump file
*.stackdump
# Folder config file
[Dd]esktop.ini
# Recycle Bin used on file shares
$RECYCLE.BIN/
# Windows Installer files
*.cab
*.msi
*.msix
*.msm
*.msp
# Windows shortcuts
*.lnk
# End of https://www.toptal.com/developers/gitignore/api/rust,linux,windows,database,rust-analyzer,visualstudiocode,venv,dotenv,virtualenv,direnv,jenv,git
{
"rust-analyzer.showUnlinkedFileNotification": false
}
[package]
name = "bambangshop_receiver"
version = "0.1.0"
edition = "2021"
[dependencies]
rocket = { version = "0.5.0", features = ["json"] }
lazy_static = "1.4.0"
reqwest = { version = "0.12", features = ["json"] }
getset = "0.1.2"
dotenvy = "0.15.7"
# BambangShop Receiver App
Tutorial and Example for Advanced Programming 2024 - Faculty of Computer Science, Universitas Indonesia
---
## About this Project
In this repository, we have provided you a REST (REpresentational State Transfer) API project using Rocket web framework.
This project consists of four modules:
1. `controller`: this module contains handler functions used to receive request and send responses.
In Model-View-Controller (MVC) pattern, this is the Controller part.
2. `model`: this module contains structs that serve as data containers.
In MVC pattern, this is the Model part.
3. `service`: this module contains structs with business logic methods.
In MVC pattern, this is also the Model part.
4. `repository`: this module contains structs that serve as databases.
You can use methods of the struct to get list of objects, or operating an object (create, read, update, delete).
This repository provides a Rocket web framework skeleton that you can work with.
As this is an Observer Design Pattern tutorial repository, you need to implement a feature: `Notification`.
This feature will receive notifications of creation, promotion, and deletion of a product, when this receiver instance is subscribed to a certain product type.
The notification will be sent using HTTP POST request, so you need to make the receiver endpoint in this project.
## API Documentations
You can download the Postman Collection JSON here: https://api.postman.com/collections/1518342-1ab29a75-95d2-4e61-a257-0971e15b75a0?access_key=PMAT-01HT9WP343FDK3QKB7HGE2AKHJ
After you download the Postman Collection, you can try the endpoints inside "BambangShop Receiver" folder.
Postman is an installable client that you can use to test web endpoints using HTTP request.
You can also make automated functional testing scripts for REST API projects using this client.
You can install Postman via this website: https://www.postman.com/downloads/
## How to Run in Development Environment
1. Set up environment variables first by creating `.env` file.
Here is the example of `.env` file:
```bash
ROCKET_PORT=8001
APP_INSTANCE_ROOT_URL=http://localhost:${ROCKET_PORT}
APP_PUBLISHER_ROOT_URL=http://localhost:8000
APP_INSTANCE_NAME=Safira Sudrajat
```
Here are the details of each environment variable:
| variable | type | description |
|-------------------------|--------|-----------------------------------------------------------------|
| ROCKET_PORT | string | Port number that will be listened by this receiver instance. |
| APP_INSTANCE_ROOT_URL | string | URL address where this receiver instance can be accessed. |
| APP_PUUBLISHER_ROOT_URL | string | URL address where the publisher instance can be accessed. |
| APP_INSTANCE_NAME | string | Name of this receiver instance, will be shown on notifications. |
2. Use `cargo run` to run this app.
(You might want to use `cargo check` if you only need to verify your work without running the app.)
3. To simulate multiple instances of BambangShop Receiver (as the tutorial mandates you to do so),
you can open new terminal, then edit `ROCKET_PORT` in `.env` file, then execute another `cargo run`.
For example, if you want to run 3 (three) instances of BambangShop Receiver at port `8001`, `8002`, and `8003`, you can do these steps:
- Edit `ROCKET_PORT` in `.env` to `8001`, then execute `cargo run`.
- Open new terminal, edit `ROCKET_PORT` in `.env` to `8002`, then execute `cargo run`.
- Open another new terminal, edit `ROCKET_PORT` in `.env` to `8003`, then execute `cargo run`.
## Mandatory Checklists (Subscriber)
- [ ] Clone https://gitlab.com/ichlaffterlalu/bambangshop-receiver to a new repository.
- **STAGE 1: Implement models**
- [ ] Commit: `Create Notification model struct.`
- [ ] Commit: `Create SubscriberRequest model struct.`
- [ ] Write answers of your learning module's "Reflection Subscriber-1" questions in this README.
- **STAGE 2: Implement repositories**
- [ ] Commit: `Create Notification database and Notification repository struct skeleton.`
- [ ] Commit: `Implement add function in Notification repository.`
- [ ] Commit: `Implement list_all_as_string function in Notification repository.`
- [ ] Write answers of your learning module's "Reflection Subscriber-2" questions in this README.
- **STAGE 3: Implement services and controllers**
- [ ] Commit: `Create Notification service struct skeleton.`
- [ ] Commit: `Implement subscribe function in Notification service.`
- [ ] Commit: `Implement subscribe function in Notification controller.`
- [ ] Commit: `Implement unsubscribe function in Notification service.`
- [ ] Commit: `Implement unsubscribe function in Notification controller.`
- [ ] Commit: `Implement receive_notification function in Notification service.`
- [ ] Commit: `Implement receive function in Notification controller.`
- [ ] Commit: `Implement list_messages function in Notification service.`
- [ ] Commit: `Implement list function in Notification controller.`
- [ ] Write answers of your learning module's "Reflection Subscriber-3" questions in this README.
## Your Reflections
This is the place for you to write reflections:
### Mandatory (Subscriber) Reflections
#### Reflection Subscriber-1
#### Reflection Subscriber-2
#### Reflection Subscriber-3
[debug]
address = "127.0.0.1"
port = 8001
[release]
address = "0.0.0.0"
port = 8001
use rocket::fairing::AdHoc;
pub fn route_stage() -> AdHoc {
return AdHoc::on_ignite("Initializing controller routes...", |rocket| async {
rocket
.mount("/", routes![])
});
}
use lazy_static::lazy_static;
use dotenvy::dotenv;
use getset::Getters;
use rocket::figment::{Figment, providers::{Serialized, Env}};
use rocket::http::Status;
use rocket::serde::json::Json;
use rocket::serde::{Deserialize, Serialize};
use rocket::response::status::Custom;
use reqwest::{Client, ClientBuilder};
lazy_static! {
pub static ref REQWEST_CLIENT: Client = ClientBuilder::new().build().unwrap();
pub static ref APP_CONFIG: AppConfig = AppConfig::generate();
}
#[derive(Debug, Deserialize, Serialize, Getters)]
#[serde(crate = "rocket::serde")]
pub struct AppConfig {
#[getset(get = "pub with_prefix")]
instance_root_url: String,
#[getset(get = "pub with_prefix")]
pub publisher_root_url: String,
#[getset(get = "pub with_prefix")]
pub instance_name: String
}
impl Default for AppConfig {
fn default() -> AppConfig {
return AppConfig {
instance_root_url: String::from("http://localhost:8001"),
publisher_root_url: String::from("http://localhost:8000"),
instance_name: String::from("BambangShop Receiver")
}
}
}
impl AppConfig {
pub fn generate() -> AppConfig {
dotenv().ok();
return Figment::from(Serialized::defaults(AppConfig::default()))
.merge(Env::prefixed("APP_").global())
.extract().unwrap();
}
}
pub type Result<T, E = Error> = std::result::Result<T, E>;
pub type Error = Custom<Json<ErrorResponse>>;
#[derive(Serialize, Debug, Clone, PartialEq)]
#[serde(crate = "rocket::serde")]
pub struct ErrorResponse {
pub status_code: Status,
pub message: String
}
pub fn compose_error_response(status_code: Status, message: String) -> Custom<Json<ErrorResponse>> {
return Custom(status_code, Json::from(
ErrorResponse {
status_code: status_code,
message: message,
}
));
}
#[macro_use] extern crate rocket;
pub mod controller;
pub mod service;
pub mod repository;
pub mod model;
use dotenvy::dotenv;
use crate::controller::route_stage;
#[launch]
fn rocket() -> _ {
dotenv().ok();
rocket::build()
.manage(reqwest::Client::builder().build().unwrap())
.attach(route_stage())
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment