calathea/src/main.rs

88 lines
2.3 KiB
Rust

mod db;
mod server;
use std::sync::Arc;
use r2d2_sqlite::SqliteConnectionManager;
use tera::Tera;
use tower_http::compression::CompressionLayer;
const MIGRATIONS: [&str; 4] = [
include_str!("migrations/000_initial.sql"),
include_str!("migrations/001_plants.sql"),
include_str!("migrations/002_comments.sql"),
include_str!("migrations/003_events.sql"),
];
const STATIC_FILES: [(&str, &'static str); 1] = [(
"htmx_2.0.4.min.js",
include_str!("static/htmx_2.0.4.min.js"),
)];
#[derive(Clone)]
pub struct Context {
pool: db::DbPool,
tera: Arc<Tera>,
}
#[tokio::main]
async fn main() {
tracing_subscriber::fmt::init();
let manager = SqliteConnectionManager::file("db.sqlite");
let pool = r2d2::Pool::new(manager).unwrap();
db::run_migrations(&pool, &MIGRATIONS).unwrap();
let tera = load_templates();
let ctx = Context {
pool,
tera: Arc::new(tera),
};
let app = server::app(ctx).layer(CompressionLayer::new().br(true).gzip(true));
let address = "0.0.0.0:8000";
tracing::info!("Starting server on {address}");
let listener = tokio::net::TcpListener::bind(address).await.unwrap();
axum::serve(listener, app.into_make_service())
.await
.unwrap();
}
fn load_templates() -> Tera {
let mut tera = Tera::default();
tera.add_raw_templates(vec![
("base.html", include_str!("templates/base.html")),
("index.html", include_str!("templates/index.html")),
(
"partials/plants_ul.html",
include_str!("templates/partials/plants_ul.html"),
),
(
"partials/plant_li.html",
include_str!("templates/partials/plant_li.html"),
),
("plant_page.html", include_str!("templates/plant_page.html")),
(
"partials/plant_info.html",
include_str!("templates/partials/plant_info.html"),
),
(
"partials/comment_li.html",
include_str!("templates/partials/comment_li.html"),
),
(
"partials/event_li.html",
include_str!("templates/partials/event_li.html"),
),
(
"macros/event.html",
include_str!("templates/macros/event.html"),
),
])
.unwrap();
tera
}