drogon
drogon copied to clipboard
Aligned Static File Routes
Currently, if drogon checks a route to a static file folder and does not find it, it returns not found, the desired behavior would be for it to continue checking subsequent routes like nginx, giving priority to larger routes, example:
"/static/an- folder/to-file/" over "/static/" is also desired
I'm not quite following. Could you describe the situation in more detail? I'm not sure what you are refering by subsequent routes or Aligned.
I'm not quite following. Could you describe the situation in more detail? I'm not sure what you are refering by
subsequent routesorAligned.
example, there are 2 rules in the config.json locations, one has the uri_prefix: "/static/", and the other uri_prefix: "/static/images/pictures", and they have completely different aliases, the second has to have a higher search priority, and if nothing is found in it, instead of returning 404, it looks in /static/ alias and then, if nothing is found, returns 404
The search order is the same as the config locations array. If you want to search "/static/images/pictures" first, you should put it in front of "/static/".
A ordem de pesquisa é a mesma da matriz de locais de configuração. Se você quiser pesquisar "/static/images/pictures" primeiro, você deve colocá-lo antes de "/static/".
Okay, that answers the priority, but what about checking in the other folder?
It will only check the first matched url_prefix.
DIY a controller to do that.
class File_fallback : public drogon::HttpController<File_fallback> {
public:
METHOD_LIST_BEGIN
ADD_METHOD_VIA_REGEX(File_fallback::get_file, "/static/(.*)", Get);
METHOD_LIST_END
void get_file(const HttpRequestPtr &req,
std::function<void(const HttpResponsePtr &)> &&callback,
std::string path) const {
auto resolved = [&callback](std::filesystem::path p) {
auto resp =
HttpResponse::newFileResponse(std::filesystem::absolute(p.string()));
callback(resp);
};
using std::filesystem::exists;
if (exists(base_ / path)) {
resolved(base_ / path);
return;
}
// subsequent routes
for (auto fallback : fallbacks_) {
auto p = fallback / std::filesystem::path{path}.filename();
if (exists(p)) {
resolved(p);
return;
}
}
auto resp = HttpResponse::newNotFoundResponse();
callback(resp);
}
std::filesystem::path base_;
std::vector<std::filesystem::path> fallbacks_;
};