Open1
Rust axum + SQLite 試すメモ

概要
- Rust axum 試すメモです。
- SQLiteの、 CRUD的な APIです。
- 今回も、Rustコードは AIコーディング的な実装です。
[ 公開 2025/06/23 ]
環境
- rustc 1.87.0
- cargo 1.87.0
- ubuntu 22 (windows WSL)
- axum
- tokio
書いたコード
- Cargo.toml
[package]
name = "routes_example"
version = "0.1.0"
edition = "2024"
[dependencies]
axum = "0.7.5"
tokio = { version = "1.0", features = ["full"] }
sqlx = { version = "0.8", features = ["runtime-tokio-rustls", "sqlite", "chrono"] }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
chrono = { version = "0.4", features = ["serde"] }
- start
cargo build
cargo new
- sqlite4/src/main.rs
- ルーティング: /list から /update のあたり。
let app = Router::new()
.route("/", get(root))
.route("/list", get(list_todos))
.route("/create", post(create_todo))
.route("/delete", post(delete_todo))
.route("/update", post(update_todo))
.with_state(app_state);
- table
CREATE TABLE todos (
id INTEGER PRIMARY KEY AUTOINCREMENT,
title TEXT NOT NULL,
content TEXT,
created_at TEXT,
updated_at TEXT
);
- Db, table を作成しておきます。
- API 起動すると localhot:3000 のパスが home