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;
2022using Path = std::string;
2123using 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
3236struct 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
3942std::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+
104137Arguments 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
148174int 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) {
0 commit comments