Skip to content

Commit ee3784f

Browse files
committed
Restructure static file serving
1 parent ac64f8f commit ee3784f

File tree

5 files changed

+95
-41
lines changed

5 files changed

+95
-41
lines changed

devserver/defs.bzl

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,36 @@
1-
def devserver(name, port, static_file, workspace_name, debug = False, data = []):
2-
# normalized_static_file = Label(native.repository_name() + "//" + native.package_name()).relative(static_file)
1+
def devserver(name, port, static_files, workspace_name, debug = False, data = []):
32
is_debug = 1 if debug else 0
3+
4+
files = []
5+
for path, pkg_set in static_files.items():
6+
print(path)
7+
pkg, file = pkg_set[0], pkg_set[1]
8+
norm_pkg = Label(native.repository_name() + "//" + native.package_name()).relative(pkg)
9+
pkg = str(norm_pkg)
10+
11+
if pkg[0] == ":":
12+
pkg = pkg[1:]
13+
elif pkg[0] == "@":
14+
pkg = pkg[pkg.find("//") + 2:]
15+
pkg = pkg[:pkg.find(":")]
16+
else:
17+
fail("Expected a package name starting with @ or :")
18+
19+
files.append(",".join([path, pkg, file]))
20+
21+
static_files_arg = ""
22+
for file in files:
23+
static_files_arg += "--static_files=" + file + " "
24+
425
native.cc_binary(
526
name = name,
627
srcs = [
728
Label("@rules_devserver//devserver:main"),
829
],
930
args = [
1031
"--port=%d" % port,
11-
"--static_file=%s" % static_file,
32+
static_files_arg,
1233
"--workspace_name=%s" % workspace_name,
13-
"--package_name=%s" % native.package_name(),
1434
"--debug=%d" % is_debug,
1535
],
1636
data = data + [Label("@rules_devserver//devserver:devserver_loader")],

devserver/main.cc

Lines changed: 61 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#include <iostream>
22
#include <map>
3+
#include <sstream>
4+
#include <vector>
35

46
// #include "external/rules_devserver/devserver/argparse/argparse.h"
57
// #include "external/rules_devserver/devserver/httplib/httplib.h"
@@ -20,7 +22,9 @@ using ::nlohmann::json;
2022
using Path = std::string;
2123
using FileContents = std::string;
2224

23-
bool DEBUG = false;
25+
using PathMap = std::map<Path, FileContents>;
26+
27+
bool DEBUG = true;
2428

2529
#define DEBUG_LOG(msg) \
2630
if (DEBUG) std::cout << msg << std::endl;
@@ -31,9 +35,8 @@ constexpr char kHost[] = "localhost";
3135

3236
struct Arguments {
3337
int32_t port;
34-
std::string static_file;
38+
std::vector<std::string> static_files;
3539
std::string workspace_name;
36-
std::string package_name;
3740
};
3841

3942
std::string GetFileContents(const Path &path) {
@@ -43,7 +46,7 @@ std::string GetFileContents(const Path &path) {
4346
return content;
4447
}
4548

46-
json ComputeManifest(const std::map<Path, FileContents> &path_to_contents) {
49+
json ComputeManifest(const PathMap &path_to_contents) {
4750
json manifest;
4851

4952
for (const auto &path_and_contents : path_to_contents) {
@@ -101,33 +104,58 @@ std::string GetStaticFileContents(const std::string &workspace_root,
101104
return static_file_contents;
102105
}
103106

107+
PathMap ComputePathMap(const std::string &workspace_root,
108+
const std::vector<std::string> &static_files) {
109+
PathMap path_map;
110+
111+
for (const auto &static_file : static_files) {
112+
std::stringstream test(static_file);
113+
std::string segment;
114+
std::vector<std::string> seglist;
115+
116+
while (std::getline(test, segment, ',')) {
117+
seglist.push_back(segment);
118+
}
119+
120+
const std::string path = seglist[0];
121+
const std::string package_name = seglist[1];
122+
const std::string file = seglist[2];
123+
124+
std::string contents =
125+
GetStaticFileContents(workspace_root, package_name, file);
126+
127+
if (file.find(".html") != std::string::npos) {
128+
contents = AddDevserverLoaderToStaticFileContents(contents);
129+
}
130+
131+
path_map[path] = contents;
132+
}
133+
134+
return path_map;
135+
}
136+
104137
Arguments ParseArguments(int argc, char **argv) {
105138
std::string workspace_name = kWorkspaceName;
106139
args::ArgumentParser parser("This is a test program.",
107140
"This goes after the options.");
108141
args::ValueFlag<int32_t> port(parser, "port", "Server port", {"port"},
109142
kDefaultPort);
110-
args::ValueFlag<std::string> static_file(
111-
parser, "static_file", "Index file to serve from top-level / route",
112-
{"static_file"});
143+
args::ValueFlagList<std::string> static_files(
144+
parser, "static_files", "Files to serve per route", {"static_files"});
113145
args::ValueFlag<std::string> workspace_name_arg(
114-
parser, "workspace_name", "Cgalling Bazel workspace name",
146+
parser, "workspace_name", "Calling Bazel workspace name",
115147
{"workspace_name"});
116-
args::ValueFlag<std::string> package_name(parser, "package_name",
117-
"Package name", {"package_name"});
118-
args::ValueFlag<bool> debug(parser, "debug", "Debug mode", {"debug"}, DEBUG);
119-
120-
// DEBUG = debug;
121-
std::cout << "DEBUG: " << DEBUG << std::endl;
122-
std::cout << "debug: " << debug << std::endl;
148+
args::ValueFlag<bool> debug(parser, "debug", "Debug mode", {"debug"});
123149

124150
parser.ParseCLI(argc, argv);
125151

126152
if (port) {
127153
DEBUG_LOG("port: " << args::get(port));
128154
}
129-
if (static_file) {
130-
DEBUG_LOG("static_file: " << args::get(static_file));
155+
if (static_files) {
156+
for (const auto &static_file : args::get(static_files)) {
157+
DEBUG_LOG("static_file: " << static_file);
158+
}
131159
}
132160
if (workspace_name_arg) {
133161
if (args::get(workspace_name_arg) != "@" &&
@@ -137,12 +165,10 @@ Arguments ParseArguments(int argc, char **argv) {
137165

138166
DEBUG_LOG("workspace_name: " << workspace_name);
139167
}
140-
if (package_name) {
141-
DEBUG_LOG("package_name: " << args::get(package_name));
142-
}
143168

144-
return Arguments{args::get(port), args::get(static_file), workspace_name,
145-
args::get(package_name)};
169+
const std::vector<std::string> static_files_list(args::get(static_files));
170+
171+
return Arguments{args::get(port), static_files_list, workspace_name};
146172
}
147173

148174
int main(int argc, char **argv) {
@@ -154,28 +180,28 @@ int main(int argc, char **argv) {
154180

155181
const Arguments args = ParseArguments(argc, argv);
156182
const int32_t port = args.port;
157-
const std::string package_name = args.package_name;
158-
const std::string static_file = args.static_file;
183+
const std::vector<std::string> static_files = args.static_files;
159184
const std::string workspace_name = args.workspace_name;
160185

161186
const std::string workspace_root = runfiles->Rlocation(workspace_name + "/");
162187
DEBUG_LOG("workspace_root: " << workspace_root << "\n\n");
163188

164-
std::string static_file_contents =
165-
GetStaticFileContents(workspace_root, package_name, static_file);
166-
static_file_contents =
167-
AddDevserverLoaderToStaticFileContents(static_file_contents);
189+
const PathMap path_map = ComputePathMap(workspace_root, static_files);
168190

169-
const std::map<Path, FileContents> path_to_contents = {
170-
{"/", static_file_contents}};
171-
json manifest = ComputeManifest(path_to_contents);
191+
json manifest = ComputeManifest(path_map);
172192
DEBUG_LOG("manifest: " << manifest.dump() << "\n\n");
173193

174-
svr.Get("/", [&static_file_contents](const httplib::Request &req,
175-
httplib::Response &res) {
176-
res.set_content(static_file_contents, "text/html");
177-
});
194+
for (auto &entry : path_map) {
195+
const std::string path = entry.first;
196+
const std::string contents = entry.second;
178197

198+
svr.Get(path.c_str(), [path, contents](const httplib::Request &req,
199+
httplib::Response &res) {
200+
DEBUG_LOG("path: " << path << "\n");
201+
DEBUG_LOG("contents: " << contents << "\n\n");
202+
res.set_content(contents, "text/html");
203+
});
204+
}
179205
svr.Get(
180206
"/devserver/devserver_loader.js",
181207
[&workspace_root](const httplib::Request &req, httplib::Response &res) {

examples/single_static_file/BUILD

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

3+
34
devserver(
45
name = "serve",
56
port = 8081,
67
workspace_name = "rules_devserver",
7-
static_file = "index.html",
8+
static_files = {
9+
"/": ["//examples/single_static_file/static:index", "index.html"]
10+
},
811
debug = True,
9-
data = ["index.html"]
12+
data = ["//examples/single_static_file/static:index"]
1013
)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
filegroup(
2+
name = "index",
3+
srcs = ["index.html"],
4+
visibility = ["//visibility:public"]
5+
)
File renamed without changes.

0 commit comments

Comments
 (0)