Skip to content

Commit aa845ef

Browse files
committed
Static file stuff
1 parent b41ad57 commit aa845ef

File tree

4 files changed

+22
-14
lines changed

4 files changed

+22
-14
lines changed

devserver/defs.bzl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
def devserver(name, port, static_file, data = []):
2-
normalized_static_file = Label(native.repository_name() + "//" + native.package_name()).relative(static_file)
2+
# normalized_static_file = Label(native.repository_name() + "//" + native.package_name()).relative(static_file)
33
native.cc_binary(
44
name = name,
55
srcs = [
@@ -8,7 +8,9 @@ def devserver(name, port, static_file, data = []):
88
],
99
args = [
1010
"--port=%d" % port,
11-
"--static_file=%s" % normalized_static_file
11+
"--static_file=%s" % static_file,
12+
"--workspace_name=%s" % native.repository_name(),
13+
"--package_name=%s" % native.package_name(),
1214
],
1315
data = data,
1416
deps = ["@bazel_tools//tools/cpp/runfiles"],

devserver/main.cc

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,39 @@
1+
#include <iostream>
2+
13
#include "devserver/httplib/httplib.h"
24
#include "tools/cpp/runfiles/runfiles.h"
35

46
using bazel::tools::cpp::runfiles::Runfiles;
57

8+
constexpr char kWorkspaceName[] = "rules_devserver";
9+
constexpr char kDefaultPort[] = "8080";
10+
611
int main(int argc, char **argv) {
712
httplib::Server svr;
8-
std::string port = "8080";
13+
std::string port = std::string(kDefaultPort);
14+
std::string static_file;
915

1016
std::string runfiles_error;
1117
std::unique_ptr<Runfiles> runfiles(
1218
Runfiles::Create(argv[0], BAZEL_CURRENT_REPOSITORY, &runfiles_error));
1319

14-
std::string path = runfiles->Rlocation("devserver/");
20+
std::string path = runfiles->Rlocation("rules_devserver/");
1521
std::cout << "path: " << path << std::endl;
1622

17-
// print all args
1823
for (int i = 0; i < argc; i++) {
1924
std::cout << "argv[" << i << "] = " << argv[i] << std::endl;
20-
}
2125

22-
for (int i = 0; i < argc; i++) {
2326
if (std::string(argv[i]).rfind("--port", 0) != std::string::npos) {
2427
port = std::string(argv[i]).substr(7);
2528
}
29+
30+
if (std::string(argv[i]).rfind("--static_file", 0) != std::string::npos) {
31+
static_file = std::string(argv[i]).substr(14);
32+
}
2633
}
2734

35+
std::cout << "static_file:" << static_file << std::endl;
36+
2837
svr.Get("/", [](const httplib::Request &req, httplib::Response &res) {
2938
res.set_content("Hello World!", "text/html");
3039
});

examples/WORKSPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
workspace(name = "devserver_examples")

examples/single_static_file/BUILD

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
11
load("//devserver:defs.bzl", "devserver")
22

3-
filegroup(
4-
name = "index",
5-
srcs = ["index.html"],
6-
visibility = ["//visibility:public"],
7-
)
3+
# exports_files(["index.html"])
84

95
devserver(
106
name = "serve",
117
port = 8081,
12-
static_file = ":index",
13-
data = [":index"],
8+
static_file = "index.html",
9+
data = ["index.html"]
1410
)

0 commit comments

Comments
 (0)