1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
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"#,
)
}
|