Dynamic routes #2194
Dynamic routes
#2194
-
I am trying to create routes dynamically but struggling to get them to work say i have a vec of config struct ImgRoute {
path: String,
bucket: String
}
let img_route_configs = vec!(ImgRoute("foo", "foo_uploads"), ImgRoute("bar", "bar_uploads")) let mut img_router = Router::new();
for img_route_config in config.img_route_configs.iter() {
img_router = img_router
.route(&format!("{}", img_route_config.path), get(handle_img))
.with_state(BucketConfig {
bucket_name: &img_route_config.bucket
});
} let routes: Router = Router::new()
.route("/", get(index))
.nest("/img", img_router.layer(Extension(s3_client)))
.route("/health", get(health))
.fallback(handler_404)
axum::Server::bind(&addr)
.serve(routes.into_make_service())
.await
.unwrap(); but this gives an error. the type of router doesn't support into_make_service Is there a better way to achieve this ? |
Beta Was this translation helpful? Give feedback.
Answered by
davidpdrsn
Aug 23, 2023
Replies: 2 comments 1 reply
-
Does using let mut img_router = Router::new();
for img_route_config in config.img_route_configs.iter() {
img_router = img_router
.route(
&format!("{}", img_route_config.path),
get(handle_img).with_state(BucketConfig { bucket_name: &img_route_config.bucket })
);
} That uses |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
shrynx
This comment has been hidden.
This comment has been hidden.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Does using
with_state
onget
instead work?That uses
MethodRouter::with_state