add cors and previous and next name functions

This commit is contained in:
Fries 2023-07-05 02:10:31 -07:00
parent 79e88cb2f4
commit 6e9bbe1d60
6 changed files with 148 additions and 18 deletions

86
Cargo.lock generated
View file

@ -17,6 +17,15 @@ version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe"
[[package]]
name = "aho-corasick"
version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "43f6cb1bf222025340178f382c426f13757b2960e89779dfcb319c32542a5a41"
dependencies = [
"memchr",
]
[[package]]
name = "anstream"
version = "0.3.2"
@ -443,6 +452,15 @@ version = "1.0.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1"
[[package]]
name = "form_urlencoded"
version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a62bc1cf6f830c2ec14a513a9fb124d0a213a629668a4186f329db21fe045652"
dependencies = [
"percent-encoding",
]
[[package]]
name = "fsevent-sys"
version = "4.1.0"
@ -653,6 +671,16 @@ dependencies = [
"want",
]
[[package]]
name = "idna"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7d20d6b07bfbc108882d88ed8e37d39636dcc260e15e30c45e6ba089610b917c"
dependencies = [
"unicode-bidi",
"unicode-normalization",
]
[[package]]
name = "indexmap"
version = "1.9.3"
@ -831,6 +859,7 @@ dependencies = [
"log",
"notify",
"rocket",
"rocket_cors",
"rust-embed",
"serde",
"serde_json",
@ -1161,6 +1190,8 @@ version = "1.8.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d0ab3ca65655bb1e41f2a8c8cd662eb4fb035e67c3f78da1d61dffe89d07300f"
dependencies = [
"aho-corasick",
"memchr",
"regex-syntax 0.7.2",
]
@ -1240,6 +1271,20 @@ dependencies = [
"unicode-xid",
]
[[package]]
name = "rocket_cors"
version = "0.6.0-alpha2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b12771b47f52e34d5d0e0e444aeba382863e73263cb9e18847e7d5b74aa2cbd0"
dependencies = [
"http",
"log",
"regex",
"rocket",
"unicase",
"url",
]
[[package]]
name = "rocket_http"
version = "0.5.0-rc.3"
@ -1579,6 +1624,21 @@ dependencies = [
"time-core",
]
[[package]]
name = "tinyvec"
version = "1.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "87cc5ceb3875bb20c2890005a4e226a4651264a5c75edb2421b52861a0a0cb50"
dependencies = [
"tinyvec_macros",
]
[[package]]
name = "tinyvec_macros"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20"
[[package]]
name = "tokio"
version = "1.29.0"
@ -1775,18 +1835,44 @@ dependencies = [
"version_check",
]
[[package]]
name = "unicode-bidi"
version = "0.3.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "92888ba5573ff080736b3648696b70cafad7d250551175acbaa4e0385b3e1460"
[[package]]
name = "unicode-ident"
version = "1.0.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b15811caf2415fb889178633e7724bad2509101cde276048e013b9def5e51fa0"
[[package]]
name = "unicode-normalization"
version = "0.1.22"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5c5713f0fc4b5db668a2ac63cdb7bb4469d8c9fed047b1d0292cc7b0ce2ba921"
dependencies = [
"tinyvec",
]
[[package]]
name = "unicode-xid"
version = "0.2.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f962df74c8c05a667b5ee8bcf162993134c104e96440b663c8daa176dc772d8c"
[[package]]
name = "url"
version = "2.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "50bff7831e19200a85b17131d085c25d7811bc4e186efdaf54bbd132994a88cb"
dependencies = [
"form_urlencoded",
"idna",
"percent-encoding",
]
[[package]]
name = "utf8parse"
version = "0.2.1"

View file

@ -7,6 +7,11 @@ version = "0.1.0"
edition = "2021"
rust-version = "1.70"
[profile.dev]
lto = "thin"
[profile.release]
lto = "thin"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
@ -51,3 +56,7 @@ default-features = false
version = "6"
default-features = false
features = ["macos_fsevent"]
[dependencies.rocket_cors]
version = "=0.6.0-alpha2"
default_features = false

View file

@ -17,4 +17,30 @@ pub fn next_url(source_url: &String, names: &Vec<Site>) -> Option<String> {
}
}
// TODO: previous_name, next_name
pub fn previous_name(source_url: &String, names: &Vec<Site>) -> Option<String> {
match names.iter().position(|r| &r.url == source_url) {
Some(index) if index == 0 => match &names[names.len() - 1].name {
Some(name) => Some(name.clone()),
None => previous_url(source_url, names),
},
Some(index) => match &names[index - 1].name {
Some(name) => Some(name.clone()),
None => previous_url(source_url, names),
},
None => None,
}
}
pub fn next_name(source_url: &String, names: &Vec<Site>) -> Option<String> {
match names.iter().position(|r| &r.url == source_url) {
Some(index) if index == names.len() - 1 => match &names[names.len() - 1].name {
Some(name) => Some(name.clone()),
None => next_url(source_url, names),
},
Some(index) => match &names[index + 1].name {
Some(name) => Some(name.clone()),
None => next_url(source_url, names),
},
None => None,
}
}

