Skip to content

Commit 7c531be

Browse files
committed
Optionally, return a static html file on the root endpoint.
1 parent 1facb9f commit 7c531be

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

main.cpp

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <fmt/format.h>
44
#include <boost/program_options.hpp>
55
#include <spdlog/spdlog.h>
6+
#include <fstream>
67

78

89
using namespace drogon;
@@ -11,6 +12,7 @@ using namespace drogon;
1112
struct GlobalConfig
1213
{
1314
std::filesystem::path invokePath;
15+
std::filesystem::path indexPath;
1416
} globalConfig;
1517

1618
bool 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)
107122
namespace 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() )

readme.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ It is useful when you have an early-stage prototype, and you want to share it wi
77

88
## Workflow
99

10-
- Accepts `POST` request with a single `.zip` file to the root endpoint `/`
10+
- Accepts `POST` request with a single `.zip` file to the endpoint `/submit`
1111
- Displays the URL to track the task if it was accepted (`/track_result?task=...`)
1212
- When processing is finished, the track page displays the URL to download result (`get_result?task=...`)
1313

@@ -27,6 +27,7 @@ $ simple_inference_server --help
2727
Simple Inference Server:
2828
-h [ --help ] Display help message
2929
--invokePath arg Path to the script that will be invoked
30+
--indexPath arg (="") Path to the index.html
3031
--host arg (=127.0.0.1) Host to bind to
3132
--port arg (=7654) Port to bind to
3233
--cert arg (="") Path to SSL certificate
@@ -64,7 +65,7 @@ The simplest web form that may serve as an interface to this server (provided th
6465
</head>
6566

6667
<body>
67-
<form method="post" enctype="multipart/form-data" action="http://localhost:7654">
68+
<form method="post" enctype="multipart/form-data" action="http://localhost:7654/submit">
6869
<p>
6970
<input type="file" name="file" accept="application/zip">
7071
</p>

0 commit comments

Comments
 (0)