Basic redirection working

This commit is contained in:
SinTan1729
2023-04-02 22:26:23 -05:00
parent 0e97516759
commit b9d76b6734
7 changed files with 139 additions and 27 deletions

View File

@@ -1,19 +1,44 @@
use actix_files::Files;
use actix_web::{get, web, App, HttpServer, Responder};
use actix_files::{Files, NamedFile};
use actix_web::{
get,
web::{self, Redirect},
App, HttpServer, Responder,
};
mod database;
mod utils;
#[get("/hello/{name}")]
async fn greet(name: web::Path<String>) -> impl Responder {
format!("Hello {name}!")
// Define the routes
// Add new links
// Return all active links
// 404 error page
#[get("/err/404")]
async fn error404() -> impl Responder {
NamedFile::open_async("./resources/404.html").await
}
#[actix_web::main] // or #[tokio::main]
// Handle a given shortlink
#[get("/{shortlink}")]
async fn link_handler(shortlink: web::Path<String>) -> impl Responder {
let longlink = utils::get_longurl(shortlink);
if longlink == String::from("") {
Redirect::to("/err/404")
} else {
Redirect::to(longlink).permanent()
}
}
#[actix_web::main]
async fn main() -> std::io::Result<()> {
HttpServer::new(|| {
App::new()
.service(greet)
.service(link_handler)
.service(error404)
.service(Files::new("/", "./resources/").index_file("index.html"))
})
.bind(("127.0.0.1", 2000))?
.bind(("0.0.0.0", 2000))?
.run()
.await
}