Skip to content

Commit 01646a5

Browse files
committed
Successfully reading static file
1 parent aa845ef commit 01646a5

File tree

1 file changed

+45
-9
lines changed

1 file changed

+45
-9
lines changed

devserver/main.cc

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,36 +7,72 @@ using bazel::tools::cpp::runfiles::Runfiles;
77

88
constexpr char kWorkspaceName[] = "rules_devserver";
99
constexpr char kDefaultPort[] = "8080";
10+
constexpr char kHost[] = "localhost";
11+
12+
#define DEBUG true
13+
#define DEBUG_LOG(msg) \
14+
if (DEBUG) std::cout << msg << std::endl;
15+
16+
std::string GetFileContents(const std::string &path) {
17+
std::ifstream ifs(path);
18+
std::string content((std::istreambuf_iterator<char>(ifs)),
19+
(std::istreambuf_iterator<char>()));
20+
return content;
21+
}
1022

1123
int main(int argc, char **argv) {
1224
httplib::Server svr;
13-
std::string port = std::string(kDefaultPort);
25+
26+
std::string port = kDefaultPort;
27+
std::string workspace_name = kWorkspaceName;
28+
std::string package_name;
1429
std::string static_file;
30+
std::string static_file_contents;
1531

1632
std::string runfiles_error;
1733
std::unique_ptr<Runfiles> runfiles(
1834
Runfiles::Create(argv[0], BAZEL_CURRENT_REPOSITORY, &runfiles_error));
1935

20-
std::string path = runfiles->Rlocation("rules_devserver/");
21-
std::cout << "path: " << path << std::endl;
22-
2336
for (int i = 0; i < argc; i++) {
24-
std::cout << "argv[" << i << "] = " << argv[i] << std::endl;
25-
2637
if (std::string(argv[i]).rfind("--port", 0) != std::string::npos) {
2738
port = std::string(argv[i]).substr(7);
2839
}
2940

3041
if (std::string(argv[i]).rfind("--static_file", 0) != std::string::npos) {
3142
static_file = std::string(argv[i]).substr(14);
3243
}
44+
45+
if (std::string(argv[i]).rfind("--package_name", 0) != std::string::npos) {
46+
package_name = std::string(argv[i]).substr(15);
47+
}
48+
49+
if (std::string(argv[i]).rfind("--workspace_name", 0) !=
50+
std::string::npos) {
51+
const std::string workspace_name_arg = std::string(argv[i]).substr(17);
52+
if (workspace_name_arg != "@" && !workspace_name_arg.empty()) {
53+
workspace_name = workspace_name_arg;
54+
}
55+
}
3356
}
3457

58+
std::cout << "workspace_name: " << workspace_name << std::endl;
59+
std::cout << "package_name: " << package_name << std::endl;
3560
std::cout << "static_file:" << static_file << std::endl;
3661

37-
svr.Get("/", [](const httplib::Request &req, httplib::Response &res) {
38-
res.set_content("Hello World!", "text/html");
62+
std::string path = runfiles->Rlocation(workspace_name + "/");
63+
std::cout << "path: " << path << std::endl;
64+
65+
std::cout << "\nport: " << port << std::endl;
66+
67+
const std::string static_file_path = path + package_name + "/" + static_file;
68+
std::cout << "static_file_path: " << static_file_path << std::endl;
69+
static_file_contents = GetFileContents(static_file_path);
70+
std::cout << "static_file_contents: " << static_file_contents << std::endl;
71+
72+
svr.Get("/", [&static_file_contents](const httplib::Request &req,
73+
httplib::Response &res) {
74+
res.set_content(static_file_contents, "text/html");
3975
});
4076

41-
svr.listen("localhost", std::stoi(port));
77+
svr.listen(kHost, std::stoi(port));
4278
}

0 commit comments

Comments
 (0)