View file

@ -14,9 +14,11 @@ mod watcher;
#[launch]
async fn rocket() -> _ {
init_names().unwrap();
let cors = rocket_cors::CorsOptions::default().to_cors().unwrap();
tokio::task::spawn_blocking(hot_reloading);
rocket::build()
.manage(cors)
.mount(
"/",
routes![routes::index, routes::previous, routes::next, routes::name],

View file

@ -1,6 +1,6 @@
use crate::{
assets::{ErrorTemplate, IndexTemplate},
links::{next_url, previous_url},
links::{next_url, previous_url, previous_name, next_name},
sites::get_global_names,
};
@ -9,6 +9,7 @@ use rocket::{
response::Redirect,
serde::{json::Json, Serialize},
};
use rocket_cors::{Guard, Responder};
const NOT_FOUND_ERROR: ErrorTemplate = ErrorTemplate {
error: "Not Found",
@ -23,8 +24,10 @@ pub struct JsonResponse {
}
#[get("/")]
pub async fn index() -> IndexTemplate {
IndexTemplate { sites: get_global_names().await }
pub async fn index() -> IndexTemplate {
IndexTemplate {
sites: get_global_names().await,
}
}
#[get("/previous?<source_url>")]
@ -44,18 +47,22 @@ pub async fn next(source_url: String) -> Result<Redirect, Status> {
}
#[get("/name?<source_url>")]
pub async fn name(source_url: String) -> Result<Json<JsonResponse>, Status> {
let previous_site_name = previous_url(&source_url, &get_global_names().await);
let next_site_name = next_url(&source_url, &get_global_names().await);
pub async fn name(
source_url: String,
cors: Guard<'_>,
) -> Responder<Result<Json<JsonResponse>, Status>> {
let previous_site_name = previous_name(&source_url, &get_global_names().await);
let next_site_name = next_name(&source_url, &get_global_names().await);
if previous_site_name.is_none() && next_site_name.is_none() {
return Err(Status::NotFound);
return cors.responder(Err(Status::NotFound));
}
Ok(Json(JsonResponse {
let response = Ok(Json(JsonResponse {
previous_site_name,
next_site_name,
}))
}));
cors.responder(response)
}
#[catch(404)]

View file

@ -12,14 +12,14 @@
<main>
<h1>Welcome</h1>
<h2>Sites</h2>
{% for site in sites %}
{% match site.name %}
{% when Some with (value) %}
<p><a href="{{ site.url }}">{{ value }}</a></p>
{% when None %}
<p><a href="{{ site.url }}">{{ site.url }}</a></p>
{% endmatch %}
{% endfor %}
{% for site in sites %}
{% match site.name %}
{% when Some with (value) %}
<p><a href="{{ site.url }}">{{ value }}</a></p>
{% when None %}
<p><a href="{{ site.url }}">{{ site.url }}</a></p>
{% endmatch %}
{% endfor %}
</main>
</body>