libmedium/src/main.rs

98 lines
2.9 KiB
Rust
Raw Normal View History

2021-10-31 15:12:12 +05:30
/*
* Copyright (C) 2021 Aravinth Manivannan <realaravinth@batsense.net>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
use std::env;
use actix_web::{
error::InternalError, http::StatusCode, middleware as actix_middleware, web::JsonConfig, App,
HttpServer,
};
use lazy_static::lazy_static;
use log::info;
2021-10-31 22:26:22 +05:30
mod data;
2021-10-31 15:12:12 +05:30
mod meta;
mod proxy;
mod routes;
mod settings;
2021-10-31 22:26:22 +05:30
pub use data::AppData;
pub use data::Data;
2021-10-31 15:12:12 +05:30
pub use routes::ROUTES as V1_API_ROUTES;
pub use settings::Settings;
lazy_static! {
pub static ref SETTINGS: Settings = Settings::new().unwrap();
}
pub const CACHE_AGE: u32 = 604800;
pub const GIT_COMMIT_HASH: &str = env!("GIT_HASH");
pub const VERSION: &str = env!("CARGO_PKG_VERSION");
pub const PKG_NAME: &str = env!("CARGO_PKG_NAME");
pub const PKG_DESCRIPTION: &str = env!("CARGO_PKG_DESCRIPTION");
pub const PKG_HOMEPAGE: &str = env!("CARGO_PKG_HOMEPAGE");
#[cfg(not(tarpaulin_include))]
#[actix_web::main]
async fn main() -> std::io::Result<()> {
env::set_var("RUST_LOG", "info");
pretty_env_logger::init();
info!(
"{}: {}.\nFor more information, see: {}\nBuild info:\nVersion: {} commit: {}",
PKG_NAME, PKG_DESCRIPTION, PKG_HOMEPAGE, VERSION, GIT_COMMIT_HASH
);
println!("Starting server on: http://{}", SETTINGS.server.get_ip());
2021-10-31 22:26:22 +05:30
let data = Data::new();
2021-10-31 15:12:12 +05:30
HttpServer::new(move || {
App::new()
.wrap(actix_middleware::Logger::default())
.wrap(actix_middleware::Compress::default())
.app_data(get_json_err())
.wrap(
actix_middleware::DefaultHeaders::new()
.header("Permissions-Policy", "interest-cohort=()"),
)
.wrap(actix_middleware::NormalizePath::new(
actix_middleware::TrailingSlash::Trim,
))
2021-10-31 22:26:22 +05:30
.app_data(data.clone())
2021-10-31 15:12:12 +05:30
.configure(services)
})
.workers(SETTINGS.server.workers.unwrap_or_else(num_cpus::get))
.bind(SETTINGS.server.get_ip())
.unwrap()
.run()
.await
}
#[cfg(not(tarpaulin_include))]
pub fn get_json_err() -> JsonConfig {
JsonConfig::default().error_handler(|err, _| {
//debug!("JSON deserialization error: {:?}", &err);
InternalError::new(err, StatusCode::BAD_REQUEST).into()
})
}
pub fn services(cfg: &mut actix_web::web::ServiceConfig) {
routes::services(cfg);
}