Replies: 1 comment 7 replies
-
use actix_web::{web, App, HttpRequest, HttpServer, Responder};
async fn greet(req: HttpRequest) -> impl Responder {
let name = req.match_info().get("name").unwrap_or("World");
format!("Hello {}!", &name)
}
#[actix_web::main]
async fn main() -> std::io::Result<()> {
HttpServer::new(|| {
let app = App::new().route("/", web::get().to(greet));
let app = if cfg!(feature = "on") {
app.configure(|cfg| {
cfg.route("/on", web::get().to(greet));
})
} else {
app
};
let app = app.route("/{name}", web::get().to(greet));
app
})
.bind(("127.0.0.1", 8080))?
.run()
.await
} |
Beta Was this translation helpful? Give feedback.
7 replies
Answer selected by
robjtede
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hey! is there a way that isn't too cumbersome to have some routes present only if compiled with a specific feature flag or stuff like that ?
we can't put a
#[cfg]
macro between two.route()
so i wonder if there is a clean way to do it.i thought of doing something like
but that doesn't seem clean ^^
also i haven't found a good solution for app_data().
maybe it is possible to define App outside http server ?
or should we use a big configure function that has the conditions here ?
thank you ! :)
Beta Was this translation helpful? Give feedback.
All reactions