fix: open submission endpoint

This commit is contained in:
Aravinth Manivannan 2024-03-19 02:12:17 +05:30
parent 852b0b9491
commit 6cdad92313
Signed by untrusted user: realaravinth
GPG key ID: F8F50389936984FF
3 changed files with 45 additions and 39 deletions

13
README.md Normal file
View file

@ -0,0 +1,13 @@
## Usage
```bash
Usage: thirappu-vizha --port <PORT> --ribbon <RIBBON> --store <STORE> --store-weburl <STORE_WEBURL>
Options:
-p, --port <PORT> Port number to start server
-r, --ribbon <RIBBON> File name of nginx vhost for the ribbon service
-s, --store <STORE> File name of nginx vhost for the store front
--store-weburl <STORE_WEBURL>
-h, --help Print help
-V, --version Print version
```

View file

@ -7,34 +7,34 @@ use std::process::Command;
use actix_web::web::ServiceConfig; use actix_web::web::ServiceConfig;
use actix_web::HttpResponse; use actix_web::HttpResponse;
use actix_web::{get, post, web, App, HttpServer, Responder, http::header}; use actix_web::{get, http::header, post, web, App, HttpServer, Responder};
use url::Url;
use clap::Parser; use clap::Parser;
use url::Url;
fn thirappu(ribbon: &str, store: &str) { fn thirappu(ribbon: &str, store: &str) {
let ribbon = format!("/etc/nginx/sites-enabled/{ribbon}"); let ribbon = format!("/etc/nginx/sites-enabled/{ribbon}");
let store_original = format!("/etc/nginx/sites-available/{store}"); let store_original = format!("/etc/nginx/sites-available/{store}");
let store_link = format!("/etc/nginx/sites-enabled/{store}"); let store_link = format!("/etc/nginx/sites-enabled/{store}");
std::fs::remove_file(&ribbon).unwrap(); std::fs::remove_file(ribbon).unwrap();
unix_symlink(&store_original, &store_link).unwrap(); unix_symlink(store_original, store_link).unwrap();
Command::new("nginx").args(["-s", "reload"]).output().unwrap(); Command::new("nginx")
.args(["-s", "reload"])
.output()
.unwrap();
} }
fn reset(ribbon: &str, store: &str) { fn reset(ribbon: &str, store: &str) {
let ribbon_link = format!("/etc/nginx/sites-enabled/{ribbon}"); let ribbon_link = format!("/etc/nginx/sites-enabled/{ribbon}");
let ribbon_original = format!("/etc/nginx/sites-available/{store}"); let ribbon_original = format!("/etc/nginx/sites-available/{store}");
let store_link = format!("/etc/nginx/sites-enabled/{store}"); let store_link = format!("/etc/nginx/sites-enabled/{store}");
std::fs::remove_file(&store_link).unwrap(); std::fs::remove_file(store_link).unwrap();
unix_symlink(&ribbon_original, &ribbon_link).unwrap(); unix_symlink(ribbon_original, ribbon_link).unwrap();
Command::new("nginx").args(["-s", "reload"]).output().unwrap(); Command::new("nginx")
.args(["-s", "reload"])
.output()
.unwrap();
} }
/// Simple program to greet a person
#[derive(Parser, Debug, Clone)] #[derive(Parser, Debug, Clone)]
#[command(version, about, long_about = None)] #[command(version, about, long_about = None)]
struct Args { struct Args {
@ -49,51 +49,46 @@ struct Args {
#[arg(short, long)] #[arg(short, long)]
store: String, store: String,
#[arg(short, long)] #[arg(long)]
store_weburl: Url, store_weburl: Url,
} }
//#[actix_web::main]
//async fn main() {
//
//
// thirappu(&args.ribbon, &args.store)
//}
#[get("/reset")] #[get("/reset")]
async fn reset_get() -> impl Responder { async fn reset_get() -> impl Responder {
let reset = include_str!("./reset.html"); 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")] #[post("/reset")]
async fn reset_post(ctx: web::Data<Args>) -> impl Responder { async fn reset_post(ctx: web::Data<Args>) -> impl Responder {
reset(&ctx.ribbon, &ctx.store); reset(&ctx.ribbon, &ctx.store);
HttpResponse::Found()
HttpResponse::Found().append_header((header::LOCATION, "/")).finish() .append_header((header::LOCATION, "/"))
.finish()
} }
#[post("/")] #[post("/")]
async fn open_post(ctx: web::Data<Args>) -> impl Responder { async fn open_post(ctx: web::Data<Args>) -> impl Responder {
thirappu(&ctx.ribbon, &ctx.store); 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("/")] #[get("/")]
async fn open_get() -> impl Responder { async fn open_get() -> impl Responder {
let open = include_str!("./open.html"); 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) { fn services(cfg: &mut ServiceConfig) {
cfg.service(reset_get); cfg.service(reset_get);
cfg.service(reset_post); cfg.service(reset_post);
@ -105,9 +100,7 @@ fn services(cfg: &mut ServiceConfig) {
async fn main() -> std::io::Result<()> { async fn main() -> std::io::Result<()> {
let args = Args::parse(); let args = Args::parse();
let data = web::Data::new(args.clone()); let data = web::Data::new(args.clone());
HttpServer::new(move|| { HttpServer::new(move || App::new().app_data(data.clone()).configure(services))
App::new().app_data(data.clone()).configure(services)
})
.bind(("127.0.0.1", args.port))? .bind(("127.0.0.1", args.port))?
.run() .run()
.await .await

View file

@ -13,7 +13,7 @@ SPDX-License-Identifier: AGPL-3.0-or-later
</head> </head>
<body> <body>
<form action="/reset" method="post"> <form action="/" method="post">
<button type="submit">Click here to open store</button> <button type="submit">Click here to open store</button>
</form> </form>