Rust   |  Web   Todo   |   Create & Read & Delete

ホーム  
はじめの一歩
Web Todo

概要

Rust で Todo ウェブアプリケーションを作っています。 sqlite のデータベースに接続して、Create と Read と Delete を実装しました。

Cargo.toml


[package]
name = "todo"
version = "0.1.0"
edition = "2024"

[dependencies]
askama = "0.14.0"
axum = "0.8.4"
tokio = { version = "1.0", features = ["full"] }
rusqlite = { version = "0.33.0", features = ["bundled"] }
tower-http = { version = "0.6.6", features = ["full"] }
serde = { version = "1.0.225", features = ["derive"] }


main.rs


//******************************
// main.rs
// copyright   : vivacocoa.jp
// last modified: Sep. 18. 2025
//******************************
use axum::{response::{Html, IntoResponse, Redirect}, routing::{get, post}, Router, Form};
use askama::Template;
use serde::Deserialize;
use tower_http::services::ServeDir;

#[derive(Template)]
#[template(path = "index.html")]
struct Todos
{
    todos: Vec<Todo>,
}
#[derive(Clone)]
struct Todo
{
    id: u64,
    message: String,
}
#[derive(Deserialize)]
struct FormData {
    id: u64,
    message: String,
}

#[tokio::main]
async fn main() {
    let connection = rusqlite::Connection::open("test.db").unwrap();

    connection.execute("
        CREATE TABLE IF NOT EXISTS users (
            id INTEGER PRIMARY KEY AUTOINCREMENT, 
            todo TEXT
        )
    ", []).unwrap();

    let serve_dir = ServeDir::new("public");

    let app = Router::new()
        .nest_service("/static", serve_dir)
        .route("/", get(index))
        .route("/delete", post(delete))
        .route("/create", post(create));
        
    let listener = tokio::net::TcpListener::bind("127.0.0.1:3000").await.unwrap();
    let addr = listener.local_addr().unwrap();
    println!("Listening on {}", addr);
    axum::serve(listener, app).await.unwrap();
}

async fn index() -> impl IntoResponse
{
    let connection = rusqlite::Connection::open("test.db").unwrap();
    let mut stat = connection.prepare("SELECT * FROM users").unwrap();
    let mut rows = stat.query([]).unwrap();

    let mut todos = Vec::new();
    while let Some(row) =  rows.next().unwrap() {
        let i_d: u64 = row.get(0).unwrap();
            let todo: String = row.get(1).unwrap();
            todos.push(Todo {message: todo, id: i_d});
    }
    let _ = todos.clone().into_iter().collect::<Vec<_>>();
    let todos = Todos { todos };
    Html(todos.render().unwrap()).into_response()
}

async fn create(Form(data): Form<FormData>) -> Redirect
{
    let connection = rusqlite::Connection::open("test.db").unwrap();
    connection. execute ( "INSERT INTO users (todo) VALUES (?1)", (data.message,)). unwrap ();
    Redirect::to("/")
}

async fn delete(Form(data): Form<FormData>) -> Redirect
{
    println!("delete: {}", data.id);
    let connection = rusqlite::Connection::open("test.db").unwrap();
    let _ = connection.execute("DELETE FROM users WHERE id = ?", (data.id,));
    Redirect::to("/")
}


index.html

プロジェクトのルートディレクトリに templates というディレクトリを作り、 その中に次の index.html を作ります。


<!-----------------------------
  index.html
  copyright    : vivacocoa.jp
  last modified: Sep. 18, 2025
 ------------------------------>
<!DOCTYPE html>
<html lang="ja">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Todo</title>
  <link rel="stylesheet" href="/static/style.css">
</head>

<body>
  <div class="container">
    <h1>Todo</h1>
    <form method="post" action="/create">
      <div class="h-layout">
        <input type="text" name="message" required maxlength="30">
        <input type="hidden" name="id" value="1">
        <button class="btn-black">Create</button>
      </div>
    </form>
    {%for todo in todos %}
    <form method="post" action="/delete">
      <div class="h-layout-2">
        <p class="todo">{{ todo.message }}</p>
        <input type="hidden" name="id" value={{todo.id}}>
        <input type="hidden" name="message" value={{todo.message}}>
        <button class="btn-white">Delete</button>
      </div>
    </form>
    {% endfor %}
  </div>
</body>

</html>


style.css

プロジェクトのルートディレクトリに public というディレクトリを作り、 その中に次の style.css を作ります。


/*****************************
style.css
copyright    : vivacocoa.jp
last modified: Sep. 18, 2025
******************************/
body {
  background-color: #efefef;
}

.container {
  width: 500px;
  margin: 16px auto;
}

h1 {
  font-size: 20px;
}

.todo {
  height: 80px;
  width: 100%;
  border-radius: 4px;
  padding: 0px 8px;
  line-height: 80px;
  box-sizing: border-box;
  background-color: white;
  margin-bottom: 0px;
}

.h-layout {
  display: flex;
}

input {
  height: 25px;
  width: 100%;
  padding: 4px;
  border: solid 0px #efefef;
  border-radius: 4px;
}

.btn-black {
  background-color: black;
  width: 60px;
  color: white;
  border-radius: 4px;
}

.h-layout-2 {
  display: flex;
  position: relative;
}

.btn-white {
  background-color: white;
  border: solid 1px lightgray;
  border-radius: 4px;
  color: gray;
  width: 50px;
  height: 30px;
  position: absolute;
  right: 4px;
  bottom: 4px;
}

ターミナルでプロジェクトのルートディレクトリに移動して、cargo run とコマンドしてサーバーを起動してください。 そしてお使いのブラウザで localhost:3000 もしくは 127.0.0.1:3000 を開いてください。



204 visits
Posted: Sep. 18, 2025
Update: Sep. 18, 2025

ホーム   目次