drogonframework / drogon

Drogon: A C++14/17/20 based HTTP web application framework running on Linux/macOS/Unix/Windows
MIT License
11.44k stars 1.1k forks source link

Drogon refusing to serve static JSON file #1840

Closed okvik closed 10 months ago

okvik commented 10 months ago

I'm trying to get a static manifest.json file served from my application, however Drogon refuses to abide and serves a 404 instead.

I'm aware that by default Drogon doesn't serve .json files, so I attempted the following prior to running the "app":

  drogon::app().setFileTypes({"gif", "png", "jpg", "js", "css", "html",
            "ico", "swf", "xap", "apk", "cur", "xml", "webp", "svg", "json"});
  drogon::app().registerCustomExtensionMime("json", "application/json");

Which is the default set of extensions according to an example configuration file plus "json". Unfortunately this doesn't seem to help. For the record, all the other filetypes mentioned here work fine.

I'm running a recent Drogon master branch. Using the configuration file is not an option for my use-case.

Desktop (please complete the following information):

an-tao commented 10 months ago

@okvik your code works fine in my linux server. Did you load configuration file some where?

okvik commented 10 months ago

Did you load configuration file some where?

Not that I'm aware. This is the entire code that I'm using to start up Drogon, the rest are normal controllers.

  drogon::app().setDocumentRoot(WEB_DOCUMENT_ROOT);
  drogon::app().setUploadPath("/tmp");
  drogon::app().setFileTypes({"gif", "png", "jpg", "js", "css", "html",
            "ico", "swf", "xap", "apk", "cur", "xml",
            "webp", "svg", "json"});
  drogon::app().registerCustomExtensionMime("json", "application/json");

  auto pose_ctrl = std::make_shared<PoseCtrl>(program->rdata);
  drogon::app().registerController(pose_ctrl);
  auto jpose_ctrl = std::make_shared<JPoseCtrl>(program->rdata);
  drogon::app().registerController(jpose_ctrl);
  auto robot_ctrl = std::make_shared<RobotCtrl>(robot);
  drogon::app().registerController(robot_ctrl);
  std::thread web_thread([&] {
    drogon::app().addListener("0.0.0.0", 8080).run();
  });
  web_thread.detach();
an-tao commented 10 months ago

Which version of drogon did you use? Please comfirm the the static json file is in your WEB_DOCUMENT_ROOT

okvik commented 10 months ago

I'm using v1.9.0.

$ ls -l WEB_DOCUMENT_ROOT

  -rw-r--r-- 1 kvik kvik   300 Nov  7 21:14 manifest.css
  -rw-r--r-- 1 kvik kvik   186 Nov  7 20:03 manifest.json
  drwxr-xr-x 5 kvik kvik  4.0K Nov  7 20:35 static
$ curl localhost:8080/manifest.json
<html>
<head><title>404 Not Found</title></head>
<body bgcolor="white" text="black">
<center><h1>404 Not Found</h1></center>
<hr><center>drogon/1.9.0</center>
</body>
</html>
<!-- a padding to disable MSIE and Chrome friendly error page -->
<!-- a padding to disable MSIE and Chrome friendly error page -->
<!-- a padding to disable MSIE and Chrome friendly error page -->
<!-- a padding to disable MSIE and Chrome friendly error page -->
<!-- a padding to disable MSIE and Chrome friendly error page -->
<!-- a padding to disable MSIE and Chrome friendly error page -->

$ curl localhost:8080/manifest.css
{
    "name": "",
    "short_name": "",
    "description": "",
    "display": "fullscreen",
    "start_url": "/",
    "icons": [
        {
            "src": "/static/img/icon.svg",
            "sizes": "any"
        }
    ]
}

As you can see, when renamed to .css extension it works just fine.

okvik commented 10 months ago

Oh, crap, I just realized my stupid mistake. I'm building two sibling applications, but I was only changing the configuration of one and testing with another... so of course it didn't work.

Sorry for wasting your time...