Add some more Response types
All checks were successful
Cargo Build & Test / Tests (pull_request) Successful in 19s
Cargo Build & Test / check-cargo-version (pull_request) Successful in 5s
Cargo Build & Test / Test publish (pull_request) Successful in 16s

This commit is contained in:
Jan Gleytenhoover 2024-08-20 22:11:40 +02:00
parent 55fb3a196e
commit aaa062db31
3 changed files with 148 additions and 1 deletions

94
Cargo.lock generated

@ -20,10 +20,18 @@ version = "1.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "df3b46402a9d5adb4c86a0cf463f42e19994e3ee891101b1841f30a545cb49a9"
[[package]]
name = "itoa"
version = "1.0.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "49f1f14873335454500d59611f1cf4a4b0f786f9ac11f4312a78e4cf2566695b"
[[package]]
name = "loadstar"
version = "0.0.7"
dependencies = [
"ascii",
"maud",
"tiny_http",
]
@ -33,6 +41,80 @@ version = "0.4.22"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a7a70ba024b9dc04c27ea2f0c0548feb474ec5c54bba33a7f72f873a39d07b24"
[[package]]
name = "maud"
version = "0.26.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "df518b75016b4289cdddffa1b01f2122f4a49802c93191f3133f6dc2472ebcaa"
dependencies = [
"itoa",
"maud_macros",
]
[[package]]
name = "maud_macros"
version = "0.26.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fa453238ec218da0af6b11fc5978d3b5c3a45ed97b722391a2a11f3306274e18"
dependencies = [
"proc-macro-error",
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "proc-macro-error"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c"
dependencies = [
"proc-macro-error-attr",
"proc-macro2",
"quote",
"version_check",
]
[[package]]
name = "proc-macro-error-attr"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869"
dependencies = [
"proc-macro2",
"quote",
"version_check",
]
[[package]]
name = "proc-macro2"
version = "1.0.86"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5e719e8df665df0d1c8fbfd238015744736151d4445ec0836b8e628aae103b77"
dependencies = [
"unicode-ident",
]
[[package]]
name = "quote"
version = "1.0.36"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0fa76aaf39101c457836aec0ce2316dbdc3ab723cdda1c6bd4e6ad4208acaca7"
dependencies = [
"proc-macro2",
]
[[package]]
name = "syn"
version = "2.0.75"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f6af063034fc1935ede7be0122941bafa9bacb949334d090b77ca98b5817c7d9"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
name = "tiny_http"
version = "0.12.0"
@ -44,3 +126,15 @@ dependencies = [
"httpdate",
"log",
]
[[package]]
name = "unicode-ident"
version = "1.0.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b"
[[package]]
name = "version_check"
version = "0.9.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a"

@ -8,3 +8,5 @@ edition = "2021"
[dependencies]
tiny_http = "0.12.0"
maud = "0.26.0"
ascii = "1.1.0"

@ -1,7 +1,58 @@
use tiny_http::{Request, Response};
use ascii::AsciiString;
use maud::Markup;
use tiny_http::{Header, Request, Response};
/// Returns a 404 response
pub fn respond_not_found(request: Request) {
let response = Response::from_string("Not Found").with_status_code(404);
let _ = request.respond(response);
}
/// Renders html
pub fn respond_html(request: Request, content: Markup) {
let response = Response::from_string(content);
let response = response.with_header(Header {
field: "Content-Type".parse().unwrap(),
value: AsciiString::from_ascii("text/html; charset=utf8").unwrap(),
});
let _ = request.respond(response);
}
/// Redirects the user to another route
pub fn respond_redirect(request: Request, new_route: &str) {
let response = Response::from_string("Redirect");
let response = response.with_header(Header {
field: "Content-Type".parse().unwrap(),
value: AsciiString::from_ascii("text/html").unwrap(),
});
let response = response.with_header(Header {
field: "Location".parse().unwrap(),
value: AsciiString::from_ascii(new_route).unwrap(),
});
let response = response.with_status_code(302);
let _ = request.respond(response);
}
/// Redirects the user to another route while also adding a cookie
pub fn respond_redirect_cookie(request: Request, new_route: &str, cookie_name: &str, cookie_value: &str) {
let response = Response::from_string("Redirect")
.with_header(Header {
field: "Content-Type".parse().unwrap(),
value: AsciiString::from_ascii("text/html").unwrap(),
})
.with_header(Header {
field: "Location".parse().unwrap(),
value: AsciiString::from_ascii(new_route).unwrap(),
})
.with_header(Header {
field: "Set-Cookie".parse().unwrap(),
value: AsciiString::from_ascii(format!("{}={}", cookie_name, cookie_value)).unwrap(),
})
.with_status_code(302);
let _ = request.respond(response);
}