Skip to content

conditional compilation routes and data. #2946

Answered by robjtede
alkeryn asked this question in Q&A
Discussion options

You must be logged in to vote
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
}

Replies: 1 comment 7 replies

Comment options

You must be logged in to vote
7 replies
@robjtede
Comment options

@robjtede
Comment options

@alkeryn
Comment options

@Cakeday
Comment options

@alkeryn
Comment options

Answer selected by robjtede
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants