use base64::{prelude::BASE64_STANDARD, Engine}; use fully_pub::fully_pub; use anyhow::{Result, Context}; use log::info; use minijinja::{context, Environment}; use sqlx::{Pool, Sqlite}; use crate::{models::config::{AppSecrets, Config}, router::build_router}; fn build_templating_env(config: &Config) -> Environment<'static> { let mut env = Environment::new(); minijinja_embed::load_templates!(&mut env); env.add_global("gl", context! { instance => config.instance }); env.add_function("encode_b64str", |bin_val: Vec| { BASE64_STANDARD.encode(bin_val) }); env } #[derive(Debug)] #[fully_pub] /// HTTP server arguments pub struct ServerConfig { listen_host: String, listen_port: u32, assets_path: String } #[derive(Debug, Clone)] #[fully_pub] pub struct AppState { secrets: AppSecrets, config: Config, db: Pool, templating_env: Environment<'static> } pub async fn start_http_server( server_config: ServerConfig, config: Config, secrets: AppSecrets, db_pool: Pool ) -> Result<()> { // build state let state = AppState { templating_env: build_templating_env(&config), config, secrets, db: db_pool }; // build routes let services = build_router( &server_config, state.clone() ) .with_state(state); let listen_addr = format!("{}:{}", server_config.listen_host, server_config.listen_port); info!("Starting web server on http://{}", &listen_addr); let listener = tokio::net::TcpListener::bind(listen_addr).await.unwrap(); axum::serve(listener, services).await.context("Axum serve")?; Ok(()) }