2023-04-04 04:50:23 +10:00
|
|
|
use std::env;
|
|
|
|
|
2023-04-03 13:26:23 +10:00
|
|
|
use actix_files::{Files, NamedFile};
|
|
|
|
use actix_web::{
|
2023-04-04 08:58:19 +10:00
|
|
|
delete, get, post,
|
2023-04-03 13:26:23 +10:00
|
|
|
web::{self, Redirect},
|
2023-04-04 02:55:27 +10:00
|
|
|
App, HttpResponse, HttpServer, Responder,
|
2023-04-03 13:26:23 +10:00
|
|
|
};
|
|
|
|
mod database;
|
|
|
|
mod utils;
|
2023-04-03 07:53:55 +10:00
|
|
|
|
2023-04-03 13:26:23 +10:00
|
|
|
// Define the routes
|
|
|
|
|
|
|
|
// Add new links
|
2023-04-04 08:40:37 +10:00
|
|
|
#[post("/api/new")]
|
|
|
|
async fn add_link(req: String) -> HttpResponse {
|
|
|
|
let out = utils::add_link(req);
|
|
|
|
if out.0 {
|
|
|
|
println!("ok{}", out.1);
|
|
|
|
HttpResponse::Ok().body(out.1)
|
|
|
|
} else {
|
|
|
|
println!("bad{}", out.1);
|
|
|
|
HttpResponse::BadRequest().body(out.1)
|
|
|
|
}
|
|
|
|
}
|
2023-04-03 13:26:23 +10:00
|
|
|
|
|
|
|
// Return all active links
|
2023-04-04 02:55:27 +10:00
|
|
|
#[get("/api/all")]
|
|
|
|
async fn getall() -> HttpResponse {
|
|
|
|
HttpResponse::Ok().body(utils::getall())
|
|
|
|
}
|
|
|
|
|
2023-04-04 04:50:23 +10:00
|
|
|
// Get the site URL
|
|
|
|
#[get("/api/siteurl")]
|
|
|
|
async fn siteurl() -> HttpResponse {
|
2023-04-04 06:46:22 +10:00
|
|
|
let site_url = env::var("site_url").unwrap_or("unset".to_string());
|
2023-04-04 04:50:23 +10:00
|
|
|
HttpResponse::Ok().body(site_url)
|
|
|
|
}
|
|
|
|
|
2023-04-03 13:26:23 +10:00
|
|
|
// 404 error page
|
|
|
|
#[get("/err/404")]
|
|
|
|
async fn error404() -> impl Responder {
|
|
|
|
NamedFile::open_async("./resources/404.html").await
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle a given shortlink
|
|
|
|
#[get("/{shortlink}")]
|
|
|
|
async fn link_handler(shortlink: web::Path<String>) -> impl Responder {
|
2023-04-04 08:40:37 +10:00
|
|
|
let shortlink_str = shortlink.to_string();
|
|
|
|
let longlink = utils::get_longurl(shortlink_str);
|
2023-04-04 06:46:22 +10:00
|
|
|
if longlink == "".to_string() {
|
2023-04-03 13:26:23 +10:00
|
|
|
Redirect::to("/err/404")
|
|
|
|
} else {
|
2023-04-04 08:41:52 +10:00
|
|
|
database::add_hit(shortlink.as_str());
|
2023-04-03 13:26:23 +10:00
|
|
|
Redirect::to(longlink).permanent()
|
|
|
|
}
|
2023-04-03 07:53:55 +10:00
|
|
|
}
|
|
|
|
|
2023-04-04 08:58:19 +10:00
|
|
|
// Delete a given shortlink
|
|
|
|
#[delete("/api/del/{shortlink}")]
|
|
|
|
async fn delete_link(shortlink: web::Path<String>) -> HttpResponse {
|
|
|
|
database::delete_link(shortlink.to_string());
|
|
|
|
HttpResponse::Ok().body("")
|
|
|
|
}
|
|
|
|
|
2023-04-03 13:26:23 +10:00
|
|
|
#[actix_web::main]
|
2023-04-03 07:53:55 +10:00
|
|
|
async fn main() -> std::io::Result<()> {
|
|
|
|
HttpServer::new(|| {
|
|
|
|
App::new()
|
2023-04-03 13:26:23 +10:00
|
|
|
.service(link_handler)
|
|
|
|
.service(error404)
|
2023-04-04 02:55:27 +10:00
|
|
|
.service(getall)
|
2023-04-04 04:50:23 +10:00
|
|
|
.service(siteurl)
|
2023-04-04 08:40:37 +10:00
|
|
|
.service(add_link)
|
2023-04-04 08:58:19 +10:00
|
|
|
.service(delete_link)
|
2023-04-03 07:53:55 +10:00
|
|
|
.service(Files::new("/", "./resources/").index_file("index.html"))
|
|
|
|
})
|
2023-04-03 13:26:23 +10:00
|
|
|
.bind(("0.0.0.0", 2000))?
|
2023-04-03 07:53:55 +10:00
|
|
|
.run()
|
|
|
|
.await
|
|
|
|
}
|