added crates to public project, made api structure
This commit is contained in:
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, "¯\\_(ツ)_/¯")
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user