refactor: into files
Signed-off-by: kjuulh <contact@kjuulh.io>
This commit is contained in:
parent
cf321d110e
commit
5395e8bb1e
1
Cargo.lock
generated
1
Cargo.lock
generated
@ -915,6 +915,7 @@ dependencies = [
|
||||
"serde",
|
||||
"sqlx",
|
||||
"tokio",
|
||||
"tokio-util",
|
||||
"tower-http",
|
||||
"tracing",
|
||||
"tracing-subscriber",
|
||||
|
@ -23,3 +23,4 @@ sqlx = { version = "0.7.3", features = [
|
||||
uuid = { version = "1.7.0", features = ["v4"] }
|
||||
tower-http = { version = "0.5.2", features = ["cors", "trace"] }
|
||||
mad = { git = "https://github.com/kjuulh/mad", branch = "main" }
|
||||
tokio-util = "0.7.11"
|
||||
|
65
crates/nodata/src/http.rs
Normal file
65
crates/nodata/src/http.rs
Normal file
@ -0,0 +1,65 @@
|
||||
use std::net::SocketAddr;
|
||||
|
||||
use anyhow::Context;
|
||||
use axum::async_trait;
|
||||
use axum::extract::MatchedPath;
|
||||
use axum::http::Request;
|
||||
use axum::routing::get;
|
||||
use axum::Router;
|
||||
use mad::Component;
|
||||
use mad::MadError;
|
||||
use tokio_util::sync::CancellationToken;
|
||||
use tower_http::trace::TraceLayer;
|
||||
|
||||
use crate::state::SharedState;
|
||||
|
||||
pub struct HttpServer {
|
||||
state: SharedState,
|
||||
host: SocketAddr,
|
||||
}
|
||||
|
||||
impl HttpServer {
|
||||
pub fn new(state: &SharedState, host: SocketAddr) -> Self {
|
||||
Self {
|
||||
state: state.clone(),
|
||||
host,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl Component for HttpServer {
|
||||
async fn run(&self, cancellation_token: CancellationToken) -> Result<(), mad::MadError> {
|
||||
let app = Router::new()
|
||||
.route("/", get(root))
|
||||
.with_state(self.state.clone())
|
||||
.layer(
|
||||
TraceLayer::new_for_http().make_span_with(|request: &Request<_>| {
|
||||
let matched_path = request
|
||||
.extensions()
|
||||
.get::<MatchedPath>()
|
||||
.map(MatchedPath::as_str);
|
||||
|
||||
tracing::info_span!(
|
||||
"http_request",
|
||||
method = ?request.method(),
|
||||
matched_path,
|
||||
some_other_field = tracing::field::Empty,
|
||||
)
|
||||
}),
|
||||
);
|
||||
|
||||
tracing::info!("http: listening on {}", self.host);
|
||||
let listener = tokio::net::TcpListener::bind(self.host).await.unwrap();
|
||||
axum::serve(listener, app.into_make_service())
|
||||
.await
|
||||
.context("axum server stopped")
|
||||
.map_err(MadError::Inner)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
async fn root() -> &'static str {
|
||||
"Hello, nodata!"
|
||||
}
|
@ -1,14 +1,12 @@
|
||||
use std::{net::SocketAddr, ops::Deref, sync::Arc};
|
||||
mod http;
|
||||
mod state;
|
||||
|
||||
use std::net::SocketAddr;
|
||||
|
||||
use anyhow::Context;
|
||||
use axum::extract::MatchedPath;
|
||||
use axum::http::Request;
|
||||
use axum::routing::get;
|
||||
use axum::Router;
|
||||
use clap::{Parser, Subcommand};
|
||||
use mad::{Mad, MadError};
|
||||
use sqlx::{Pool, Postgres};
|
||||
use tower_http::trace::TraceLayer;
|
||||
use http::HttpServer;
|
||||
use mad::Mad;
|
||||
use state::SharedState;
|
||||
|
||||
#[derive(Parser)]
|
||||
#[command(author, version, about, long_about = None, subcommand_required = true)]
|
||||
@ -35,82 +33,13 @@ async fn main() -> anyhow::Result<()> {
|
||||
if let Some(Commands::Serve { host }) = cli.command {
|
||||
tracing::info!("Starting service");
|
||||
|
||||
let state = SharedState(Arc::new(State::new().await?));
|
||||
let state = SharedState::new().await?;
|
||||
|
||||
let state = state.clone();
|
||||
Mad::builder()
|
||||
.add_fn(move |_cancel| {
|
||||
let state = state.clone();
|
||||
async move {
|
||||
let app = Router::new()
|
||||
.route("/", get(root))
|
||||
.with_state(state.clone())
|
||||
.layer(TraceLayer::new_for_http().make_span_with(
|
||||
|request: &Request<_>| {
|
||||
let matched_path = request
|
||||
.extensions()
|
||||
.get::<MatchedPath>()
|
||||
.map(MatchedPath::as_str);
|
||||
|
||||
tracing::info_span!(
|
||||
"http_request",
|
||||
method = ?request.method(),
|
||||
matched_path,
|
||||
some_other_field = tracing::field::Empty,
|
||||
)
|
||||
},
|
||||
));
|
||||
|
||||
tracing::info!("listening on {}", host);
|
||||
let listener = tokio::net::TcpListener::bind(host).await.unwrap();
|
||||
axum::serve(listener, app.into_make_service())
|
||||
.await
|
||||
.context("axum server stopped")
|
||||
.map_err(MadError::Inner)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
})
|
||||
.add(HttpServer::new(&state, host))
|
||||
.run()
|
||||
.await?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn root() -> &'static str {
|
||||
"Hello, nodata!"
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct SharedState(Arc<State>);
|
||||
|
||||
impl Deref for SharedState {
|
||||
type Target = Arc<State>;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
|
||||
pub struct State {
|
||||
pub db: Pool<Postgres>,
|
||||
}
|
||||
|
||||
impl State {
|
||||
pub async fn new() -> anyhow::Result<Self> {
|
||||
let db = sqlx::PgPool::connect(
|
||||
&std::env::var("DATABASE_URL").context("DATABASE_URL is not set")?,
|
||||
)
|
||||
.await?;
|
||||
|
||||
sqlx::migrate!("migrations/crdb")
|
||||
.set_locking(false)
|
||||
.run(&db)
|
||||
.await?;
|
||||
|
||||
let _ = sqlx::query("SELECT 1;").fetch_one(&db).await?;
|
||||
|
||||
Ok(Self { db })
|
||||
}
|
||||
}
|
||||
|
43
crates/nodata/src/state.rs
Normal file
43
crates/nodata/src/state.rs
Normal file
@ -0,0 +1,43 @@
|
||||
use std::{ops::Deref, sync::Arc};
|
||||
|
||||
use anyhow::Context;
|
||||
use sqlx::{Pool, Postgres};
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct SharedState(Arc<State>);
|
||||
|
||||
impl SharedState {
|
||||
pub async fn new() -> anyhow::Result<Self> {
|
||||
Ok(Self(Arc::new(State::new().await?)))
|
||||
}
|
||||
}
|
||||
|
||||
impl Deref for SharedState {
|
||||
type Target = Arc<State>;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
|
||||
pub struct State {
|
||||
pub db: Pool<Postgres>,
|
||||
}
|
||||
|
||||
impl State {
|
||||
pub async fn new() -> anyhow::Result<Self> {
|
||||
let db = sqlx::PgPool::connect(
|
||||
&std::env::var("DATABASE_URL").context("DATABASE_URL is not set")?,
|
||||
)
|
||||
.await?;
|
||||
|
||||
sqlx::migrate!("migrations/crdb")
|
||||
.set_locking(false)
|
||||
.run(&db)
|
||||
.await?;
|
||||
|
||||
let _ = sqlx::query("SELECT 1;").fetch_one(&db).await?;
|
||||
|
||||
Ok(Self { db })
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user