feat(url): add server-side rendering feature with URL routing
This commit is contained in:
parent
fddec7f728
commit
7939c9e7b6
1 changed files with 17 additions and 0 deletions
17
src/main.rs
17
src/main.rs
|
@ -1,4 +1,6 @@
|
||||||
#[cfg(feature = "ssr")]
|
#[cfg(feature = "ssr")]
|
||||||
|
use actix_web::{web, HttpResponse};
|
||||||
|
|
||||||
#[actix_web::main]
|
#[actix_web::main]
|
||||||
async fn main() -> std::io::Result<()> {
|
async fn main() -> std::io::Result<()> {
|
||||||
use actix_files::Files;
|
use actix_files::Files;
|
||||||
|
@ -55,11 +57,26 @@ async fn main() -> std::io::Result<()> {
|
||||||
//.wrap(middleware::Compress::default())
|
//.wrap(middleware::Compress::default())
|
||||||
// Pass the database as shared state
|
// Pass the database as shared state
|
||||||
.app_data(web::Data::new(db))
|
.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)?
|
.bind(&addr)?
|
||||||
.run()
|
.run()
|
||||||
.await
|
.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")]
|
#[cfg(feature = "ssr")]
|
||||||
#[actix_web::get("favicon.ico")]
|
#[actix_web::get("favicon.ico")]
|
||||||
|
|
Loading…
Add table
Reference in a new issue