Return index.html to the client requesting '/'

This commit is contained in:
antao 2019-05-07 23:19:26 +08:00
parent 52bc179b51
commit 66c6441c96
8 changed files with 52 additions and 14 deletions

View File

@ -60,6 +60,10 @@
"session_timeout": 0,
//document_root: Root path of HTTP document, defaut path is ./
"document_root": "./",
//home_page: Set the HTML file of the home page, the default value is "index.html"
//If there isn't any handler registered to the path "/", the home page file in the "document_root" is send to clients as a response
//to the request for "/".
"home_page": "index.html",
//upload_path: The path to save the uploaded file. "uploads" by default.
//If the path isn't prefixed with /, ./ or ../,
//it is relative path of document_root path

View File

@ -56,10 +56,14 @@
//is the number of CPU cores
"threads_num": 1,
//enable_session: False by default
"enable_session": true,
"enable_session": false,
"session_timeout": 0,
//document_root: Root path of HTTP document, defaut path is ./
"document_root": "./",
//home_page: Set the HTML file of the home page, the default value is "index.html"
//If there isn't any handler registered to the path "/", the home page file in the "document_root" is send to clients as a response
//to the request for "/".
"home_page": "index.html",
//upload_path: The path to save the uploaded file. "uploads" by default.
//If the path isn't prefixed with /, ./ or ../,
//it is relative path of document_root path

View File

@ -666,6 +666,15 @@ public:
*/
virtual void setClientMaxWebSocketMessageSize(size_t maxSize) = 0;
//Set the HTML file of the home page, the default value is "index.html"
/**
* If there isn't any handler registered to the path "/", the home page file in the "document_root"
* is send to clients as a response to the request for "/".
* NOTE:
* This operation can be performed by an option in the configuration file.
*/
virtual void setHomePage(const std::string &homePageFile) = 0;
#if USE_ORM
///Get a database client by @param name
/**

View File

@ -131,8 +131,8 @@ public:
/// @param data is the data displayed on the page.
/// For more details, see the wiki pages, the "View" section.
static HttpResponsePtr newHttpViewResponse(const std::string &viewName, const HttpViewData &data = HttpViewData());
/// Create a response that returns a 302 Found page, redirecting to another page located in the @param path.
static HttpResponsePtr newLocationRedirectResponse(const std::string &path);
/// Create a response that returns a 302 Found page, redirecting to another page located in the @param location.
static HttpResponsePtr newLocationResponse(const std::string &location);
/// Create a response that returns a file to the client.
/**
* @param fullPath is the full path to the file.

View File

@ -54,7 +54,7 @@ static bool bytesSize(std::string &sizeStr, size_t &size)
//64bit system
case 't':
case 'T':
size = (1024 * 1024 * 1024 * 1024);
size = (1024L * 1024L * 1024L * 1024L);
sizeStr.resize(sizeStr.length() - 1);
break;
#endif
@ -323,6 +323,7 @@ static void loadApp(const Json::Value &app)
{
exit(-1);
}
drogon::app().setHomePage(app.get("home_page", "index.html").asString());
}
static void loadDbClients(const Json::Value &dbClients)
{

View File

@ -90,6 +90,7 @@ public:
resp->setStatusCode(k404NotFound);
_custom404 = resp;
}
const HttpResponsePtr &getCustom404Page()
{
return _custom404;
@ -171,10 +172,13 @@ public:
virtual void setKeepaliveRequestsNumber(const size_t number) override { _keepaliveRequestsNumber = number; }
virtual void setPipeliningRequestsNumber(const size_t number) override { _pipeliningRequestsNumber = number; }
virtual void setGzipStatic(bool useGzipStatic) override { _gzipStaticFlag = useGzipStatic; }
bool getGzipStatic() const { return _gzipStaticFlag; }
virtual void setClientMaxBodySize(size_t maxSize) override { _clientMaxBodySize = maxSize; }
virtual void setClientMaxWebSocketMessageSize(size_t maxSize) override { _clientMaxWebSocketMessageSize = maxSize; }
size_t getClientMaxBodySize() { return _clientMaxBodySize; }
size_t getClientMaxWebSocketMessageSize() { return _clientMaxWebSocketMessageSize; }
virtual void setHomePage(const std::string &homePageFile) override { _homePageFile = homePageFile; }
const std::string &getHomePage() const { return _homePageFile; }
size_t getClientMaxBodySize() const { return _clientMaxBodySize; }
size_t getClientMaxWebSocketMessageSize() const { return _clientMaxWebSocketMessageSize; }
virtual std::vector<std::tuple<std::string, HttpMethod, std::string>> getHandlersInfo() const override;
size_t keepaliveRequestsNumber() const { return _keepaliveRequestsNumber; }
@ -301,6 +305,7 @@ private:
bool _gzipStaticFlag = true;
size_t _clientMaxBodySize = 1024 * 1024;
size_t _clientMaxWebSocketMessageSize = 128 * 1024;
std::string _homePageFile = "index.html";
int _staticFilesCacheTime = 5;
std::unordered_map<std::string, std::weak_ptr<HttpResponse>> _staticFilesCache;
std::mutex _staticFilesCacheMutex;

View File

@ -20,6 +20,23 @@
using namespace drogon;
namespace drogon
{
static void doWhenNoHandlerFound(const HttpRequestImplPtr &req,
const std::function<void(const HttpResponsePtr &)> &callback)
{
if (req->path() == "/" && !HttpAppFrameworkImpl::instance().getHomePage().empty())
{
auto resp = drogon::HttpResponse::newLocationResponse("/" + HttpAppFrameworkImpl::instance().getHomePage());
callback(resp);
return;
}
auto resp = drogon::HttpResponse::newNotFoundResponse();
callback(resp);
}
} // namespace drogon
void HttpControllersRouter::init(const std::vector<trantor::EventLoop *> &ioLoops)
{
std::string regString;
@ -264,16 +281,14 @@ void HttpControllersRouter::route(const HttpRequestImplPtr &req,
}
else
{
//No controller found
auto res = drogon::HttpResponse::newNotFoundResponse();
callback(res);
//No handler found
doWhenNoHandlerFound(req, callback);
}
}
else
{
//No controller found
auto res = drogon::HttpResponse::newNotFoundResponse();
callback(res);
//No handler found
doWhenNoHandlerFound(req, callback);
}
}

View File

@ -66,11 +66,11 @@ HttpResponsePtr HttpResponse::newNotFoundResponse()
return notFoundResp;
}
}
HttpResponsePtr HttpResponse::newLocationRedirectResponse(const std::string &path)
HttpResponsePtr HttpResponse::newLocationResponse(const std::string &location)
{
auto res = std::make_shared<HttpResponseImpl>();
res->setStatusCode(k302Found);
res->redirect(path.c_str());
res->redirect(location);
return res;
}