aboutsummaryrefslogtreecommitdiff
path: root/src/routes/ui.rs
blob: f70b6f104a49609e8f60f44b85145c71844bafad (plain) (blame)
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
// 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(),
    )
  }
}