Add web app boilerplate
This commit is contained in:
parent
a76610331e
commit
d0171df934
|
@ -4,7 +4,7 @@ version = "0.1.0"
|
|||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
tokio = { version = "1", features = ["rt", "signal", "macros"] }
|
||||
tokio = { version = "1", features = ["rt", "sync", "signal", "macros"] }
|
||||
toml = "0.5"
|
||||
serde = { version = "1.0", features = ["derive"] }
|
||||
sqlx = { version = "0.5", default-features = false, features = ["runtime-tokio-rustls", "postgres"] }
|
||||
|
|
|
@ -1,3 +1,21 @@
|
|||
/*
|
||||
* tmtd - Suckless To Do list
|
||||
* Copyright (C) 2022 lemon.sh & famfo
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the Free
|
||||
* Software Foundation, either version 3 of the License, or (at your option)
|
||||
* any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||
* more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with
|
||||
* this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
use sqlx::postgres::{PgConnectOptions, PgConnectionInfo, PgPoolOptions};
|
||||
use sqlx::{ConnectOptions, PgPool};
|
||||
use tracing::info;
|
||||
|
@ -30,4 +48,8 @@ impl Database {
|
|||
pub fn pool(&self) -> PgPool {
|
||||
self.pool.clone()
|
||||
}
|
||||
|
||||
pub async fn close(&self) {
|
||||
self.pool.close().await;
|
||||
}
|
||||
}
|
||||
|
|
64
src/main.rs
64
src/main.rs
|
@ -17,26 +17,80 @@
|
|||
*/
|
||||
|
||||
use crate::{config::Config, database::Database};
|
||||
use std::env;
|
||||
use std::{env, sync::Arc};
|
||||
use std::str::FromStr;
|
||||
use std::time::Duration;
|
||||
use async_sqlx_session::PostgresSessionStore;
|
||||
use tokio::sync::broadcast;
|
||||
use tokio::task::JoinHandle;
|
||||
use tokio::time::sleep;
|
||||
use tracing::{error, info};
|
||||
use tracing::{debug, error, info, Level};
|
||||
use crate::web::App;
|
||||
|
||||
mod config;
|
||||
mod database;
|
||||
mod web;
|
||||
|
||||
#[cfg(unix)]
|
||||
async fn terminate_signal() {
|
||||
use tokio::signal::unix::{signal, SignalKind};
|
||||
let mut sigterm = signal(SignalKind::terminate()).unwrap();
|
||||
let mut sigint = signal(SignalKind::interrupt()).unwrap();
|
||||
debug!("Installed ctrl+c handler");
|
||||
tokio::select! {
|
||||
_ = sigterm.recv() => {},
|
||||
_ = sigint.recv() => {}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(windows)]
|
||||
async fn terminate_signal() {
|
||||
use tokio::signal::windows::ctrl_c;
|
||||
let mut ctrlc = ctrl_c().unwrap();
|
||||
debug!("Installed ctrl+c handler");
|
||||
let _ = ctrlc.recv().await;
|
||||
}
|
||||
|
||||
#[tokio::main(flavor = "current_thread")]
|
||||
async fn main() -> anyhow::Result<()> {
|
||||
let config_var = env::var("TMTD_CONFIG");
|
||||
let config_path = config_var.as_deref().unwrap_or("tmtd.toml");
|
||||
println!("Loading config from '{}'...", config_path);
|
||||
let cfg = Config::load_from_file(config_path).await?;
|
||||
let cfg = Arc::new(Config::load_from_file(config_path).await?);
|
||||
|
||||
tracing_subscriber::fmt::fmt()
|
||||
.with_max_level({
|
||||
if let Some(o) = cfg.log_level.as_deref() {
|
||||
Level::from_str(o)?
|
||||
} else {
|
||||
Level::INFO
|
||||
}
|
||||
})
|
||||
.init();
|
||||
|
||||
info!(concat!("Initializing - tmtd ", env!("CARGO_PKG_VERSION")));
|
||||
let database = Database::connect(&cfg.connection_string).await?;
|
||||
let (ctx, _) = broadcast::channel(1);
|
||||
let database = Arc::new(Database::connect(&cfg.connection_string).await?);
|
||||
let session_store = PostgresSessionStore::from_client(database.pool()).with_table_name("sessions");
|
||||
session_store.migrate().await?;
|
||||
|
||||
let cleanup_task = spawn_session_cleanup_task(&session_store, Duration::from_secs(600));
|
||||
info!("Started session cleanup task");
|
||||
|
||||
let web_app = App::new(cfg.clone(), database.clone())?;
|
||||
let web_task = tokio::spawn(web_app.run(ctx.subscribe()));
|
||||
info!("Started the web app at http://{}", cfg.listen_addr);
|
||||
|
||||
terminate_signal().await;
|
||||
|
||||
cleanup_task
|
||||
.await
|
||||
.unwrap_or_else(|e| error!("Couldn't join cleanup task: {}", e));
|
||||
web_task
|
||||
.await
|
||||
.unwrap_or_else(|e| error!("Couldn't join web task: {}", e));
|
||||
database.close().await;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
@ -50,4 +104,4 @@ fn spawn_session_cleanup_task(store: &PostgresSessionStore, period: Duration) ->
|
|||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
49
src/web.rs
Normal file
49
src/web.rs
Normal file
|
@ -0,0 +1,49 @@
|
|||
/*
|
||||
* tmtd - Suckless To Do list
|
||||
* Copyright (C) 2022 lemon.sh & famfo
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the Free
|
||||
* Software Foundation, either version 3 of the License, or (at your option)
|
||||
* any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||
* more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with
|
||||
* this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
use crate::{Config, Database};
|
||||
use std::sync::Arc;
|
||||
use tokio::sync::broadcast;
|
||||
use warp::Filter;
|
||||
use tera::Tera;
|
||||
|
||||
pub struct App {
|
||||
config: Arc<Config>,
|
||||
db: Arc<Database>,
|
||||
tera: Tera
|
||||
}
|
||||
|
||||
impl App {
|
||||
pub fn new(config: Arc<Config>, db: Arc<Database>) -> Result<Self, tera::Error> {
|
||||
Ok(Self {
|
||||
config,
|
||||
db,
|
||||
tera: Tera::new("templates/**/*.html")?
|
||||
})
|
||||
}
|
||||
|
||||
pub async fn run(self, mut cancel: broadcast::Receiver<()>) {
|
||||
let hello = warp::path!("hello" / String).map(|name| format!("Hello, {}!", name));
|
||||
|
||||
let (_, server) =
|
||||
warp::serve(hello).bind_with_graceful_shutdown(self.config.listen_addr, async move {
|
||||
cancel.recv().await.unwrap();
|
||||
});
|
||||
server.await;
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue