Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Store can now be converted into an Axum REST API Router.
- Loading branch information
Showing
9 changed files
with
462 additions
and
3 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
use axum::Router; | ||
use miniorm::prelude::*; | ||
use serde::{Deserialize, Serialize}; | ||
use sqlx::{FromRow, PgPool}; | ||
use std::net::SocketAddr; | ||
use tokio::net::TcpListener; | ||
|
||
#[derive(Debug, Clone, Eq, PartialEq, FromRow, Entity, Serialize, Deserialize)] | ||
struct Todo { | ||
#[postgres(TEXT NOT NULL)] | ||
description: String, | ||
|
||
#[postgres(BOOLEAN NOT NULL DEFAULT false)] | ||
done: bool, | ||
} | ||
|
||
impl Todo { | ||
pub fn new(description: impl AsRef<str>) -> Self { | ||
let description = description.as_ref().to_string(); | ||
let done = false; | ||
Todo { description, done } | ||
} | ||
} | ||
|
||
#[tokio::main] | ||
async fn main() -> Result<(), Box<dyn std::error::Error>> { | ||
// connect to postgres | ||
dotenv::dotenv().ok(); | ||
let url = std::env::var("POSTGRES_URL").expect("missing POSTGRES_URL env"); | ||
let db = PgPool::connect(&url).await?; | ||
|
||
// initialize todo store | ||
let todos = Store::new(db); | ||
todos.recreate_table().await?; | ||
todos.create(Todo::new("do the laundry")).await?; | ||
todos.create(Todo::new("wash the dishes")).await?; | ||
todos.create(Todo::new("go walk the dog")).await?; | ||
todos.create(Todo::new("groceries")).await?; | ||
|
||
// create the app | ||
let app = Router::new().nest("/todos", todos.into_axum_router()); | ||
let addr = SocketAddr::from(([127, 0, 0, 1], 3000)); | ||
println!("listening on http://{}", addr); | ||
|
||
// serve the app | ||
let listener = TcpListener::bind(&addr).await.unwrap(); | ||
axum::serve(listener, app).await?; | ||
|
||
Ok(()) | ||
} |
Oops, something went wrong.