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::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<Args>) -> 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<Args>) -> 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
}

View file

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