feat: logging

This commit is contained in:
Aravinth Manivannan 2024-03-20 14:49:03 +05:30
parent d06438b5f0
commit 1a0b35d615
Signed by: realaravinth
GPG key ID: F8F50389936984FF
3 changed files with 118 additions and 3 deletions

88
Cargo.lock generated
View file

@ -513,6 +513,19 @@ dependencies = [
"cfg-if",
]
[[package]]
name = "env_logger"
version = "0.10.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4cd405aab171cb85d6735e5c8d9db038c17d3ca007a4d2c25f337935c3d90580"
dependencies = [
"humantime",
"is-terminal",
"log",
"regex",
"termcolor",
]
[[package]]
name = "equivalent"
version = "1.0.1"
@ -632,6 +645,12 @@ version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea"
[[package]]
name = "hermit-abi"
version = "0.3.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d231dfb89cfffdbc30e7fc41579ed6066ad03abda9e567ccafae602b97ec5024"
[[package]]
name = "http"
version = "0.2.12"
@ -655,6 +674,12 @@ version = "1.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "df3b46402a9d5adb4c86a0cf463f42e19994e3ee891101b1841f30a545cb49a9"
[[package]]
name = "humantime"
version = "2.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4"
[[package]]
name = "idna"
version = "0.5.0"
@ -675,6 +700,17 @@ dependencies = [
"hashbrown",
]
[[package]]
name = "is-terminal"
version = "0.4.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f23ff5ef2b80d608d61efee834934d862cd92461afc0560dedf493e4c033738b"
dependencies = [
"hermit-abi",
"libc",
"windows-sys 0.52.0",
]
[[package]]
name = "itoa"
version = "1.0.10"
@ -854,6 +890,16 @@ version = "0.2.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de"
[[package]]
name = "pretty_env_logger"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "865724d4dbe39d9f3dd3b52b88d859d66bcb2d6a0acfd5ea68a65fb66d4bdc1c"
dependencies = [
"env_logger",
"log",
]
[[package]]
name = "proc-macro2"
version = "1.0.79"
@ -1089,12 +1135,23 @@ dependencies = [
"unicode-ident",
]
[[package]]
name = "termcolor"
version = "1.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "06794f8f6c5c898b3275aebefa6b8a1cb24cd2c6c79397ab15774837a0bc5755"
dependencies = [
"winapi-util",
]
[[package]]
name = "thirappu-vizha"
version = "0.1.0"
dependencies = [
"actix-web",
"clap",
"log",
"pretty_env_logger",
"serde",
"serde_json",
"url",
@ -1254,6 +1311,37 @@ version = "0.11.0+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423"
[[package]]
name = "winapi"
version = "0.3.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
dependencies = [
"winapi-i686-pc-windows-gnu",
"winapi-x86_64-pc-windows-gnu",
]
[[package]]
name = "winapi-i686-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
[[package]]
name = "winapi-util"
version = "0.1.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f29e6f9198ba0d26b4c9f07dbe6f9ed633e1f3d5b8b414090084349e46a52596"
dependencies = [
"winapi",
]
[[package]]
name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
[[package]]
name = "windows-sys"
version = "0.48.0"

View file

@ -8,6 +8,8 @@ edition = "2021"
[dependencies]
actix-web = "4.5.1"
clap = { version = "4", features = ["derive"] }
log = "0.4.21"
pretty_env_logger = "0.5.0"
serde = { version = "1.0.197", features = ["derive"] }
serde_json = "1.0.114"
url = { version = "2.5.0", features = ["serde"] }

View file

@ -2,6 +2,7 @@
//
// SPDX-License-Identifier: AGPL-3.0-or-later
use std::fmt::Display;
use std::os::unix::fs::symlink as unix_symlink;
use std::process::Command;
@ -10,29 +11,37 @@ use actix_web::HttpResponse;
use actix_web::{get, http::header, post, web, App, HttpServer, Responder};
use clap::Parser;
use url::Url;
use log::info;
fn thirappu(ribbon: &str, store: &str) {
let ribbon = format!("/etc/nginx/sites-enabled/{ribbon}");
let ribbon_link = 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();
std::fs::remove_file(ribbon_link).unwrap();
info!("removed store");
unix_symlink(store_original, store_link).unwrap();
info!("Linked store");
Command::new("nginx")
.args(["-s", "reload"])
.output()
.unwrap();
info!("nginx reloaded");
}
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 ribbon_original = format!("/etc/nginx/sites-available/{ribbon}");
let store_link = format!("/etc/nginx/sites-enabled/{store}");
info!("resetting vizha: store_link {store_link} ribbon_link: {ribbon_link} ribbon_original: {ribbon_original}");
std::fs::remove_file(store_link).unwrap();
info!("Removed store link");
unix_symlink(ribbon_original, ribbon_link).unwrap();
info!("Linked ribbon");
Command::new("nginx")
.args(["-s", "reload"])
.output()
.unwrap();
info!("nginx reloaded");
}
#[derive(Parser, Debug, Clone)]
@ -53,6 +62,13 @@ struct Args {
store_weburl: Url,
}
impl Display for Args {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "port: {} ribbon: {} store: {} store_weburl: {}", self.port, self.ribbon, self.store, self.store_weburl.to_string())
}
}
#[get("/reset")]
async fn reset_get() -> impl Responder {
let reset = include_str!("./reset.html");
@ -108,7 +124,16 @@ fn services(cfg: &mut ServiceConfig) {
#[actix_web::main] // or #[tokio::main]
async fn main() -> std::io::Result<()> {
if std::env::var("RUST_LOG").is_err() {
std::env::set_var("RUST_LOG", "info");
}
pretty_env_logger::init();
let args = Args::parse();
info!("Starting server with {args}");
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))?