2021-10-04 21:21:10 +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 std::sync::Arc;
|
|
|
|
|
|
|
|
use actix_identity::{CookieIdentityPolicy, IdentityService};
|
|
|
|
use actix_web::{
|
|
|
|
error::InternalError, http::StatusCode, middleware as actix_middleware,
|
|
|
|
web::JsonConfig, App, HttpServer,
|
|
|
|
};
|
|
|
|
use lazy_static::lazy_static;
|
|
|
|
use log::info;
|
|
|
|
|
|
|
|
mod api;
|
|
|
|
mod data;
|
|
|
|
mod errors;
|
|
|
|
mod middleware;
|
2021-10-13 14:11:39 +05:30
|
|
|
mod pages;
|
2021-10-04 21:21:10 +05:30
|
|
|
mod settings;
|
2021-10-11 10:32:19 +05:30
|
|
|
mod static_assets;
|
2021-10-11 10:25:45 +05:30
|
|
|
#[cfg(test)]
|
|
|
|
#[macro_use]
|
|
|
|
mod tests;
|
2021-10-04 21:21:10 +05:30
|
|
|
|
|
|
|
pub use crate::data::Data;
|
|
|
|
pub use api::v1::ROUTES as V1_API_ROUTES;
|
|
|
|
pub use middleware::auth::CheckLogin;
|
2021-10-13 14:11:39 +05:30
|
|
|
pub use pages::routes::ROUTES as PAGES;
|
2021-10-04 21:21:10 +05:30
|
|
|
pub use settings::Settings;
|
2021-10-11 10:32:19 +05:30
|
|
|
pub use static_assets::static_files::assets;
|
|
|
|
|
|
|
|
use static_assets::FileMap;
|
2021-10-04 21:21:10 +05:30
|
|
|
|
|
|
|
lazy_static! {
|
|
|
|
pub static ref SETTINGS: Settings = Settings::new().unwrap();
|
2021-10-11 10:32:19 +05:30
|
|
|
pub static ref FILES: FileMap = FileMap::new();
|
2021-10-13 14:11:39 +05:30
|
|
|
|
|
|
|
pub static ref CSS: &'static str =
|
|
|
|
FILES.get("./static/cache/bundle/css/main.css").unwrap();
|
2021-10-12 14:02:34 +05:30
|
|
|
pub static ref JS: &'static str =
|
|
|
|
FILES.get("./static/cache/bundle/bundle.js").unwrap();
|
|
|
|
|
2021-10-12 14:54:51 +05:30
|
|
|
pub static ref GLUE: &'static str =
|
|
|
|
FILES.get("./static/cache/bundle/glue.js").unwrap();
|
|
|
|
|
2021-10-04 21:21:10 +05:30
|
|
|
/// points to source files matching build commit
|
|
|
|
pub static ref SOURCE_FILES_OF_INSTANCE: String = {
|
|
|
|
let mut url = SETTINGS.source_code.clone();
|
|
|
|
if !url.ends_with('/') {
|
|
|
|
url.push('/');
|
|
|
|
}
|
|
|
|
let mut base = url::Url::parse(&url).unwrap();
|
|
|
|
base = base.join("tree/").unwrap();
|
|
|
|
base = base.join(GIT_COMMIT_HASH).unwrap();
|
|
|
|
base.into()
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
pub const CACHE_AGE: u32 = 604800;
|
|
|
|
|
|
|
|
pub const COMPILED_DATE: &str = env!("COMPILED_DATE");
|
|
|
|
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");
|
|
|
|
|
|
|
|
pub type AppData = actix_web::web::Data<Arc<crate::data::Data>>;
|
|
|
|
|
|
|
|
#[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
|
|
|
|
);
|
|
|
|
|
|
|
|
let data = Data::new().await;
|
|
|
|
sqlx::migrate!("./migrations/").run(&data.db).await.unwrap();
|
|
|
|
let data = actix_web::web::Data::new(data);
|
|
|
|
|
|
|
|
println!("Starting server on: http://{}", SETTINGS.server.get_ip());
|
|
|
|
|
|
|
|
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(get_identity_service())
|
2021-10-11 09:56:15 +05:30
|
|
|
.wrap(get_survey_identity_service())
|
2021-10-04 21:21:10 +05:30
|
|
|
.wrap(actix_middleware::NormalizePath::new(
|
|
|
|
actix_middleware::TrailingSlash::Trim,
|
|
|
|
))
|
|
|
|
.configure(services)
|
|
|
|
.app_data(data.clone())
|
|
|
|
})
|
|
|
|
.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()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(tarpaulin_include))]
|
2021-10-11 09:56:15 +05:30
|
|
|
pub fn get_survey_identity_service() -> IdentityService<CookieIdentityPolicy> {
|
2021-10-04 21:21:10 +05:30
|
|
|
let cookie_secret = &SETTINGS.server.cookie_secret;
|
|
|
|
IdentityService::new(
|
|
|
|
CookieIdentityPolicy::new(cookie_secret.as_bytes())
|
2021-10-06 19:00:53 +05:30
|
|
|
.name("survey-id")
|
2021-10-11 09:56:15 +05:30
|
|
|
.path(V1_API_ROUTES.benches.scope)
|
|
|
|
.max_age_secs(30 * 60)
|
|
|
|
.domain(&SETTINGS.server.domain)
|
|
|
|
.secure(false),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(tarpaulin_include))]
|
|
|
|
pub fn get_identity_service() -> IdentityService<CookieIdentityPolicy> {
|
|
|
|
let cookie_secret = &SETTINGS.server.cookie_secret;
|
|
|
|
IdentityService::new(
|
|
|
|
CookieIdentityPolicy::new(cookie_secret.as_bytes())
|
2021-10-11 13:41:36 +05:30
|
|
|
.path("/api/v1/admin")
|
2021-10-11 09:56:15 +05:30
|
|
|
.name("survey-auth")
|
|
|
|
.max_age_secs(60 * 24)
|
2021-10-04 21:21:10 +05:30
|
|
|
.domain(&SETTINGS.server.domain)
|
|
|
|
.secure(false),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn services(cfg: &mut actix_web::web::ServiceConfig) {
|
2021-10-13 14:11:39 +05:30
|
|
|
pages::services(cfg);
|
2021-10-04 21:21:10 +05:30
|
|
|
api::v1::services(cfg);
|
2021-10-11 10:32:19 +05:30
|
|
|
static_assets::services(cfg);
|
2021-10-04 21:21:10 +05:30
|
|
|
}
|