Skip to content

Jiichiro/learning-rust

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Rust REST API dengan MySQL dan RSA Authentication

Prerequisites

  1. Rust - Install dari https://rustup.rs/
  2. MySQL Server - Pastikan berjalan di localhost:3306
  3. Database - Buat database baru untuk aplikasi ini

Setup Database

Buat database MySQL:

CREATE DATABASE restful_api_db;

Setup Environment

  1. Copy file .env.example ke .env:
cp .env.example .env
  1. Edit file .env dengan kredensial MySQL Anda:
DATABASE_URL=mysql://root:your_password@localhost:3306/restful_api_db
RUST_LOG=info

Menjalankan Aplikasi

  1. Install dependencies:
cargo build
  1. Jalankan aplikasi:
cargo run
  1. copy rsa key yang sudah di generate kedalam .env file
=== PRIVATE KEY ===
-----BEGIN PRIVATE KEY-----
\nMIIEvwIBADANBgkqhkiG9w0BAQEF...
-----END PRIVATE KEY-----\n

=== PUBLIC KEY ===
-----BEGIN PUBLIC KEY-----
\nMIIBIjANBgkqhkiG9w0BAQEFAAOC...
-----END PUBLIC KEY-----\n

Jalankan ulang dan jika berhasil, Anda akan melihat output seperti:

Starting application...
Database URL loaded: mysql://root:password@localhost:3306/restful_api_db
Attempting to connect to MySQL...
Successfully connected to MySQL database!
Setting up database tables...
Database setup completed successfully!
Starting HTTP server...
Server ready at http://localhost:8080

Testing API

1. Login untuk mendapatkan token:

curl -X POST http://localhost:8080/api/auth/login \
  -H "Content-Type: application/json" \
  -d '{"username": "admin", "password": "password"}'

Response:

{"token": "your_jwt_token_here"}

2. Gunakan token untuk mengakses API:

curl -X GET http://localhost:8080/api/data \
  -H "Authorization: Bearer your_jwt_token_here"

3. Buat data baru:

curl -X POST http://localhost:8080/api/data \
  -H "Authorization: Bearer your_jwt_token_here" \
  -H "Content-Type: application/json" \
  -d '{
    "title": "Task 1",
    "description": "First task description",
    "status": "pending",
    "image": "task1.jpg",
    "details": [
      {
        "name": "Subtask 1",
        "progress": "50%",
        "target": "100%"
      },
      {
        "name": "Subtask 2", 
        "progress": "0%",
        "target": "100%"
      }
    ]
  }'

Troubleshooting

Error: "DATABASE_URL environment variable not set"

  • Pastikan file .env ada di root directory
  • Pastikan DATABASE_URL sudah diset dengan benar

Error: "Failed to connect to MySQL"

  • Pastikan MySQL server berjalan
  • Pastikan database sudah dibuat
  • Periksa username, password, host, dan port

Error: "Failed to setup database"

  • Pastikan user MySQL memiliki permission untuk CREATE TABLE
  • Periksa sintaks SQL di file database.rs

Server tidak merespons

  • Pastikan port 8080 tidak digunakan aplikasi lain
  • Periksa firewall settings

API Endpoints

  • GET /api/health - Health check (no auth required)
  • POST /api/auth/register - Register a new user
  • POST /api/auth/login - Login to get token
  • GET /api/data - Get all data with pagination
  • GET /api/data/{id} - Get specific data
  • POST /api/data - Create new data
  • PUT /api/data/{id} - Update data
  • DELETE /api/data/{id} - Delete data
  • PUT /api/data/{id}/payment - Update payment for specific data
  • DELETE /api/data/{id}/payment - Delete payment for specific data

Struktur Database

Table: list_data

  • id VARCHAR(8) PRIMARY KEY
  • title TEXT NOT NULL
  • description TEXT NOT NULL
  • status ENUM('pending', 'in progress', 'done') NOT NULL DEFAULT 'pending'
  • image TEXT NOT NULL
  • start_date TIMESTAMP NULL
  • end_date TIMESTAMP NULL
  • created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP

Table: details

  • id BIGINT AUTO_INCREMENT PRIMARY KEY
  • list_id VARCHAR(8) NOT NULL
  • name TEXT NOT NULL
  • progress VARCHAR(255) NOT NULL
  • target VARCHAR(255) NOT NULL
  • FOREIGN KEY (list_id) REFERENCES list_data(id) ON DELETE CASCADE

Table: payment

  • id BIGINT AUTO_INCREMENT PRIMARY KEY
  • list_id VARCHAR(8) NOT NULL
  • name TEXT NOT NULL
  • type enum('cash', 'cashless') NOT NULL
  • amount BIGINT NOT NULL
  • date TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
  • FOREIGN KEY (list_id) REFERENCES list_data(id) ON DELETE CASCADE

Table: users

  • id BIGINT AUTO_INCREMENT PRIMARY KEY
  • username VARCHAR(255) NOT NULL UNIQUE
  • password_hash TEXT NOT NULL
  • email VARCHAR(255) NULL UNIQUE
  • role ENUM('user', 'admin') NOT NULL DEFAULT 'user'
  • active BOOLEAN DEFAULT TRUE
  • created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
  • updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
  • deleted_at TIMESTAMP NULL DEFAULT NULL

About

make a simple api

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages