added crates to public project, made api structure

This commit is contained in:
Wyatt J. Miller 2024-09-22 22:38:35 -04:00
parent 3afff00b29
commit cf199ab48a
12 changed files with 2327 additions and 2 deletions

2234
backend/public/Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -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"

View File

@ -0,0 +1,2 @@
pub struct AuthorsDatasource;
impl AuthorsDatasource {}

View File

@ -0,0 +1,2 @@
pub struct CommentsDatasource;
impl CommentsDatasource {}

View File

@ -0,0 +1,3 @@
pub mod authors;
pub mod comments;
pub mod posts;

View File

@ -0,0 +1,2 @@
pub struct PostsDatasource;
impl PostsDatasource {}

View File

@ -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();
}

View File

@ -0,0 +1,2 @@
pub struct AuthorsRoute;
impl AuthorsRoute {}

View File

@ -0,0 +1,2 @@
pub struct CommentsRoute;
impl CommentsRoute {}

View File

@ -0,0 +1,4 @@
pub mod authors;
pub mod comments;
pub mod posts;
pub mod root;

View File

@ -0,0 +1,2 @@
pub struct PostsRoute;
impl PostsRoute {}

View 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, "¯\\_(ツ)_/¯")
}
}