Add listener to config
This commit is contained in:
parent
4cd32831c1
commit
e0ef3229ab
5
BookForge.toml
Normal file
5
BookForge.toml
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
database_path = ""
|
||||||
|
|
||||||
|
[listener]
|
||||||
|
port = 8000
|
||||||
|
host = "0.0.0.0"
|
||||||
@ -1 +0,0 @@
|
|||||||
database_path = "/home/torrpenn/projects/Gabatxo1312/bookforge/db.sqlite"
|
|
||||||
14
src/main.rs
14
src/main.rs
@ -1,4 +1,5 @@
|
|||||||
use bookforge::state::error::AppStateError;
|
use bookforge::state::error::AppStateError;
|
||||||
|
use bookforge::state::listener::ListenerError;
|
||||||
use snafu::ErrorCompat;
|
use snafu::ErrorCompat;
|
||||||
use snafu::prelude::*;
|
use snafu::prelude::*;
|
||||||
|
|
||||||
@ -11,6 +12,10 @@ pub enum AppError {
|
|||||||
State {
|
State {
|
||||||
source: AppStateError,
|
source: AppStateError,
|
||||||
},
|
},
|
||||||
|
#[snafu(display("Listener Error"))]
|
||||||
|
Listener {
|
||||||
|
source: ListenerError,
|
||||||
|
},
|
||||||
Error,
|
Error,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -18,9 +23,14 @@ async fn main_inner() -> Result<(), AppError> {
|
|||||||
pretty_env_logger::init();
|
pretty_env_logger::init();
|
||||||
let app_state = AppState::new().await.context(StateSnafu)?;
|
let app_state = AppState::new().await.context(StateSnafu)?;
|
||||||
|
|
||||||
let app = build_app(app_state);
|
let app = build_app(app_state.clone());
|
||||||
|
|
||||||
let listener = tokio::net::TcpListener::bind("0.0.0.0:8000").await.unwrap();
|
let addr = app_state
|
||||||
|
.config
|
||||||
|
.listener
|
||||||
|
.socket_addr()
|
||||||
|
.context(ListenerSnafu)?;
|
||||||
|
let listener = tokio::net::TcpListener::bind(addr).await.unwrap();
|
||||||
|
|
||||||
let _ = axum::serve(listener, app).await;
|
let _ = axum::serve(listener, app).await;
|
||||||
|
|
||||||
|
|||||||
@ -5,6 +5,8 @@ use camino::Utf8PathBuf;
|
|||||||
use dirs::config_dir;
|
use dirs::config_dir;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
use crate::state::listener::Listener;
|
||||||
|
|
||||||
#[derive(Snafu, Debug)]
|
#[derive(Snafu, Debug)]
|
||||||
pub enum ConfigError {
|
pub enum ConfigError {
|
||||||
#[snafu(display("File doesn't exist at path : {path}"))]
|
#[snafu(display("File doesn't exist at path : {path}"))]
|
||||||
@ -30,12 +32,14 @@ pub enum ConfigError {
|
|||||||
pub struct AppConfig {
|
pub struct AppConfig {
|
||||||
#[serde(default = "AppConfig::default_sqlite_path")]
|
#[serde(default = "AppConfig::default_sqlite_path")]
|
||||||
pub database_path: Utf8PathBuf,
|
pub database_path: Utf8PathBuf,
|
||||||
|
pub listener: Listener,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for AppConfig {
|
impl Default for AppConfig {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
AppConfig {
|
AppConfig {
|
||||||
database_path: Self::default_sqlite_path(),
|
database_path: Self::default_sqlite_path(),
|
||||||
|
listener: Listener::default(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
62
src/state/listener.rs
Normal file
62
src/state/listener.rs
Normal file
@ -0,0 +1,62 @@
|
|||||||
|
use snafu::prelude::*;
|
||||||
|
use std::net::{AddrParseError, SocketAddr};
|
||||||
|
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
/// Errors related to listener configuration and socket binding.
|
||||||
|
#[derive(Snafu, Debug)]
|
||||||
|
pub enum ListenerError {
|
||||||
|
/// The provided bind address and/or port could not be parsed into a valid
|
||||||
|
/// `SocketAddr`.
|
||||||
|
#[snafu(display("Socket address is wrong"))]
|
||||||
|
SocketAddrInvalid {
|
||||||
|
socket_addr: String,
|
||||||
|
source: AddrParseError,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Listener configuration used to bind a TCP socket.
|
||||||
|
///
|
||||||
|
/// `bind_addr` represents the interface to bind to (e.g. `0.0.0.0`,
|
||||||
|
/// `127.0.0.1`, `::`), and `port` is the TCP port.
|
||||||
|
#[derive(Clone, Deserialize, Serialize, Debug)]
|
||||||
|
pub struct Listener {
|
||||||
|
pub port: u32,
|
||||||
|
pub bind_addr: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Default listener configuration:
|
||||||
|
/// - bind on all interfaces (`0.0.0.0`)
|
||||||
|
/// - listen on port 8000
|
||||||
|
impl Default for Listener {
|
||||||
|
fn default() -> Self {
|
||||||
|
Listener {
|
||||||
|
port: Self::default_port(),
|
||||||
|
bind_addr: Self::default_bind_addr(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Listener {
|
||||||
|
/// Default bind address used when none is specified.
|
||||||
|
fn default_bind_addr() -> String {
|
||||||
|
"0.0.0.0".to_string()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Default port used whend none is specified
|
||||||
|
fn default_port() -> u32 {
|
||||||
|
8000
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Computes the socket address used for binding.
|
||||||
|
///
|
||||||
|
/// # Errors
|
||||||
|
/// Returns `ListenerError::SocketAddrInvalid` if the address or port
|
||||||
|
/// cannot be parsed into a valid `SocketAddr`.
|
||||||
|
pub fn socket_addr(&self) -> Result<SocketAddr, ListenerError> {
|
||||||
|
let socket_addr = format!("{}:{}", self.bind_addr, self.port);
|
||||||
|
socket_addr
|
||||||
|
.parse()
|
||||||
|
.context(SocketAddrInvalidSnafu { socket_addr })
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -7,6 +7,7 @@ use sea_orm_migration::MigratorTrait;
|
|||||||
|
|
||||||
pub mod config;
|
pub mod config;
|
||||||
pub mod error;
|
pub mod error;
|
||||||
|
pub mod listener;
|
||||||
|
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
pub struct AppState {
|
pub struct AppState {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user