8 #include <oatpp/web/server/api/ApiController.hpp>
9 #include <oatpp/parser/json/mapping/ObjectMapper.hpp>
10 #include <oatpp/core/macro/codegen.hpp>
11 #include <oatpp/core/macro/component.hpp>
16 #include <unordered_map>
18 #include OATPP_CODEGEN_BEGIN(ApiController)
24 : oatpp::web::server::api::ApiController(objectMapper)
35 ENDPOINT(
"GET",
"/", serveRoot, REQUEST(std::shared_ptr<IncomingRequest>, request))
41 ENDPOINT(
"GET",
"/*", serveAll, REQUEST(std::shared_ptr<IncomingRequest>, request))
43 const oatpp::String tail = request->getPathTail();
44 const std::string
filePath = (tail && !tail->empty()) ? tail->c_str() :
"index.html";
53 static const std::unordered_map<std::string, std::string> types = {
54 {
".html",
"text/html"},
55 {
".htm",
"text/html"},
57 {
".js",
"application/javascript"},
58 {
".json",
"application/json"},
59 {
".png",
"image/png"},
60 {
".jpg",
"image/jpeg"},
61 {
".jpeg",
"image/jpeg"},
62 {
".gif",
"image/gif"},
63 {
".svg",
"image/svg+xml"},
64 {
".ico",
"image/x-icon"},
65 {
".txt",
"text/plain"},
68 auto dot = filename.rfind(
'.');
69 if (
dot != std::string::npos)
71 auto it = types.find(filename.substr(
dot));
72 if (it != types.end())
74 return it->second.c_str();
77 return "application/octet-stream";
86 if (name.empty() || name.front() ==
'/' || name.front() ==
'\\')
88 if (name.find(
'\\') != std::string::npos)
90 std::istringstream
stream(name);
92 while (std::getline(stream, segment,
'/'))
104 return createResponse(Status::CODE_400,
"Bad Request");
108 std::ifstream file(path, std::ios::binary);
111 return createResponse(Status::CODE_404,
"Not Found");
114 std::ostringstream ss;
116 std::string content = ss.str();
118 auto response = createResponse(Status::CODE_200, content.c_str());
124 #include OATPP_CODEGEN_END(ApiController)
std::string myPublicDir
Definition: staticFileController.h:49
std::shared_ptr< OutgoingResponse > serveFile(const std::string &filename)
Definition: staticFileController.h:100
const char * filename
Definition: ioapi.h:38
DtStaticFileController(const std::shared_ptr< ObjectMapper > &objectMapper, const std::string &publicDir)
Definition: staticFileController.h:23
Definition: staticFileController.h:20
static oatpp::String contentTypeFor(const std::string &filename)
Definition: staticFileController.h:51
const std::string filePath
Definition: staticFileController.h:44
static bool isSafeName(const std::string &name)
Definition: staticFileController.h:84
voidpf stream
Definition: ioapi.h:39
return serveFile(filePath)
DT_DLL_vrfutil double dot(double x[3], double y[3])