diff --git a/README.md b/README.md new file mode 100644 index 0000000..8728c5c --- /dev/null +++ b/README.md @@ -0,0 +1,13 @@ +## Usage + +```bash +Usage: thirappu-vizha --port --ribbon --store --store-weburl + +Options: + -p, --port Port number to start server + -r, --ribbon File name of nginx vhost for the ribbon service + -s, --store File name of nginx vhost for the store front + --store-weburl + -h, --help Print help + -V, --version Print version +``` diff --git a/src/main.rs b/src/main.rs index 0004479..d81d030 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,34 +7,34 @@ use std::process::Command; use actix_web::web::ServiceConfig; use actix_web::HttpResponse; -use actix_web::{get, post, web, App, HttpServer, Responder, http::header}; -use url::Url; +use actix_web::{get, http::header, post, web, App, HttpServer, Responder}; use clap::Parser; - +use url::Url; fn thirappu(ribbon: &str, store: &str) { let ribbon = format!("/etc/nginx/sites-enabled/{ribbon}"); let store_original = format!("/etc/nginx/sites-available/{store}"); let store_link = format!("/etc/nginx/sites-enabled/{store}"); - std::fs::remove_file(&ribbon).unwrap(); - unix_symlink(&store_original, &store_link).unwrap(); - Command::new("nginx").args(["-s", "reload"]).output().unwrap(); + std::fs::remove_file(ribbon).unwrap(); + unix_symlink(store_original, store_link).unwrap(); + Command::new("nginx") + .args(["-s", "reload"]) + .output() + .unwrap(); } - fn reset(ribbon: &str, store: &str) { let ribbon_link = format!("/etc/nginx/sites-enabled/{ribbon}"); let ribbon_original = format!("/etc/nginx/sites-available/{store}"); let store_link = format!("/etc/nginx/sites-enabled/{store}"); - std::fs::remove_file(&store_link).unwrap(); - unix_symlink(&ribbon_original, &ribbon_link).unwrap(); - Command::new("nginx").args(["-s", "reload"]).output().unwrap(); + std::fs::remove_file(store_link).unwrap(); + unix_symlink(ribbon_original, ribbon_link).unwrap(); + Command::new("nginx") + .args(["-s", "reload"]) + .output() + .unwrap(); } - - - -/// Simple program to greet a person #[derive(Parser, Debug, Clone)] #[command(version, about, long_about = None)] struct Args { @@ -49,51 +49,46 @@ struct Args { #[arg(short, long)] store: String, - #[arg(short, long)] + #[arg(long)] store_weburl: Url, } -//#[actix_web::main] -//async fn main() { -// -// -// thirappu(&args.ribbon, &args.store) -//} - #[get("/reset")] async fn reset_get() -> impl Responder { - let reset = include_str!("./reset.html"); - HttpResponse::Ok().content_type("text/html; charset=utf-8").body(reset) + HttpResponse::Ok() + .content_type("text/html; charset=utf-8") + .body(reset) } #[post("/reset")] async fn reset_post(ctx: web::Data) -> impl Responder { reset(&ctx.ribbon, &ctx.store); - - HttpResponse::Found().append_header((header::LOCATION, "/")).finish() + HttpResponse::Found() + .append_header((header::LOCATION, "/")) + .finish() } #[post("/")] async fn open_post(ctx: web::Data) -> impl Responder { thirappu(&ctx.ribbon, &ctx.store); - HttpResponse::Found().append_header((header::LOCATION, ctx.store_weburl.to_string())).finish() + HttpResponse::Found() + .append_header((header::LOCATION, ctx.store_weburl.to_string())) + .finish() } - - #[get("/")] async fn open_get() -> impl Responder { - let open = include_str!("./open.html"); - HttpResponse::Ok().content_type("text/html; charset=utf-8").body(open) + HttpResponse::Ok() + .content_type("text/html; charset=utf-8") + .body(open) } - fn services(cfg: &mut ServiceConfig) { cfg.service(reset_get); cfg.service(reset_post); @@ -105,10 +100,8 @@ fn services(cfg: &mut ServiceConfig) { async fn main() -> std::io::Result<()> { let args = Args::parse(); let data = web::Data::new(args.clone()); - HttpServer::new(move|| { - App::new().app_data(data.clone()).configure(services) - }) - .bind(("127.0.0.1", args.port))? - .run() - .await + HttpServer::new(move || App::new().app_data(data.clone()).configure(services)) + .bind(("127.0.0.1", args.port))? + .run() + .await } diff --git a/src/open.html b/src/open.html index 5872111..8cb8271 100644 --- a/src/open.html +++ b/src/open.html @@ -13,7 +13,7 @@ SPDX-License-Identifier: AGPL-3.0-or-later -
+