feat: bootstrap actix web

This commit is contained in:
Aravinth Manivannan 2024-05-13 21:30:15 +05:30
parent 5d636892a2
commit 5eca33e9f9
Signed by: realaravinth
GPG key ID: F8F50389936984FF
5 changed files with 3424 additions and 3 deletions

1
.gitignore vendored
View file

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

3237
Cargo.lock generated

File diff suppressed because it is too large Load diff

View file

@ -3,4 +3,27 @@ name = "vanigam"
version = "0.1.0"
edition = "2021"
[workspace]
exclude = ["utils/db-migrations"] #, "utils/cache-bust"]
memebers = ["."]
[dependencies]
actix-identity = "0.7.1"
actix-rt = "2.9.0"
actix-session = { version = "0.9.0", features = ["cookie-session"] }
actix-web = "4.5.1"
async-trait = "0.1.80"
config = "0.14.0"
derive_more = "0.99.17"
log = "0.4.21"
mockall = { version = "0.12.1", features = ["nightly"] }
pretty_env_logger = "0.5.0"
rand = "0.8.5"
rust-embed = { version = "8.4.0", features = ["include-exclude"] }
serde = { version = "1.0.201", features = ["derive"] }
sqlx = { version = "0.7.4", features = ["runtime-tokio-rustls", "postgres", "time"] }
tera = "1.19.1"
time = { version = "0.3.36", features = ["serde"] }
tracing = { version = "0.1.40", features = ["log"] }
tracing-actix-web = "0.7.10"
url = { version = "2.5.0", features = ["serde"] }

107
Makefile Normal file
View file

@ -0,0 +1,107 @@
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 utils/db-migrations/ && unset DATABASE_URL && cargo build
cd utils/db-migrations/ && cargo run
endef
#define cache_bust ## run cache_busting program
# npm run sass
# cd utils/cache-bust && cargo run
#endef
#
default: ## Debug build
# $(call cache_bust)
cargo run -- serve
#
#cache-bust: ## Run cache buster on static assets
# $(call cache_bust)
check: ## Check for syntax errors on all workspaces
cargo check --workspace --tests --all-features
#cd utils/cache-bust && cargo check --tests --all-features
clean: ## Clean all build artifacts and dependencies
@cargo clean
db.migrate: ## run migrations
$(call run_migrations)
db.sqlx.offline: ## prepare sqlx offline data
cargo sqlx prepare \
--database-url=${DATABASE_URL} -- \
--all-features
#dev-env: ## Download development dependencies
# npm install
# cargo fetch
doc: ## Prepare documentation
cargo doc --no-deps --workspace --all-features
docker.build: ## Build docker images
docker build \
-t libre-solutions/vanigam:master \
-t libre-solutions/vanigam:latest \
-t libre-solutions/vanigam:0.1.0 .
docker.publish: docker.build ## Build and publish docker images
docker push libre-solutions/vanigam:master
docker push libre-solutions/vanigam:latest
docker push libre-solutions/vanigam:0.1.0
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)
lint: ## Lint codebase
cargo fmt -v --all -- --emit files
cargo clippy --workspace --tests --all-features
#migrate: ## run migrations
# $(call cache_bust)
# unset DATABASE_URL && cargo build
# DATABASE_URL=${DATABASE_URL} cargo run -- migrate
release: ## Release build
#$(call cache_bust)
cargo build --release
run: default ## Run debug build
cargo run -- serve
#sqlx-offline-data: ## prepare sqlx offline data
# cargo sqlx prepare \
# --database-url=${DATABASE_URL} -- \
# --all-features
test: ## Run tests
#$(call cache_bust)
cargo test --all-features --no-fail-fast
#test.coverage.xml: ## Generate cobertura.xml test coverage
# #$(call cache_bust)
# cargo tarpaulin -t 1200 --out Xml
#
#test.coverage.html: ## Generate HTML code coverage
# $(call cache_bust)
# cargo tarpaulin -t 1200 --out Html
#
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}'

View file

@ -2,11 +2,64 @@
//
// SPDX-License-Identifier: AGPL-3.0-or-later
use std::env;
use actix_identity::IdentityMiddleware;
use actix_session::{storage::CookieSessionStore, SessionMiddleware};
use actix_web::{cookie::Key, middleware, App, HttpServer};
use db::migrate::RunMigrations;
mod billing;
mod identity;
mod inventory;
mod ordering;
mod identity;
mod settings;
mod db;
mod utils;
fn main() {
println!("foo");
#[actix_web::main]
async fn main() {
let settings = settings::Settings::new().unwrap();
if env::var("RUST_LOG").is_err() {
env::set_var("RUST_LOG", &settings.log);
}
{
// Settings::new() outputs logs, but since we are only setting up logger _after_ Settings is
// initialized, this dummy reinitialization will output logs.
settings::Settings::new().unwrap();
}
pretty_env_logger::init();
let db = db::sqlx_postgres::Postgres::init(&settings.database.url).await;
db.migrate().await;
let secret_key = Key::from(settings.server.cookie_secret.as_bytes());
let socket_addr = settings.server.get_ip();
log::info!("Starting server at: {} {}", socket_addr, settings.server.domain);
HttpServer::new(move || {
App::new()
.wrap(IdentityMiddleware::default())
.wrap(tracing_actix_web::TracingLogger::default())
.wrap(middleware::Compress::default())
.app_data(actix_web::web::Data::new(settings.clone()))
.wrap(SessionMiddleware::new(
CookieSessionStore::default(),
secret_key.clone(),
))
.wrap(
middleware::DefaultHeaders::new().add(("Permissions-Policy", "interest-cohort=()")),
)
// .configure(auth::adapter::load_adapters(db.pool.clone(), &settings))
.configure(utils::random_string::GenerateRandomString::inject())
})
.bind(&socket_addr)
.unwrap()
.run()
.await
.unwrap();
}