Download src/main.rs from arami21/todo: direct link, hf CLI and curl.
- Browser
- Download file 3.46 kB
-
https://huggingface.co/spaces/arami21/todo/resolve/main/src/main.rs
- Command line
-
hf download hf://spaces/arami21/todo/src/main.rs
-
curl -L -o main.rs https://huggingface.co/spaces/arami21/todo/resolve/main/src/main.rs
3.46 kB
| use actix_web::{ | |
| App, HttpResponse, HttpServer, Responder, delete, get, post, put, | |
| web::{self, Data, Json}, | |
| }; | |
| use serde::{Deserialize, Serialize}; | |
| use sqlx::{PgPool, prelude::FromRow}; | |
| async fn main() -> std::io::Result<()> { | |
| println!("START APP"); | |
| dotenvy::dotenv().ok(); | |
| println!("LOAD ENV"); | |
| let pool = db().await; | |
| println!("DATABASE CONNECTED"); | |
| println!("START SERVER"); | |
| HttpServer::new(move || { | |
| App::new() | |
| .app_data(Data::new(pool.clone())) | |
| .service(todo_list) | |
| .service(add_todo) | |
| .service(get_single_todo) | |
| .service(delete_todo) | |
| .service(update_todo) | |
| }) | |
| .bind(("0.0.0.0", 7860))? | |
| .run() | |
| .await | |
| } | |
| async fn todo_list(pool: web::Data<PgPool>) -> impl Responder { | |
| let todos: Vec<TodoSql> = sqlx::query_as("Select * FROM todos") | |
| .persistent(false) | |
| .fetch_all(pool.get_ref()) | |
| .await | |
| .unwrap(); | |
| let todos_json = serde_json::to_string_pretty(&todos).unwrap(); | |
| HttpResponse::Ok().body(todos_json) | |
| } | |
| async fn add_todo(pool: web::Data<PgPool>, todo_req: Json<TodoReq>) -> impl Responder { | |
| sqlx::query("INSERT INTO todos (content) VALUES ($1)") | |
| .persistent(false) | |
| .bind(&todo_req.content) | |
| .execute(pool.get_ref()) | |
| .await | |
| .unwrap(); | |
| HttpResponse::Ok().body("Add new Todo") | |
| } | |
| async fn get_single_todo(pool: web::Data<PgPool>, path: web::Path<i32>) -> impl Responder { | |
| let id = path.into_inner(); | |
| let rows: Vec<TodoSql> = sqlx::query_as("SELECT * FROM todos WHERE id = $1") | |
| .persistent(false) | |
| .bind(&id) | |
| .fetch_all(pool.get_ref()) | |
| .await | |
| .unwrap(); | |
| if rows.len() == 0 { | |
| let msg = format!("NO todo : id : {} found!", id); | |
| HttpResponse::NotFound().body(msg) | |
| } else { | |
| let todo_json = serde_json::to_string_pretty(&rows[0]).unwrap(); | |
| HttpResponse::Ok().body(todo_json) | |
| } | |
| } | |
| async fn delete_todo(pool: web::Data<PgPool>, path: web::Path<i32>) -> impl Responder { | |
| let id = path.into_inner(); | |
| match sqlx::query("DELETE FROM todos WHERE id = $1") | |
| .persistent(false) | |
| .bind(&id) | |
| .execute(pool.get_ref()) | |
| .await | |
| { | |
| Ok(_) => HttpResponse::Ok().body("Remove Todo!"), | |
| Err(err) => HttpResponse::InternalServerError().body(format!("Database Error: {}", err)), | |
| } | |
| } | |
| async fn update_todo( | |
| pool: web::Data<PgPool>, | |
| path: web::Path<i32>, | |
| todo_req: Json<TodoReq>, | |
| ) -> impl Responder { | |
| let id = path.into_inner(); | |
| match sqlx::query("UPDATE todos SET content = $1 WHERE id = $2") | |
| .persistent(false) | |
| .bind(&todo_req.content) | |
| .bind(&id) | |
| .execute(pool.get_ref()) | |
| .await | |
| { | |
| Ok(_) => HttpResponse::Ok().body("Update Todo!"), | |
| Err(err) => HttpResponse::InternalServerError().body(format!("Database Error: {}", err)), | |
| } | |
| } | |
| async fn db() -> PgPool { | |
| let database_url = std::env::var("DATABASE_URL").expect("DATABASE_URL not found"); | |
| let pool = sqlx::postgres::PgPool::connect(&database_url) | |
| .await | |
| .unwrap(); | |
| pool | |
| } | |
| struct TodoSql { | |
| id: i32, | |
| content: String, | |
| } | |
| struct TodoReq { | |
| content: String, | |
| } | |