added crates to public project, made api structure
This commit is contained in:
parent
3afff00b29
commit
cf199ab48a
2234
backend/public/Cargo.lock
generated
2234
backend/public/Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
@ -2,7 +2,21 @@
|
||||
name = "public"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
authors = ["Wyatt J. Miller <wyatt@wyattjmiller.com"]
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
axum = "0.7.6"
|
||||
tokio = { version = "1.40.0", features = ["full"] }
|
||||
sqlx = { version = "0.8.2", features = [
|
||||
"runtime-tokio-rustls",
|
||||
"postgres",
|
||||
"chrono",
|
||||
"json",
|
||||
] }
|
||||
tracing = "0.1"
|
||||
tracing-subscriber = { version = "0.3.18", features = ["env-filter"] }
|
||||
dotenvy = "0.15.7"
|
||||
serde = "1.0.210"
|
||||
serde_json = "1.0.128"
|
||||
|
2
backend/public/src/datasources/authors.rs
Normal file
2
backend/public/src/datasources/authors.rs
Normal file
@ -0,0 +1,2 @@
|
||||
pub struct AuthorsDatasource;
|
||||
impl AuthorsDatasource {}
|
2
backend/public/src/datasources/comments.rs
Normal file
2
backend/public/src/datasources/comments.rs
Normal file
@ -0,0 +1,2 @@
|
||||
pub struct CommentsDatasource;
|
||||
impl CommentsDatasource {}
|
3
backend/public/src/datasources/mod.rs
Normal file
3
backend/public/src/datasources/mod.rs
Normal file
@ -0,0 +1,3 @@
|
||||
pub mod authors;
|
||||
pub mod comments;
|
||||
pub mod posts;
|
2
backend/public/src/datasources/posts.rs
Normal file
2
backend/public/src/datasources/posts.rs
Normal file
@ -0,0 +1,2 @@
|
||||
pub struct PostsDatasource;
|
||||
impl PostsDatasource {}
|
@ -1,3 +1,46 @@
|
||||
fn main() {
|
||||
println!("Hello, world!");
|
||||
use axum::{
|
||||
routing::{get, post},
|
||||
Router,
|
||||
};
|
||||
use sqlx::postgres::PgPoolOptions;
|
||||
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
|
||||
|
||||
use std::time::Duration;
|
||||
use tokio::net::TcpListener;
|
||||
|
||||
mod datasources;
|
||||
mod routes;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
let _ = dotenvy::dotenv();
|
||||
tracing_subscriber::registry()
|
||||
.with(
|
||||
tracing_subscriber::EnvFilter::try_from_default_env()
|
||||
.unwrap_or_else(|_| format!("{}=debug", env!("CARGO_CRATE_NAME")).into()),
|
||||
)
|
||||
.with(tracing_subscriber::fmt::layer())
|
||||
.init();
|
||||
|
||||
let db_connection_str = std::env::var("DATABASE_URL")
|
||||
.unwrap_or_else(|_| "postgres://postgres:password@localhost".to_string());
|
||||
|
||||
// set up connection pool
|
||||
let pool = PgPoolOptions::new()
|
||||
.max_connections(10)
|
||||
.acquire_timeout(Duration::from_secs(3))
|
||||
.connect(&db_connection_str)
|
||||
.await
|
||||
.expect("can't connect to database");
|
||||
|
||||
// build our application with some routes
|
||||
let app = Router::new()
|
||||
.route("/", get(routes::root::RootRoute::root))
|
||||
.fallback(routes::root::RootRoute::not_found)
|
||||
.with_state(pool);
|
||||
|
||||
// run it with hyper
|
||||
let listener = TcpListener::bind("0.0.0.0:3000").await.unwrap();
|
||||
tracing::debug!("listening on {}", listener.local_addr().unwrap());
|
||||
axum::serve(listener, app).await.unwrap();
|
||||
}
|
||||
|
2
backend/public/src/routes/authors.rs
Normal file
2
backend/public/src/routes/authors.rs
Normal file
@ -0,0 +1,2 @@
|
||||
pub struct AuthorsRoute;
|
||||
impl AuthorsRoute {}
|
2
backend/public/src/routes/comments.rs
Normal file
2
backend/public/src/routes/comments.rs
Normal file
@ -0,0 +1,2 @@
|
||||
pub struct CommentsRoute;
|
||||
impl CommentsRoute {}
|
4
backend/public/src/routes/mod.rs
Normal file
4
backend/public/src/routes/mod.rs
Normal file
@ -0,0 +1,4 @@
|
||||
pub mod authors;
|
||||
pub mod comments;
|
||||
pub mod posts;
|
||||
pub mod root;
|
2
backend/public/src/routes/posts.rs
Normal file
2
backend/public/src/routes/posts.rs
Normal file
@ -0,0 +1,2 @@
|
||||
pub struct PostsRoute;
|
||||
impl PostsRoute {}
|
15
backend/public/src/routes/root.rs
Normal file
15
backend/public/src/routes/root.rs
Normal file
@ -0,0 +1,15 @@
|
||||
use axum::{
|
||||
http::StatusCode,
|
||||
response::{Html, IntoResponse},
|
||||
};
|
||||
|
||||
pub struct RootRoute;
|
||||
impl RootRoute {
|
||||
pub async fn root() -> Html<&'static str> {
|
||||
Html("<p>Copyright Wyatt J. Miller 2024</p>")
|
||||
}
|
||||
|
||||
pub async fn not_found() -> impl IntoResponse {
|
||||
(StatusCode::NOT_FOUND, "¯\\_(ツ)_/¯")
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user