77 lines
No EOL
2.1 KiB
Rust
77 lines
No EOL
2.1 KiB
Rust
use std::{net::SocketAddr, env};
|
|
use std::str::FromStr;
|
|
|
|
use axum::{Router, routing::get};
|
|
use log::info;
|
|
use sea_orm::{Database, DatabaseConnection, ConnectOptions};
|
|
|
|
mod routes;
|
|
mod entities;
|
|
mod block_types;
|
|
|
|
#[tokio::main]
|
|
async fn main() {
|
|
if env::var("RUST_LOG").is_err() {
|
|
env::set_var("RUST_LOG", "info");
|
|
}
|
|
tracing_subscriber::fmt::init();
|
|
|
|
dotenvy::dotenv().ok();
|
|
|
|
info!("Starting up..");
|
|
|
|
let db_url = env::var("DATABASE_URL").expect("DATABASE_URL is not set in the enviroment! HALTING");
|
|
let host = env::var("HOST").unwrap_or("0.0.0.0".to_string());
|
|
let port = env::var("PORT").unwrap_or("3621".to_string());
|
|
|
|
let server_url = format!("{host}:{port}");
|
|
|
|
info!("Connecting to the database...");
|
|
|
|
let mut db_opt = ConnectOptions::new(db_url);
|
|
db_opt.sqlx_logging_level(log::LevelFilter::Debug);
|
|
|
|
let db_conn = Database::connect(db_opt)
|
|
.await
|
|
.expect("Failed to connect to the database! HALTING");
|
|
//Migrator::up(&db_conn, None).await.expect("Failed to run migrations! HALTING");
|
|
|
|
info!("Connected to database!");
|
|
|
|
let app_state = AppState { db_conn };
|
|
|
|
let app = Router::new()
|
|
.route("/", get(routes::root))
|
|
.route("/post/:slug", get(not_implemented))
|
|
.route("/:slug", get(routes::page_resolver::resolver))
|
|
.with_state(app_state);
|
|
|
|
info!("Attempting to bind to address...");
|
|
|
|
let listen_addr = SocketAddr::from_str(&server_url).expect("Failed to parse host and port! HALTING");
|
|
|
|
let bind = axum::Server::try_bind(&listen_addr);
|
|
|
|
match bind {
|
|
Ok(bind) => {
|
|
info!("Binded to address successfully!");
|
|
info!("Listening for connections...");
|
|
match bind.serve(app.into_make_service()).await {
|
|
Err(e) => panic!("Failed to start server! HALTING {}", e),
|
|
_ => (),
|
|
}
|
|
},
|
|
Err(e) => {
|
|
panic!("Failed to bind! HALTING {}", e)
|
|
},
|
|
}
|
|
}
|
|
|
|
#[derive(Clone)]
|
|
struct AppState {
|
|
db_conn: DatabaseConnection,
|
|
}
|
|
|
|
async fn not_implemented() -> &'static str {
|
|
"Not Implemented"
|
|
} |