Skip to content

Commit 85b90be

Browse files
committed
Clean up devserver setup function
1 parent e97d188 commit 85b90be

File tree

1 file changed

+28
-12
lines changed

1 file changed

+28
-12
lines changed

devserver/main.cc

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,31 @@ std::string GetDevserverLoaderScriptContents(
4343
return devserver_loader_contents;
4444
}
4545

46+
std::string AddDevserverLoaderToStaticFileContents(
47+
const std::string &static_file_contents) {
48+
const std::regex re("<\\/head>");
49+
const std::string replacement =
50+
"<script src=\"/devserver_loader.js\"></script></head>";
51+
std::string static_file_contents_with_devserver_loader =
52+
std::regex_replace(static_file_contents, re, replacement);
53+
54+
DEBUG_LOG("static_file_contents: " << static_file_contents);
55+
return static_file_contents_with_devserver_loader;
56+
}
57+
58+
std::string GetStaticFileContents(const std::string &workspace_root,
59+
const std::string &package_name,
60+
const std::string &static_file) {
61+
const std::string static_file_path =
62+
workspace_root + package_name + "/" + static_file;
63+
DEBUG_LOG("static_file_path: " << static_file_path);
64+
65+
std::string static_file_contents;
66+
static_file_contents = GetFileContents(static_file_path);
67+
68+
return static_file_contents;
69+
}
70+
4671
Arguments ParseArguments(int argc, char **argv) {
4772
std::string workspace_name = kWorkspaceName;
4873
args::ArgumentParser parser("This is a test program.",
@@ -98,20 +123,11 @@ int main(int argc, char **argv) {
98123
const std::string workspace_root = runfiles->Rlocation(workspace_name + "/");
99124
DEBUG_LOG("workspace_root: " << workspace_root << "\n\n");
100125

101-
const std::string static_file_path =
102-
workspace_root + package_name + "/" + static_file;
103-
DEBUG_LOG("static_file_path: " << static_file_path);
104-
105-
std::string static_file_contents;
106-
static_file_contents = GetFileContents(static_file_path);
107-
108-
const std::regex re("<\\/head>");
109-
const std::string replacement =
110-
"<script src=\"/devserver_loader.js\"></script></head>";
126+
std::string static_file_contents =
127+
GetStaticFileContents(workspace_root, package_name, static_file);
111128
static_file_contents =
112-
std::regex_replace(static_file_contents, re, replacement);
129+
AddDevserverLoaderToStaticFileContents(static_file_contents);
113130

114-
DEBUG_LOG("static_file_contents: " << static_file_contents);
115131
svr.Get("/", [&static_file_contents](const httplib::Request &req,
116132
httplib::Response &res) {
117133
res.set_content(static_file_contents, "text/html");

0 commit comments

Comments
 (0)