cibus-backend/cibus_bin/src/main.rs
kjuulh 39b68e0dde
All checks were successful
continuous-integration/drone/push Build is passing
Removed unused dependencies
2022-08-15 22:57:56 +02:00

87 lines
2.2 KiB
Rust

use std::env::{self, current_dir};
use askama::Template;
use axum::{
http::StatusCode,
response::{Html, IntoResponse, Response},
routing::{get},
Router,
};
use axum_extra::routing::SpaRouter;
use sqlx::PgPool;
use tower_http::{trace::TraceLayer};
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
#[tokio::main]
async fn main() -> anyhow::Result<()> {
// Environment
tracing::info!("Loading dotenv");
dotenv::dotenv()?;
// Logging
tracing_subscriber::registry()
.with(tracing_subscriber::EnvFilter::new(
std::env::var("RUST_LOG").unwrap_or_else(|_| {
"cibus_bin=debug,tower_http=debug,axum_extra=debug,hyper=info,mio=info".into()
}),
))
.with(tracing_subscriber::fmt::layer())
.init();
// Database
tracing::info!("Creating pool");
let db_url = env::var("DATABASE_URL")?;
let pool = PgPool::connect(&db_url).await?;
// Database Migrate
tracing::info!("Migrating db");
sqlx::migrate!("db/migrations").run(&pool).await?;
tracing::info!("current path: {}", current_dir()?.to_string_lossy());
// Webserver
tracing::info!("Building router");
let app = Router::new()
.route("/", get(index_handler))
.merge(SpaRouter::new("/assets", "assets"))
.layer(TraceLayer::new_for_http());
tracing::info!("Starting webserver");
axum::Server::bind(&"0.0.0.0:3000".parse().unwrap())
.serve(app.into_make_service())
.await
.unwrap();
Ok(())
}
struct HtmlTemplate<T>(T);
impl<T> IntoResponse for HtmlTemplate<T>
where
T: Template,
{
fn into_response(self) -> Response {
match self.0.render() {
Ok(html) => Html(html).into_response(),
Err(err) => (
StatusCode::INTERNAL_SERVER_ERROR,
format!("Failed to render template. Error: {}", err),
)
.into_response(),
}
}
}
#[derive(Template)]
#[template(path = "index.html")]
struct IndexTemplate {
name: String,
}
async fn index_handler() -> impl IntoResponse {
HtmlTemplate(IndexTemplate {
name: "Cibus".into(),
})
}