0

I'm trying to create a website that has multiple community feeds like one for bowling and another for poker like so: localhost:8088/communities/bowling & localhost:8088/communities/poker.

I use actix as my webserver operating under the Rust programming language.

Is there a way where all addresses operating under localhost:8088/communities encounter the same web files so that I can just have one main route?

Then store the additional header like /bowling or /poker so that I can commit to separate requests to the server for the relevant post feeds? Maybe I can save the additional header information in a javascript variable when the web page is called? -Like when I go to /poker, a variable named communityType gets set to poker. How would I do something like that?

Because there's no way anyone is making HTML pages for each of ~100s different communities.

Thanks for the help in advance!

Yun Jae Jung
  • 139
  • 1
  • 8
  • How is this question any better than [this](https://stackoverflow.com/questions/64187383/is-there-a-way-to-store-the-header-in-a-javascript-variable) one from 2 hours ago? -> [What topics can i ask about here?](https://stackoverflow.com/help/on-topic) + [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask) – Andreas Oct 03 '20 at 20:17
  • You are basically asking how dynamic content web sites work which is far too broad a question for Stack Overflow. – charlietfl Oct 03 '20 at 20:18
  • I'm asking how to use the actix file system to deliver the same web page under different headers, how is that a bad question? And also then saving the header into a variable. – Yun Jae Jung Oct 03 '20 at 20:21
  • As of now I found the solution: I can use path variables in actix so that different calls end up being served the same webpage and I can use window.location.href and the replace() method to collect and qualify the URL to get the community name. Easily answerable. – Yun Jae Jung Oct 03 '20 at 21:40

1 Answers1

0

I'm not very familiar with this crate, but based on the docs it looks like you can use #[get("/route/this/function/will/support")] to define how a route is handled. Assuming that I wrote it correctly, this should respond with small message telling you which community route you are on when using.

use actix_web::{get, web, App, HttpServer, Responder};

#[get("/communities/{name}")]
async fn communities_route(web::Path(name): web::Path<String>) -> impl Responder {
    format!("This is the page for the {:} community!", name)
}

You could also expand it to have routes for say #[get("/communities/{name}/forums")] or #[get("/communities/{name}/{file}")] to handle common routes all communities have on a per community basis.

Edit:

It sounds like you also need to include .service(communities_route) in your main function when initializing the App to use #[get(x)]. You also have better control over how the route is handled if you configure the services directly.

Here is one snippet from their hello world example. It looks like it prints the request on the server so long as you visit any route other than "/index.html".

async fn index(req: HttpRequest) -> &'static str {
    println!("REQ: {:?}", req);
    "Hello world!"
}

App::new()
.service(web::resource("/index.html").to(|| async { "Hello world!" }))
.service(web::resource("/").to(index))

I recommend looking through their examples on github. It looks like they have a bunch of concise examples for many different use cases.

Locke
  • 7,626
  • 2
  • 21
  • 41
  • This is more or less it except in order to serve the webpage as well, placing the path in a .service is needed. And then working with javascript to send a secondary request with the known name of the community. (Found the solution a few minutes ago) But thank you for the help anyway. – Yun Jae Jung Oct 03 '20 at 21:47
  • What is the point of the procedural macro then? It sounds like `.service()` more or less does the same thing. – Locke Oct 03 '20 at 21:50
  • The macro is just a secondary method to have an endpoint for a route. You could do App::new() .route("/web-address", web::get().to(function-name)) and you could replace the macro, they just offer multiple ways to do the same thing essentially. – Yun Jae Jung Oct 03 '20 at 21:53