Multiple mg_http_serve_dir at different URLs #2973
-
I was wondering if it would be possible to serve multiples directories at different URLs I have this ev_handler function in my #define MG_API_HEADERS "Content-Type: application/json\r\nAccess-Control-Allow-Origin: *\r\nAccess-Control-Allow-Methods: *\r\n"
static void ev_handler(struct mg_connection *c, int ev, void *ev_data) {
if (ev != MG_EV_HTTP_MSG) return;
struct mg_http_message *hm = (struct mg_http_message *) ev_data;
if (mg_strcmp(hm->method, mg_str("OPTIONS")) == 0) {
return mg_http_reply(c, 204, MG_API_HEADERS"Access-Control-Allow-Headers: *\r\n\r\n", "{\"res\": \"No Content\"}");
}
if(mg_match(hm->uri, mg_str("/api/#"), NULL)) {
return mg_http_reply(c, 200, MG_API_HEADERS, "{\"test\": \"%s\"}\n", "test");
}
if(mg_match(hm->uri, mg_str("/admin/#"), NULL)) {
struct mg_http_serve_opts opts = {
.root_dir = "/ui/dist",
.fs = &mg_fs_packed
};
return mg_http_serve_dir(c, ev_data, &opts);
}
else {
struct mg_http_serve_opts opts = {
.root_dir = "./public",
};
return mg_http_serve_dir(c, hm, &opts);
}
} Whenever I try to reach Do you know if it's possible to achieve this ? Note that I'm using Embedded filesystem in one of the Thank you ! |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 4 replies
-
Beta Was this translation helpful? Give feedback.
-
I think I found a way to achieve what I wanted thanks to @smilediver and #1289 I'm not sure how but changing the uri of the mg_http_message fix the error 404 Here the if(mg_match(hm->uri, mg_str("/admin/#"), NULL)) {
hm->uri.buf += 6;
hm->uri.len -= 6;
opts.root_dir = "./public";
} else {
opts.root_dir = "/ui/dist";
opts.fs = &mg_fs_packed;
}
mg_http_serve_dir(c, hm, &opts); and match the base url (here <base href="/admin/"> |
Beta Was this translation helpful? Give feedback.
-
What you want can be perfectly done if you just read the docs and do as the examples suggest |
Beta Was this translation helpful? Give feedback.
Please see our documentation, and follow the guidelines in our tutorials.
Nothing prevents that
https://mongoose.ws/documentation/tutorials/http/http-server/
https://mongoose.ws/documentation/#mg_http_serve_dir
https://mongoose.ws/documentation/#struct-mg_http_serve_opts
https://gurubase.io/g/mongoose/mongoose-serve-multiple-directories-different-urls
other path:
https://mongoose.ws/documentation/#mg_match
https://gurubase.io/g/mongoose/serve-multiple-directories-with-mg-match