mirror-chhoto-url/actix/src/main.rs

91 lines
2.4 KiB
Rust
Raw Normal View History

2023-04-03 18:50:23 +00:00
use std::env;
2023-04-03 03:26:23 +00:00
use actix_files::{Files, NamedFile};
use actix_web::{
2023-04-03 22:58:19 +00:00
delete, get, post,
2023-04-03 03:26:23 +00:00
web::{self, Redirect},
2023-04-03 16:55:27 +00:00
App, HttpResponse, HttpServer, Responder,
2023-04-03 03:26:23 +00:00
};
2023-04-03 23:52:17 +00:00
use rusqlite::Connection;
2023-04-03 03:26:23 +00:00
mod database;
mod utils;
2023-04-02 21:53:55 +00:00
2023-04-03 23:52:17 +00:00
// This struct represents state
struct AppState {
db: Connection,
}
2023-04-03 03:26:23 +00:00
// Define the routes
// Add new links
2023-04-03 22:40:37 +00:00
#[post("/api/new")]
2023-04-03 23:52:17 +00:00
async fn add_link(req: String, data: web::Data<AppState>) -> HttpResponse {
let out = utils::add_link(req, &data.db);
2023-04-03 22:40:37 +00:00
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 03:26:23 +00:00
// Return all active links
2023-04-03 16:55:27 +00:00
#[get("/api/all")]
2023-04-03 23:52:17 +00:00
async fn getall(data: web::Data<AppState>) -> HttpResponse {
HttpResponse::Ok().body(utils::getall(&data.db))
2023-04-03 16:55:27 +00:00
}
2023-04-03 18:50:23 +00:00
// Get the site URL
#[get("/api/siteurl")]
async fn siteurl() -> HttpResponse {
2023-04-03 20:46:22 +00:00
let site_url = env::var("site_url").unwrap_or("unset".to_string());
2023-04-03 18:50:23 +00:00
HttpResponse::Ok().body(site_url)
}
2023-04-03 03:26:23 +00: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}")]
2023-04-03 23:52:17 +00:00
async fn link_handler(shortlink: web::Path<String>, data: web::Data<AppState>) -> impl Responder {
2023-04-03 22:40:37 +00:00
let shortlink_str = shortlink.to_string();
2023-04-03 23:52:17 +00:00
let longlink = utils::get_longurl(shortlink_str, &data.db);
2023-04-03 20:46:22 +00:00
if longlink == "".to_string() {
2023-04-03 03:26:23 +00:00
Redirect::to("/err/404")
} else {
2023-04-03 23:52:17 +00:00
database::add_hit(shortlink.as_str(), &data.db);
2023-04-03 03:26:23 +00:00
Redirect::to(longlink).permanent()
}
2023-04-02 21:53:55 +00:00
}
2023-04-03 22:58:19 +00:00
// Delete a given shortlink
#[delete("/api/del/{shortlink}")]
2023-04-03 23:52:17 +00:00
async fn delete_link(shortlink: web::Path<String>, data: web::Data<AppState>) -> HttpResponse {
database::delete_link(shortlink.to_string(), &data.db);
2023-04-03 22:58:19 +00:00
HttpResponse::Ok().body("")
}
2023-04-03 03:26:23 +00:00
#[actix_web::main]
2023-04-02 21:53:55 +00:00
async fn main() -> std::io::Result<()> {
HttpServer::new(|| {
App::new()
2023-04-03 23:52:17 +00:00
.app_data(web::Data::new(AppState {
db: database::open_db("./urls.sqlite".to_string()),
}))
2023-04-03 03:26:23 +00:00
.service(link_handler)
.service(error404)
2023-04-03 16:55:27 +00:00
.service(getall)
2023-04-03 18:50:23 +00:00
.service(siteurl)
2023-04-03 22:40:37 +00:00
.service(add_link)
2023-04-03 22:58:19 +00:00
.service(delete_link)
2023-04-02 21:53:55 +00:00
.service(Files::new("/", "./resources/").index_file("index.html"))
})
2023-04-03 03:26:23 +00:00
.bind(("0.0.0.0", 2000))?
2023-04-02 21:53:55 +00:00
.run()
.await
}