jmserver/src/main.rs

55 lines
1.2 KiB
Rust
Raw Normal View History

2021-12-29 18:33:31 +01:00
use axum::{
body::Body,
http::{header, HeaderValue, Request},
Router,
};
2021-12-17 23:50:03 +01:00
use config::Config;
2022-01-04 20:06:57 +01:00
use error::JMError;
2021-07-19 22:29:03 +02:00
use sqlx::MySqlPool;
2021-12-29 18:33:31 +01:00
use std::path::PathBuf;
2021-12-17 23:50:03 +01:00
use structopt::StructOpt;
use tower_http::{add_extension::AddExtensionLayer, set_header::SetResponseHeaderLayer};
2021-07-19 22:29:03 +02:00
2021-12-18 20:04:34 +01:00
mod cdn;
2021-12-17 23:50:03 +01:00
mod config;
2022-01-05 23:46:19 +01:00
mod error;
2021-12-18 20:04:34 +01:00
mod ipfs;
2022-01-08 19:01:19 +01:00
mod lib;
2021-12-29 18:33:31 +01:00
mod v1;
2021-12-17 23:50:03 +01:00
#[derive(StructOpt)]
struct Opt {
#[structopt(
2021-12-29 18:33:31 +01:00
short,
long,
help = "config file to use",
default_value = "./config.toml"
2021-12-17 23:50:03 +01:00
)]
config: PathBuf,
}
2021-07-19 22:29:03 +02:00
2021-08-26 17:45:38 +02:00
#[tokio::main]
2022-01-04 20:06:57 +01:00
async fn main() -> Result<(), JMError> {
2021-12-17 23:50:03 +01:00
let opt = Opt::from_args();
2022-01-04 20:06:57 +01:00
let config = std::fs::read(&opt.config)?;
let config = toml::from_slice::<Config>(&config)?;
2021-12-17 23:50:03 +01:00
2022-01-05 23:46:19 +01:00
let db_pool = MySqlPool::new(&config.database).await?;
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()))
2021-12-29 18:33:31 +01:00
.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())
2022-01-04 20:06:57 +01:00
.await?;
Ok(())
2021-07-19 22:29:03 +02:00
}