Skip to content

Commit 96fcb13

Browse files
authored
feat: add --serve-html-path option to example server (#1123)
1 parent 3e81246 commit 96fcb13

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

examples/server/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ usage: ./bin/sd-server [options]
66
Svr Options:
77
-l, --listen-ip <string> server listen ip (default: 127.0.0.1)
88
--listen-port <int> server listen port (default: 1234)
9+
--serve-html-path <string> path to HTML file to serve at root (optional)
910
-v, --verbose print extra info
1011
--color colors the logging tags according to level
1112
-h, --help show this help message and exit

examples/server/main.cpp

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ std::string iso_timestamp_now() {
104104
struct SDSvrParams {
105105
std::string listen_ip = "127.0.0.1";
106106
int listen_port = 1234;
107+
std::string serve_html_path;
107108
bool normal_exit = false;
108109
bool verbose = false;
109110
bool color = false;
@@ -115,7 +116,11 @@ struct SDSvrParams {
115116
{"-l",
116117
"--listen-ip",
117118
"server listen ip (default: 127.0.0.1)",
118-
&listen_ip}};
119+
&listen_ip},
120+
{"",
121+
"--serve-html-path",
122+
"path to HTML file to serve at root (optional)",
123+
&serve_html_path}};
119124

120125
options.int_options = {
121126
{"",
@@ -159,6 +164,11 @@ struct SDSvrParams {
159164
LOG_ERROR("error: listen_port should be in the range [0, 65535]");
160165
return false;
161166
}
167+
168+
if (!serve_html_path.empty() && !fs::exists(serve_html_path)) {
169+
LOG_ERROR("error: serve_html_path file does not exist: %s", serve_html_path.c_str());
170+
return false;
171+
}
162172
return true;
163173
}
164174

@@ -167,6 +177,7 @@ struct SDSvrParams {
167177
oss << "SDSvrParams {\n"
168178
<< " listen_ip: " << listen_ip << ",\n"
169179
<< " listen_port: \"" << listen_port << "\",\n"
180+
<< " serve_html_path: \"" << serve_html_path << "\",\n"
170181
<< "}";
171182
return oss.str();
172183
}
@@ -312,7 +323,18 @@ int main(int argc, const char** argv) {
312323

313324
// health
314325
svr.Get("/", [&](const httplib::Request&, httplib::Response& res) {
315-
res.set_content(R"({"ok":true,"service":"sd-cpp-http"})", "application/json");
326+
if (!svr_params.serve_html_path.empty()) {
327+
std::ifstream file(svr_params.serve_html_path);
328+
if (file) {
329+
std::string content((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
330+
res.set_content(content, "text/html");
331+
} else {
332+
res.status = 500;
333+
res.set_content("Error: Unable to read HTML file", "text/plain");
334+
}
335+
} else {
336+
res.set_content("Stable Diffusion Server is running", "text/plain");
337+
}
316338
});
317339

318340
// models endpoint (minimal)

0 commit comments

Comments
 (0)