From 7322bf005795412083ede49755b1e7feeec1bdcd Mon Sep 17 00:00:00 2001 From: Mylloon Date: Sun, 9 Apr 2023 17:59:18 +0200 Subject: [PATCH] add script --- run.sh | 31 ++++++++++++++++++++++++ src/main.rs | 68 ++++++++++++++++++++++++++++++++--------------------- 2 files changed, 72 insertions(+), 27 deletions(-) create mode 100755 run.sh diff --git a/run.sh b/run.sh new file mode 100755 index 0000000..ea5d8a6 --- /dev/null +++ b/run.sh @@ -0,0 +1,31 @@ +#!/usr/bin/env bash + +set -o errexit # crash the script when a command crash +set -o pipefail # same as above for piped command +set -o nounset # crash when a variable doesnt exist + + +# TRACE=1 for debug +if [[ "${TRACE-0}" == "1" ]]; then + set -o xtrace +fi + +cd "$(dirname "$0")" # change script directory + +main() { + dir_dist="./dist" + dir_template="/templates" + if [ ! -d "$dir_dist$dir_template" ]; then + echo "Copy template to dist directory" + mkdir -p "$dir_dist$dir_template" + cp -r ".$dir_template" "$dir_dist" + + echo "Generate minified templates" + cargo run --release -- --no-http + fi + + echo "Run the app" + cargo run --release +} + +main "$@" diff --git a/src/main.rs b/src/main.rs index 00e7570..31807f1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,6 @@ use actix_files::Files; use actix_web::{middleware::DefaultHeaders, web, App, HttpServer}; +use std::env; use std::io; mod config; @@ -29,32 +30,45 @@ async fn main() -> io::Result<()> { let addr = ("0.0.0.0", config.fc.port.unwrap()); - println!( - "Listening to {}://{}:{}", - config.clone().fc.scheme.unwrap(), - addr.0, - addr.1 - ); + let mut http = true; + let mut args = env::args(); + args.next(); + for argument in args { + if argument == "--no-http" { + http = false + } + } - HttpServer::new(move || { - App::new() - .app_data(web::Data::new(config.clone())) - .wrap(DefaultHeaders::new().add(( - "Onion-Location", - config.fc.onion.as_deref().unwrap_or_default(), - ))) - .service(index::page) - .service(agreements::security) - .service(agreements::humans) - .service(agreements::robots) - .service(agreements::sitemap) - .service(networks::page) - .service(portfolio::page) - .service(contrib::page) - .service(Files::new("/", config.static_location.clone())) - .default_service(web::to(not_found::page)) - }) - .bind(addr)? - .run() - .await + if http { + println!( + "Listening to {}://{}:{}", + config.clone().fc.scheme.unwrap(), + addr.0, + addr.1 + ); + + HttpServer::new(move || { + App::new() + .app_data(web::Data::new(config.clone())) + .wrap(DefaultHeaders::new().add(( + "Onion-Location", + config.fc.onion.as_deref().unwrap_or_default(), + ))) + .service(index::page) + .service(agreements::security) + .service(agreements::humans) + .service(agreements::robots) + .service(agreements::sitemap) + .service(networks::page) + .service(portfolio::page) + .service(contrib::page) + .service(Files::new("/", config.static_location.clone())) + .default_service(web::to(not_found::page)) + }) + .bind(addr)? + .run() + .await + } else { + Ok(()) + } }