115 lines
2.9 KiB
Rust
115 lines
2.9 KiB
Rust
|
// SPDX-FileCopyrightText: 2024 Aravinth Manivannan <realaravinth@batsense.net>
|
||
|
//
|
||
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||
|
|
||
|
use std::os::unix::fs::symlink as unix_symlink;
|
||
|
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 clap::Parser;
|
||
|
|
||
|
|
||
|
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();
|
||
|
}
|
||
|
|
||
|
|
||
|
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();
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
/// Simple program to greet a person
|
||
|
#[derive(Parser, Debug, Clone)]
|
||
|
#[command(version, about, long_about = None)]
|
||
|
struct Args {
|
||
|
/// Port number to start server
|
||
|
#[arg(short, long)]
|
||
|
port: u16,
|
||
|
|
||
|
/// File name of nginx vhost for the ribbon service
|
||
|
#[arg(short, long)]
|
||
|
ribbon: String,
|
||
|
/// File name of nginx vhost for the store front
|
||
|
#[arg(short, long)]
|
||
|
store: String,
|
||
|
|
||
|
#[arg(short, 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)
|
||
|
}
|
||
|
|
||
|
#[post("/reset")]
|
||
|
async fn reset_post(ctx: web::Data<Args>) -> impl Responder {
|
||
|
reset(&ctx.ribbon, &ctx.store);
|
||
|
|
||
|
|
||
|
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()
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
|
#[get("/")]
|
||
|
async fn open_get() -> impl Responder {
|
||
|
|
||
|
let open = include_str!("./open.html");
|
||
|
|
||
|
HttpResponse::Ok().content_type("text/html; charset=utf-8").body(open)
|
||
|
}
|
||
|
|
||
|
|
||
|
fn services(cfg: &mut ServiceConfig) {
|
||
|
cfg.service(reset_get);
|
||
|
cfg.service(reset_post);
|
||
|
cfg.service(open_get);
|
||
|
cfg.service(open_post);
|
||
|
}
|
||
|
|
||
|
#[actix_web::main] // or #[tokio::main]
|
||
|
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
|
||
|
}
|