33#include < fmt/format.h>
44#include < boost/program_options.hpp>
55#include < spdlog/spdlog.h>
6+ #include < fstream>
67
78
89using namespace drogon ;
@@ -11,6 +12,7 @@ using namespace drogon;
1112struct GlobalConfig
1213{
1314 std::filesystem::path invokePath;
15+ std::filesystem::path indexPath;
1416} globalConfig;
1517
1618bool invokeProcessing ( const std::filesystem::path& input, const std::filesystem::path& output )
@@ -66,7 +68,7 @@ Task<HttpResponsePtr> getResultHandler( HttpRequestPtr req )
6668 co_return resp;
6769}
6870
69- Task<HttpResponsePtr> submitTask ( HttpRequestPtr req )
71+ Task<HttpResponsePtr> submitTaskHandler ( HttpRequestPtr req )
7072{
7173 MultiPartParser filesUpload;
7274 if ( filesUpload.parse (req) != 0 || filesUpload.getFiles ().size () != 1 )
@@ -103,6 +105,19 @@ Task<HttpResponsePtr> submitTask( HttpRequestPtr req )
103105 co_return resp;
104106}
105107
108+ Task<HttpResponsePtr> rootHandler ( HttpRequestPtr req )
109+ {
110+ auto resp = HttpResponse::newHttpResponse ( HttpStatusCode::k200OK, CT_TEXT_HTML );
111+ if ( exists ( globalConfig.indexPath ) )
112+ {
113+ std::ifstream fin ( globalConfig.indexPath );
114+ std::stringstream buffer;
115+ buffer << fin.rdbuf ();
116+ resp->setBody ( buffer.str () );
117+ }
118+ co_return resp;
119+ }
120+
106121// Fix parsing std::filesystem::path with spaces (see https://github.com/boostorg/program_options/issues/69)
107122namespace boost
108123{
@@ -124,6 +139,7 @@ int main( int argc, char** argv )
124139 desc.add_options ()
125140 ( " help,h" , " Display help message" )
126141 ( " invokePath" , po::value ( &globalConfig.invokePath )->required (), " Path to the script that will be invoked" )
142+ ( " indexPath" , po::value ( &globalConfig.indexPath )->default_value ( {} ), " Path to the index.html" )
127143 ( " host" , po::value ( &host )->default_value ( " 127.0.0.1" ), " Host to bind to" )
128144 ( " port" , po::value ( &port )->default_value ( 7654 ), " Port to bind to" )
129145 ( " cert" , po::value ( &certPath )->default_value ( {} ), " Path to SSL certificate" )
@@ -150,7 +166,8 @@ int main( int argc, char** argv )
150166
151167 app ().registerHandler ( " /track_result" , std::function{ trackResultHandler } );
152168 app ().registerHandler ( " /get_result" , std::function{ getResultHandler } );
153- app ().registerHandler ( " /" , std::function{ submitTask }, { Post } );
169+ app ().registerHandler ( " /submit" , std::function{ submitTaskHandler }, {Post } );
170+ app ().registerHandler ( " /" , std::function{ rootHandler } );
154171
155172 const bool useSSL = exists ( certPath ) && exists ( keyPath );
156173 if ( useSSL && !app ().supportSSL () )
0 commit comments