feat(url): add server-side rendering feature with URL routing

This commit is contained in:
ryan 2025-02-13 23:07:27 +03:00
parent fddec7f728
commit 7939c9e7b6

View file

@ -1,4 +1,6 @@
#[cfg(feature = "ssr")]
use actix_web::{web, HttpResponse};
#[actix_web::main]
async fn main() -> std::io::Result<()> {
use actix_files::Files;
@ -55,11 +57,26 @@ async fn main() -> std::io::Result<()> {
//.wrap(middleware::Compress::default())
// Pass the database as shared state
.app_data(web::Data::new(db))
// Register URL routing
.service(web::resource("/").route(web::get().to(index)))
.service(web::resource("/{url}").route(web::get().to(url_handler)))
})
.bind(&addr)?
.run()
.await
}
#[cfg(feature = "ssr")]
// Define the index handler
async fn index() -> HttpResponse {
HttpResponse::Ok().body("Welcome to CompareWare!")
}
#[cfg(feature = "ssr")]
// Define the URL handler
async fn url_handler(url: web::Path<String>) -> HttpResponse {
let url = url.into_inner();
// TO DO: Implement URL-based content storage and editing functionality
HttpResponse::Ok().body(format!("You are viewing the content at {}", url))
}
#[cfg(feature = "ssr")]
#[actix_web::get("favicon.ico")]