diff options
| author | Fuwn <[email protected]> | 2021-05-12 23:48:04 -0700 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2021-05-12 23:48:04 -0700 |
| commit | c7a282bd3f96c0e3cc0807aabd5b7104df6effc9 (patch) | |
| tree | 64818c0add5762ba1ae83c8d9e7aa7bdb605f6f2 /src/routes | |
| download | lime-main.tar.xz lime-main.zip | |
Diffstat (limited to 'src/routes')
| -rw-r--r-- | src/routes/api.rs | 79 | ||||
| -rw-r--r-- | src/routes/mod.rs | 5 | ||||
| -rw-r--r-- | src/routes/ui.rs | 55 |
3 files changed, 139 insertions, 0 deletions
diff --git a/src/routes/api.rs b/src/routes/api.rs new file mode 100644 index 0000000..2445d5d --- /dev/null +++ b/src/routes/api.rs @@ -0,0 +1,79 @@ +// Copyright (C) 2021-2021 Fuwn +// SPDX-License-Identifier: GPL-3.0-only + +use actix_web::{web, HttpRequest, HttpResponse}; +use askama::Template; + +use crate::{ + db::{find_link, insert_link, models::LinkForm}, + structure::{PostCreateShort, StatisticsApi, TextTemplate}, +}; + +#[post("/create")] +pub fn create(req: HttpRequest, params: web::Form<PostCreateShort>) -> HttpResponse { + if let Err(e) = insert_link(LinkForm { + long: ¶ms.long, + short: ¶ms.short, + ip: &req.peer_addr().unwrap().ip().to_string(), + }) { + HttpResponse::Ok().body( + TextTemplate { + text: e.to_string().as_str(), + } + .render() + .unwrap(), + ) + } else { + HttpResponse::Ok().body( + TextTemplate { + text: &format!("short url created: /{}", params.short), + } + .render() + .unwrap(), + ) + } +} + +#[get("/statistics")] +pub fn statistics(req: HttpRequest) -> HttpResponse { + let queries = qstring::QString::from(req.query_string()); + + let result = find_link(queries.get("short").unwrap_or(""), false); + + HttpResponse::Ok().json(if let Err(e) = result { + StatisticsApi { + long: e.to_string(), + disabled: true, + uses: 0, + } + } else { + let usable = result.unwrap(); + StatisticsApi { + long: usable.long, + disabled: usable.disabled, + uses: usable.uses, + } + }) +} + +#[get("/")] +pub fn index() -> HttpResponse { + HttpResponse::Ok().body( + r#"# lime api +## routes +if a route requires a parameter, it will be notated like <this>. +for example; if a route is notated as "/api/v1/route?<parameter>", you can +access that route via the url +"http://this.domain/api/v1/route?parameter=something" + +- /api/v1 + - /: index page (you are here) + - /statistics?<short>: get information about a short url; long, disabled, uses + - /create: a post route which takes a form; long and short, creates a new + short url + +### license +gnu general public license v3.0 (gpl-3.0-only) +https://github.com/fuwn/lime/blob/main/license"#, + ) +} diff --git a/src/routes/mod.rs b/src/routes/mod.rs new file mode 100644 index 0000000..90851fc --- /dev/null +++ b/src/routes/mod.rs @@ -0,0 +1,5 @@ +// Copyright (C) 2021-2021 Fuwn +// SPDX-License-Identifier: GPL-3.0-only + +pub mod api; +pub mod ui; diff --git a/src/routes/ui.rs b/src/routes/ui.rs new file mode 100644 index 0000000..f70b6f1 --- /dev/null +++ b/src/routes/ui.rs @@ -0,0 +1,55 @@ +// Copyright (C) 2021-2021 Fuwn +// SPDX-License-Identifier: GPL-3.0-only + +use actix_web::{web, HttpResponse}; +use askama::Template; + +use crate::{db::find_link, structure::TextTemplate}; + +#[get("/")] +pub fn index() -> HttpResponse { + HttpResponse::Ok().body(include_str!("../../templates/index.html")) +} + +#[get("/{short}")] +pub async fn handle(info: web::Path<String>) -> HttpResponse { + let result = find_link(&info.0, true); + + if let Err(ref e) = result { + HttpResponse::Ok().body( + TextTemplate { + text: e.to_string().as_str(), + } + .render() + .unwrap(), + ) + } else { + HttpResponse::Ok().body(format!( + "<script>location.href=\"/{}\"</script>", + result.unwrap().long, + )) + } +} + +#[get("/{short}/statistics")] +pub async fn statistics(info: web::Path<String>) -> HttpResponse { + let result = find_link(&info.0, false); + + if let Err(ref e) = result { + HttpResponse::Ok().body( + TextTemplate { + text: e.to_string().as_str(), + } + .render() + .unwrap(), + ) + } else { + HttpResponse::Ok().body( + TextTemplate { + text: &format!("/{} has {} uses", info.0, result.unwrap().uses), + } + .render() + .unwrap(), + ) + } +} |