commit 2f61985681bc88c47931cc85a25735b84430f65a Author: Ivan Bushchik Date: Tue Jun 20 21:02:27 2023 +0300 Fork ivabus/aliurl diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target diff --git a/.rustfmt.toml b/.rustfmt.toml new file mode 100644 index 0000000..32e806d --- /dev/null +++ b/.rustfmt.toml @@ -0,0 +1,9 @@ +edition = "2021" +hard_tabs = true +merge_derives = true +reorder_imports = true +reorder_modules = true +use_field_init_shorthand = true +use_small_heuristics = "Off" +wrap_comments = true +comment_width = 80 diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..78a87d6 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,15 @@ +[package] +name = "urouter" +version = "0.1.0" +edition = "2021" +license = "MIT" +repository = "https://github.com/ivabus/urouter" +description = "Small router for (kinda) short domains (static fork of ivabus/aliurl)" + + +[dependencies] +rocket = "0.5.0-rc.3" +serde = { version = "1.0.163", features = ["derive"] } +serde_json = "1.0.96" +url-escape = "0.1.1" +smurf = "0.2.0" \ No newline at end of file diff --git a/Rocket.toml b/Rocket.toml new file mode 100644 index 0000000..a47a3ae --- /dev/null +++ b/Rocket.toml @@ -0,0 +1,9 @@ +[default] +address = "127.0.0.1" +port = 8080 +keep_alive = 0 +ident = "urouter" +ip_header = false +log_level = "normal" +temp_dir = "/tmp" +cli_colors = true diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..f4d4ec4 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,77 @@ +/* + * MIT License + * + * Copyright (c) 2023 Ivan Bushchik + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + */ + +#[macro_use] +extern crate rocket; + +use std::cell::OnceCell; +use std::path::PathBuf; + +use rocket::http::Status; +use rocket::response::content::RawText; +use rocket::response::Redirect; +use serde::Deserialize; + +const INDEX_REDIRECT: &'static str = "https://ivabus.dev"; +const _ALIAS: &'static str = include_str!("../alias.json"); +static mut ALIAS: OnceCell> = OnceCell::new(); + +#[derive(Deserialize, Clone)] +struct Alias { + uri: String, + alias: String, + is_url: Option, +} + +#[get("/")] +async fn get_page(page: String) -> Result, Redirect> { + let mut decoded_page = String::new(); + url_escape::decode_to_string(page, &mut decoded_page); + let alias = unsafe { ALIAS.get().unwrap() }; + + for i in alias { + if i.uri == decoded_page { + return match i.is_url { + Some(true) => Err(Redirect::to(i.alias.clone())), + _ => Ok(RawText(smurf::io::read_file_to_str(&PathBuf::from(&i.alias)).unwrap())), + }; + } + } + + Err(Redirect::to("/404")) +} + +#[get("/")] +async fn get_index() -> Redirect { + Redirect::to(INDEX_REDIRECT) +} + +#[rocket::main] +async fn main() -> Result<(), rocket::Error> { + unsafe { + ALIAS.set(serde_json::from_str(_ALIAS).unwrap()).unwrap_unchecked(); + } + let _rocket = rocket::build().mount("/", routes![get_page, get_index]).launch().await?; + Ok(()) +}