jmserver/src/main.rs

46 lines
1.3 KiB
Rust
Raw Normal View History

2021-12-18 20:04:34 +01:00
use std::path::PathBuf;
2021-12-17 23:50:03 +01:00
use config::Config;
2021-07-19 22:29:03 +02:00
use sqlx::MySqlPool;
2021-12-17 23:50:03 +01:00
use structopt::StructOpt;
use axum::{Router, body::Body, http::{HeaderValue, Request, header}};
use tower_http::{add_extension::AddExtensionLayer, set_header::SetResponseHeaderLayer};
2021-07-19 22:29:03 +02:00
mod v1;
2021-12-18 20:04:34 +01:00
mod cdn;
2021-12-17 23:50:03 +01:00
mod config;
2021-12-18 20:04:34 +01:00
mod ipfs;
2021-12-17 23:50:03 +01:00
#[derive(StructOpt)]
struct Opt {
#[structopt(
short,
long,
help = "config file to use",
default_value = "./config.toml"
)]
config: PathBuf,
}
2021-07-19 22:29:03 +02:00
2021-08-26 17:45:38 +02:00
#[tokio::main]
async fn main() {
2021-07-19 22:29:03 +02:00
2021-12-17 23:50:03 +01:00
let opt = Opt::from_args();
let config = std::fs::read(&opt.config).expect("Config file reading error");
let config = toml::from_slice::<Config>(&config).expect("Config file parsing error");
let db_pool = MySqlPool::new(&config.database).await.expect("Database connection error");
2021-07-19 22:29:03 +02:00
2021-08-26 17:45:38 +02:00
let app = Router::new()
2021-12-18 20:04:34 +01:00
.nest("/api/v1", v1::routes())
.nest("/cdn", cdn::routes())
2021-12-17 23:50:03 +01:00
.layer(AddExtensionLayer::new(db_pool))
.layer(AddExtensionLayer::new(config.vars()))
.layer(SetResponseHeaderLayer::<_, Request<Body>>::if_not_present(header::ACCESS_CONTROL_ALLOW_ORIGIN, HeaderValue::from_static("*")));
2021-07-19 22:29:03 +02:00
2021-12-17 23:50:03 +01:00
axum::Server::bind(&config.addr)
2021-08-26 17:45:38 +02:00
.serve(app.into_make_service())
.await
.expect("Something went wrong :(");
2021-07-19 22:29:03 +02:00
}
2021-12-17 23:50:03 +01:00