Add config
This commit is contained in:
parent
74ea5fc59a
commit
ee22233788
5 changed files with 181 additions and 12 deletions
|
@ -5,7 +5,9 @@ use http::Request;
|
|||
use tower_http::trace::{DefaultOnRequest, DefaultOnResponse, TraceLayer};
|
||||
use tracing::{field, span, Level};
|
||||
|
||||
pub fn build_app() -> Router {
|
||||
use crate::config::Config;
|
||||
|
||||
pub fn build_app(_config: Config) -> Router {
|
||||
Router::new().nest("/yt-embed", yt_embed::router()).layer(
|
||||
TraceLayer::new_for_http()
|
||||
.make_span_with(|request: &Request<Body>| {
|
||||
|
|
13
src/config.rs
Normal file
13
src/config.rs
Normal file
|
@ -0,0 +1,13 @@
|
|||
use std::net::SocketAddr;
|
||||
|
||||
use serde::Deserialize;
|
||||
|
||||
#[derive(Clone, Debug, Deserialize)]
|
||||
pub struct Config {
|
||||
#[serde(default = "default_listen_addr")]
|
||||
pub listen_addr: SocketAddr,
|
||||
}
|
||||
|
||||
fn default_listen_addr() -> SocketAddr {
|
||||
([0, 0, 0, 0], 3000).into()
|
||||
}
|
36
src/main.rs
36
src/main.rs
|
@ -1,18 +1,22 @@
|
|||
mod app;
|
||||
mod config;
|
||||
mod error;
|
||||
mod reqwest;
|
||||
mod youtube;
|
||||
|
||||
use std::net::SocketAddr;
|
||||
|
||||
use eyre::Context;
|
||||
use figment::{
|
||||
providers::{Env, Format, Toml},
|
||||
Figment,
|
||||
};
|
||||
use tokio::runtime::Runtime;
|
||||
use tracing::{debug, field, Level};
|
||||
use tracing_subscriber::{filter, layer::SubscriberExt, util::SubscriberInitExt};
|
||||
|
||||
use self::app::build_app;
|
||||
use crate::config::Config;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> eyre::Result<()> {
|
||||
fn main() -> eyre::Result<()> {
|
||||
let filter = filter::Targets::new()
|
||||
.with_target("honbra_api", Level::TRACE)
|
||||
.with_target("tower_http::trace::on_response", Level::TRACE)
|
||||
|
@ -28,15 +32,25 @@ async fn main() -> eyre::Result<()> {
|
|||
.map_err(eyre::Error::msg)
|
||||
.context("failed to initialize tracing subscriber")?;
|
||||
|
||||
let addr: SocketAddr = ([0, 0, 0, 0], 3000).into();
|
||||
let config: Config = Figment::new()
|
||||
.merge(Toml::file("config.toml"))
|
||||
.merge(Env::raw())
|
||||
.extract()
|
||||
.context("failed to parse config")?;
|
||||
|
||||
debug!(addr = field::display(addr), "binding");
|
||||
let rt = Runtime::new().context("failed to create tokio runtime")?;
|
||||
|
||||
axum::Server::try_bind(&addr)
|
||||
.context("unable to bind to server address")?
|
||||
.serve(build_app().into_make_service())
|
||||
.await
|
||||
.context("server encountered a runtime error")?;
|
||||
debug!(addr = field::display(&config.listen_addr), "binding");
|
||||
|
||||
rt.block_on(async move {
|
||||
Ok::<(), eyre::Error>(
|
||||
axum::Server::try_bind(&config.listen_addr)
|
||||
.context("unable to bind to server address")?
|
||||
.serve(build_app(config).into_make_service())
|
||||
.await
|
||||
.context("server encountered a runtime error")?,
|
||||
)
|
||||
})?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue