feat: bootstrap actix-web

This commit is contained in:
Aravinth Manivannan 2023-09-05 19:21:30 +05:30
parent af95c79b3b
commit d1b41ede99
Signed by: realaravinth
GPG key ID: AD9F0F08E855ED88
8 changed files with 3332 additions and 0 deletions

1
.gitignore vendored
View file

@ -1 +1,2 @@
/target
.env

2848
Cargo.lock generated Normal file

File diff suppressed because it is too large Load diff

42
Cargo.toml Normal file
View file

@ -0,0 +1,42 @@
[package]
name = "ftest"
version = "0.1.0"
edition = "2021"
build = "build.rs"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
reqwest = { version = "0.11.20", features = ["json", "gzip"] }
actix-web = "4"
actix-web-prom = "0.6.0"
futures-util = { version = "0.3.17", default-features = false, features = ["std"] }
lazy_static = "1.4.0"
log = "0.4.17"
pretty_env_logger = "0.4.0"
serde = { version = "1", features=["derive"]}
actix-web-codegen-const-routes = { version = "0.1.0", tag = "0.1.0", git = "https://github.com/realaravinth/actix-web-codegen-const-routes" }
derive_builder = "0.11.2"
config = "0.13"
derive_more = "0.99.17"
url = { version = "2.2.2", features = ["serde"] }
serde_json = { version ="1", features = ["raw_value"]}
actix-web-httpauth = "0.8.0"
mime_guess = "2.0.4"
rust-embed = "6.4.2"
sqlx = { version = "0.6.1", features = [ "runtime-actix-rustls", "postgres", "time", "offline"] }
tracing = { version = "0.1.37", features = ["log"] }
tracing-actix-web = "0.7.6"
tracing-subscriber = "0.3.17"
num_cpus = "1.16.0"
uuid = { version = "1.4.1", features = ["v4", "serde"] }
rand = "0.8.5"
semver = { version = "1.0.18", features = ["serde"] }
[build-dependencies]
serde_json = "1"
sqlx = { version = "0.6.1", features = [ "runtime-actix-rustls", "postgres", "time", "offline"] }
[dev-dependencies]
actix-rt = "2.7.0"
base64 = "0.13.0"

150
Makefile Normal file
View file

@ -0,0 +1,150 @@
# SPDX-FileCopyrightText: 2023 Aravinth Manivannan <realaravinth@batsense.net>
#
# SPDX-License-Identifier: AGPL-3.0-or-later
#BUNDLE = static/cache/bundle
#OPENAPI = docs/openapi
#CLEAN_UP = $(BUNDLE) src/cache_buster_data.json assets
define deploy_dependencies ## deploy dependencies
@-docker create --name ${db} \
-e POSTGRES_PASSWORD=password \
-p 5432:5432 \
postgres
docker start ${db}
endef
define run_migrations ## run database migrations
cd db/migrations/ && cargo run
endef
define run_dev_migrations ## run database migrations
sqlx migrate run
endef
#define frontend_env ## install frontend deps
# yarn install
# cd docs/openapi && yarn install
#endef
#
#define cache_bust ## run cache_busting program
# cd utils/cache-bust && cargo run
#endef
#
#
#define test_frontend ## run frontend tests
# yarn test
## cd $(OPENAPI)&& yarn test
#endef
define test_core
cargo test --no-fail-fast
endef
default: ## Build app in debug mode
cargo build
check: ## Check for syntax errors on all workspaces
cargo check --workspace --tests --all-features
cd db/migrations && cargo check --tests --all-features
#cache-bust: ## Run cache buster on static assets
# $(call cache_bust)
clean: ## Delete build artifacts
@cargo clean
#@yarn cache clean
#@-rm $(CLEAN_UP)
doc: ## Generate documentation
#yarn doc
cargo doc --no-deps --workspace --all-features
docker: ## Build Docker image
docker build -t forgeflux/ftest:master -t forgeflux/ftest:latest .
docker-publish: docker ## Build and publish Docker image
docker push forgeflux/ftest:master
docker push forgeflux/ftest:latest
env: ## Setup development environtment
cargo fetch
$(call frontend_env)
env.db: ## Deploy dependencies
$(call deploy_dependencies)
sleep 5
$(call run_migrations)
env.db.recreate: ## Deploy dependencies from scratch
@-docker rm -f ${db}
$(call deploy_dependencies)
sleep 5
$(call run_migrations)
#frontend-env: ## Install frontend deps
# $(call frontend_env)
#
#frontend: ## Build frontend
# $(call frontend_env)
# cd $(OPENAPI) && yarn build
# yarn install
# @-rm -rf $(BUNDLE)
# @-mkdir $(BUNDLE)
# yarn build
# @yarn run sass -s \
# compressed templates/main.scss \
# ./static/cache/bundle/css/main.css
# @yarn run sass -s \
# compressed templates/mobile.scss \
# ./static/cache/bundle/css/mobile.css
# @yarn run sass -s \
# compressed templates/widget/main.scss \
# ./static/cache/bundle/css/widget.css
# @./scripts/librejs.sh
# @./scripts/cachebust.sh
lint: ## Lint codebase
cargo fmt -v --all -- --emit files
cargo clippy --workspace --tests --all-features
#yarn lint
#cd $(OPENAPI)&& yarn test
migrate: ## Run database migrations
$(call run_migrations)
migrate.dev: ## Run database migrations during development
$(call run_dev_migrations)
release: ## Build app with release optimizations
cargo build --release
run: ## Run app in debug mode
cargo run
db.sqlx.offline: ## prepare sqlx offline data
cargo sqlx prepare \
--database-url=${DATABASE_URL} -- \
--all-features
test: ## Run all available tests
$(call test_core)
# ./scripts/tests.sh
test.cov.html: migrate ## Generate code coverage report in HTML format
cargo tarpaulin -t 1200 --out Html
test.cov.xml: migrate ## Generate code coverage report in XML format
cargo tarpaulin -t 1200 --out Xml
test.core: ## Run all core tests
$(call test_core)
#test.frontend: ## Run frontend tests
# $(call test_frontend)
help: ## Prints help for targets with comments
@cat $(MAKEFILE_LIST) | grep -E '^[a-zA-Z_-].+:.*?## .*$$' | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'

46
src/ctx.rs Normal file
View file

@ -0,0 +1,46 @@
/*
* Copyright (C) 2022 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::sync::Arc;
use std::thread;
use crate::db::*;
use crate::settings::Settings;
use reqwest::Client;
use tracing::info;
pub type ArcCtx = Arc<Ctx>;
#[derive(Clone)]
pub struct Ctx {
pub settings: Settings,
pub db: Database,
client: Client,
}
impl Ctx {
pub async fn new(settings: Settings) -> Arc<Self> {
let client = Client::default();
let db = get_db(&settings).await;
#[cfg(not(debug_assertions))]
init.join();
Arc::new(Self {
settings,
client,
db,
})
}
}

94
src/errors.rs Normal file
View file

@ -0,0 +1,94 @@
/*
* Copyright (C) 2022 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::convert::From;
use actix_web::http;
use actix_web::http::StatusCode;
use actix_web::HttpResponse;
use actix_web::HttpResponseBuilder;
use actix_web::ResponseError;
use derive_more::{Display, Error};
use serde::{Deserialize, Serialize};
use url::ParseError;
#[derive(Debug, Display, PartialEq, Eq, Error)]
#[cfg(not(tarpaulin_include))]
#[allow(dead_code)]
pub enum ServiceError {
#[display(fmt = "unauthorized")]
Unauthorized,
#[display(fmt = "internal server error")]
InternalServerError,
#[display(
fmt = "This server is is closed for registration. Contact admin if this is unexpecter"
)]
ClosedForRegistration,
#[display(fmt = "The value you entered for URL is not a URL")] //405j
NotAUrl,
#[display(fmt = "Wrong password")]
WrongPassword,
}
#[derive(Serialize, Deserialize)]
#[cfg(not(tarpaulin_include))]
pub struct ErrorToResponse {
pub error: String,
}
#[cfg(not(tarpaulin_include))]
impl ResponseError for ServiceError {
#[cfg(not(tarpaulin_include))]
fn error_response(&self) -> HttpResponse {
HttpResponseBuilder::new(self.status_code())
.append_header((
http::header::CONTENT_TYPE,
"application/json; charset=UTF-8",
))
.body(
serde_json::to_string(&ErrorToResponse {
error: self.to_string(),
})
.unwrap(),
)
}
#[cfg(not(tarpaulin_include))]
fn status_code(&self) -> StatusCode {
match self {
ServiceError::ClosedForRegistration => StatusCode::FORBIDDEN,
ServiceError::InternalServerError => StatusCode::INTERNAL_SERVER_ERROR,
ServiceError::NotAUrl => StatusCode::BAD_REQUEST,
ServiceError::WrongPassword => StatusCode::UNAUTHORIZED,
ServiceError::Unauthorized => StatusCode::UNAUTHORIZED,
}
}
}
impl From<ParseError> for ServiceError {
#[cfg(not(tarpaulin_include))]
fn from(_: ParseError) -> ServiceError {
ServiceError::NotAUrl
}
}
#[cfg(not(tarpaulin_include))]
pub type ServiceResult<V> = std::result::Result<V, ServiceError>;

122
src/main.rs Normal file
View file

@ -0,0 +1,122 @@
/*
* Copyright (C) 2022 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::Data as WebData,
web::JsonConfig, App, HttpServer,
};
//use clap::{Parser, Subcommand};
//use static_assets::FileMap;
use tracing::info;
use tracing_actix_web::TracingLogger;
//
//pub use crate::api::v1::ROUTES as V1_API_ROUTES;
use ctx::Ctx;
pub use settings::Settings;
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");
pub type AppCtx = WebData<ctx::ArcCtx>;
mod ctx;
mod db;
mod docker;
mod errors;
mod compliance;
mod git;
mod settings;
mod utils;
//lazy_static::lazy_static! {
// pub static ref FILES: FileMap = FileMap::new();
//}
//
//#[derive(Parser)]
//#[clap(author, version, about, long_about = None)]
//struct Cli {
// #[clap(subcommand)]
// command: Commands,
//}
#[actix_web::main]
#[cfg(not(tarpaulin_include))]
async fn main() -> std::io::Result<()> {
if env::var("RUST_LOG").is_err() {
env::set_var("RUST_LOG", "info");
}
pretty_env_logger::init();
// let cli = Cli::parse();
info!(
"{}: {}.\nFor more information, see: {}\nBuild info:\nVersion: {} commit: {}",
PKG_NAME, PKG_DESCRIPTION, PKG_HOMEPAGE, VERSION, GIT_COMMIT_HASH
);
let settings = Settings::new().unwrap();
Ok(())
}
async fn serve(settings: Settings, ctx: AppCtx) -> std::io::Result<()> {
let ip = settings.server.get_ip();
let workers = settings.server.workers.unwrap_or_else(num_cpus::get);
info!("Starting server on: http://{}", ip);
HttpServer::new(move || {
App::new()
.wrap(TracingLogger::default())
.wrap(actix_middleware::Compress::default())
.app_data(ctx.clone())
.app_data(get_json_err())
.wrap(
actix_middleware::DefaultHeaders::new()
.add(("Permissions-Policy", "interest-cohort=()")),
)
.wrap(actix_middleware::NormalizePath::new(
actix_middleware::TrailingSlash::Trim,
))
.configure(services)
})
.workers(workers)
.bind(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) {
// crate::api::v1::services(cfg);
// crate::pages::services(cfg);
// crate::static_assets::services(cfg);
// crate::serve::services(cfg);
}

29
src/utils.rs Normal file
View file

@ -0,0 +1,29 @@
/*
* Copyright (C) 2022 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/>.
*/
/// Get random string of specific length
pub(crate) fn get_random(len: usize) -> String {
use rand::{distributions::Alphanumeric, rngs::ThreadRng, thread_rng, Rng};
use std::iter;
let mut rng: ThreadRng = thread_rng();
iter::repeat(())
.map(|()| rng.sample(Alphanumeric))
.map(char::from)
.take(len)
.collect::<String>()
}