mirror of
https://github.com/ivabus/urouter
synced 2024-11-14 12:35:14 +03:00
0.5.1: Add proper alias.json specification
Signed-off-by: Ivan Bushchik <ivabus@ivabus.dev>
This commit is contained in:
parent
e84e1a3317
commit
0815b295b2
4 changed files with 68 additions and 10 deletions
|
@ -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"
|
||||||
|
|
43
README.md
43
README.md
|
@ -5,24 +5,55 @@ 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
27
alias.json
Normal 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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
|
@ -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]
|
||||||
|
|
Loading…
Reference in a new issue