2022-08-15 22:55:04 +02:00
|
|
|
use std::env::{self, current_dir};
|
2022-08-09 01:33:34 +02:00
|
|
|
|
2022-08-15 22:55:04 +02:00
|
|
|
use askama::Template;
|
2022-08-09 01:33:34 +02:00
|
|
|
use axum::{
|
2022-08-15 22:55:04 +02:00
|
|
|
http::StatusCode,
|
|
|
|
response::{Html, IntoResponse, Response},
|
2022-08-15 22:57:56 +02:00
|
|
|
routing::{get},
|
2022-08-15 22:55:04 +02:00
|
|
|
Router,
|
2022-08-09 01:33:34 +02:00
|
|
|
};
|
2022-08-15 22:55:04 +02:00
|
|
|
use axum_extra::routing::SpaRouter;
|
2022-08-09 01:33:34 +02:00
|
|
|
use sqlx::PgPool;
|
2022-08-15 22:57:56 +02:00
|
|
|
use tower_http::{trace::TraceLayer};
|
2022-08-15 22:55:04 +02:00
|
|
|
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
|
2022-08-09 01:33:34 +02:00
|
|
|
|
|
|
|
#[tokio::main]
|
|
|
|
async fn main() -> anyhow::Result<()> {
|
|
|
|
// Environment
|
2022-08-15 22:55:04 +02:00
|
|
|
tracing::info!("Loading dotenv");
|
2022-08-09 01:33:34 +02:00
|
|
|
dotenv::dotenv()?;
|
|
|
|
|
2022-08-15 22:55:04 +02:00
|
|
|
// 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();
|
|
|
|
|
2022-08-09 01:33:34 +02:00
|
|
|
// Database
|
2022-08-15 22:55:04 +02:00
|
|
|
tracing::info!("Creating pool");
|
|
|
|
let db_url = env::var("DATABASE_URL")?;
|
|
|
|
let pool = PgPool::connect(&db_url).await?;
|
2022-08-09 01:33:34 +02:00
|
|
|
|
|
|
|
// Database Migrate
|
2022-08-15 22:55:04 +02:00
|
|
|
tracing::info!("Migrating db");
|
2022-08-09 01:33:34 +02:00
|
|
|
sqlx::migrate!("db/migrations").run(&pool).await?;
|
|
|
|
|
2022-08-15 22:55:04 +02:00
|
|
|
tracing::info!("current path: {}", current_dir()?.to_string_lossy());
|
2022-08-09 01:33:34 +02:00
|
|
|
|
2022-08-15 22:55:04 +02:00
|
|
|
// Webserver
|
|
|
|
tracing::info!("Building router");
|
2022-08-09 01:33:34 +02:00
|
|
|
let app = Router::new()
|
2022-08-15 22:55:04 +02:00
|
|
|
.route("/", get(index_handler))
|
|
|
|
.merge(SpaRouter::new("/assets", "assets"))
|
|
|
|
.layer(TraceLayer::new_for_http());
|
2022-08-09 01:33:34 +02:00
|
|
|
|
2022-08-15 22:55:04 +02:00
|
|
|
tracing::info!("Starting webserver");
|
2022-08-09 01:33:34 +02:00
|
|
|
axum::Server::bind(&"0.0.0.0:3000".parse().unwrap())
|
|
|
|
.serve(app.into_make_service())
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2022-08-15 22:55:04 +02:00
|
|
|
|
|
|
|
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(),
|
|
|
|
})
|
|
|
|
}
|