0.5.1: Add proper alias.json specification

Signed-off-by: Ivan Bushchik <ivabus@ivabus.dev>
This commit is contained in:
Ivan Bushchik 2023-12-21 20:17:06 +03:00
parent e84e1a3317
commit 0815b295b2
No known key found for this signature in database
GPG key ID: 2F16FBF3262E090C
4 changed files with 68 additions and 10 deletions

View file

@ -1,6 +1,6 @@
[package] [package]
name = "urouter" name = "urouter"
version = "0.5.0" version = "0.5.1"
edition = "2021" edition = "2021"
license = "MIT" license = "MIT"
repository = "https://github.com/ivabus/urouter" repository = "https://github.com/ivabus/urouter"

View file

@ -1,28 +1,59 @@
# urouter # urouter
Static (list of routes read once) http router for routing small domains. Static (list of routes read once) http router for routing small domains.
## Installation ## Installation
```shell ```shell
git clone https://github.com/ivabus/urouter cargo install urouter
cd urouter
``` ```
Edit `alias.json` (or any other JSON file, check `--alias-file` option) and `cargo run` Edit `alias.json` (or any other JSON file, check `--alias-file` option) and `cargo run`
## `alias.json` example ## `alias.json` specification
JSON file with array of sets (or set with one field of arrays of sets with `--alias-file-is-set-not-a-list`, may be useful i.e. [Nix packaging](https://github.com/ivabus/nixos/blob/master/roles/server/urouter.nix)).
Each set contains 2 necessary elements and 1 optional.
- Necessary
- `uri` (string) - of url after host (e.g., `/`, `some/cool/path`, should not start with `/` (only for root))
- `alias` (set) - set of one field
- `url` (string) - redirect to url with HTTP 303 See Other
- `file` (string) - read file from path `--dir/file` where `--dir` is option (default: `.`, see `--help`) and respond with HTTP 200 OK `content-type: text/plain; charset=utf-8`
- `text` (string) - plain text
- Optional
- `agent` (set) - set of one necessary field and one optional
- `regex` (string) - regular expression to match user-agent HTTP header
- `only_matching` (bool, optional, false by default) - if false whole alias will be visible for any user agent, if true only for regex matched
#### Set of array of sets
```json
{
"alias": [
{
"uri": "/",
"alias": {
"url": "https://somecoolwebsite"
}
}
]
}
```
### `alias.json` example
```json ```json
[ [
{ {
"uri":"/", "uri": "/",
"alias": { "alias": {
"url": "https://somecoolwebsite" "url": "https://somecoolwebsite"
} }
}, },
{ {
"uri":"/", "uri": "/",
"alias": { "alias": {
"file": "somecoolscript" "file": "somecoolscript"
}, },
@ -32,7 +63,7 @@ Edit `alias.json` (or any other JSON file, check `--alias-file` option) and `car
} }
}, },
{ {
"uri":"/text", "uri": "text",
"alias": { "alias": {
"text": "sometext" "text": "sometext"
} }

27
alias.json Normal file
View file

@ -0,0 +1,27 @@
[
{
"uri":"/",
"alias": {
"url": "https://ivabus.dev"
}
},
{
"uri":"/",
"alias": {
"file": "src/main.rs"
},
"agent": {
"regex": "^curl/[0-9].[0-9].[0-9]$"
}
},
{
"uri":"text",
"alias": {
"text": "sometext"
},
"agent": {
"regex": "^curl/[0-9].[0-9].[0-9]$",
"only_matching": true
}
}
]

View file

@ -57,7 +57,7 @@ fn get_return(alias: &Alias) -> Response {
} }
#[get("/<page>")] #[get("/<page>")]
fn get_page(page: String, user_agent: UserAgent) -> Response { fn get_page(page: &str, user_agent: UserAgent) -> Response {
let mut decoded_page = String::new(); let mut decoded_page = String::new();
url_escape::decode_to_string(page, &mut decoded_page); url_escape::decode_to_string(page, &mut decoded_page);
let alias = unsafe { ALIAS.get().unwrap() }; let alias = unsafe { ALIAS.get().unwrap() };
@ -102,7 +102,7 @@ fn get_page(page: String, user_agent: UserAgent) -> Response {
#[get("/")] #[get("/")]
async fn index(user_agent: UserAgent) -> Response { async fn index(user_agent: UserAgent) -> Response {
get_page("/".to_string(), user_agent) get_page("/", user_agent)
} }
#[rocket::main] #[rocket::main]