Skip to content

yuto-kimura-g/todo-app-api-rs

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

todo-app-api

mattnさんの記事 (技術アウトプットに自作TODOアプリ) を読んで,TODOアプリを作ってみた.

Tech Stack

  • Rust (programming language)
  • Actix Web (api framework)
  • mold (a fast linker)
  • Diesel (or mapper)
  • MySQL (rdbms)

API (CRUD)

  • GET /
    • args: None
    • returns: index page
  • GET /tasks
    • args: None
    • returns: Vec<Task>
  • POST /tasks
    • args: NewTask
    • returns: Task
  • PUT /tasks/{id}
    • args: id, NewTask
    • returns: Task
  • DELETE /tasks/{id}
    • args: id
    • returns: None
// src/models.rs
struct Task {
  id: i32,
  title: String,
  description: Option<String>,
  due_date: Option<chrono::NaiveDateTime>,
  is_done: bool,
}
struct NewTask {
  title: String,
  description: Option<String>,
  due_date: Option<chrono::NaiveDateTime>,
  is_done: bool,
}

LOG

# compile mold linker (aproox. 30 min.)
git clone --branch stable https://github.com/rui314/mold.git
cd mold
./install-build-deps.sh
cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_CXX_COMPILER=c++ -B build
cmake --build build -j$(nproc)
sudo cmake --build build --target install

cargo new todo-app-api
rustc --version
rustup toolchain list
rustup toolchain add 1.80.0  # diesel_cli requires rustc 1.78.0 or newer
# edit rust-toolchain.toml
# edit Cargo.toml
cargo install cargo-watch
# mold -run ...
# cargo watch -x ...
# cargo run
mold -run cargo watch -x run
mold -run cargo run --bin get_tasks
# test
curl -X GET http://localhost:8080/
# also, use Postman
cargo clean

sudo apt update
sudo apt install libmysqlclient-dev
sudo mysql -u root
mysql> select user, host, plugin from mysql.user;
mysql> update mysql.user set plugin = 'caching_sha2_password' where user = 'root';
mysql> FLUSH PRIVILEGES;
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'password';
mysql> FLUSH PRIVILEGES;
mysql> quit;

# mysql -u <user> -p<password>
mysql -u root -ppassword
mysql> show databases;
mysql> show tables from todoapp_db;
mysql> show columns from tasks from todoapp_db;
mysql> use todoapp_db;
mysql> select * from tasks;
mysql> truncate table tasks; # reset

cargo install diesel_cli --no-default-features --features mysql
# edit .env (DATABASE_URL)
diesel setup
diesel migration generate create_tasks_table
# edit up.sql and down.sql
diesel migration run  # generate src/schema.rs
diesel print-schema
diesel migration redo

References