Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| 3 | #include <experimental/filesystem> |
| 4 | #include <fstream> |
| 5 | #include <string> |
| 6 | #include <crow/app.h> |
| 7 | #include <crow/http_request.h> |
| 8 | #include <crow/http_response.h> |
| 9 | #include <crow/routing.h> |
| 10 | #include <boost/algorithm/string/replace.hpp> |
| 11 | #include <boost/container/flat_set.hpp> |
| 12 | |
| 13 | namespace crow { |
| 14 | namespace webassets { |
| 15 | |
| 16 | namespace filesystem = std::experimental::filesystem; |
Ed Tanous | ba9f9a6 | 2017-10-11 16:40:35 -0700 | [diff] [blame] | 17 | |
| 18 | struct cmp_str { |
| 19 | bool operator()(const char* a, const char* b) const { |
| 20 | return std::strcmp(a, b) < 0; |
| 21 | } |
| 22 | }; |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 23 | |
| 24 | static boost::container::flat_set<std::string> routes; |
| 25 | |
| 26 | template <typename... Middlewares> |
| 27 | void request_routes(Crow<Middlewares...>& app) { |
Ed Tanous | ba9f9a6 | 2017-10-11 16:40:35 -0700 | [diff] [blame] | 28 | const static boost::container::flat_map<const char*, const char*, cmp_str> |
| 29 | content_types{ |
| 30 | {{".css", "text/css;charset=UTF-8"}, |
| 31 | {".html", "text/html;charset=UTF-8"}, |
| 32 | {".js", "text/html;charset=UTF-8"}, |
| 33 | {".png", "image/png;charset=UTF-8"}, |
| 34 | {".woff", "application/x-font-woff"}, |
| 35 | {".woff2", "application/x-font-woff2"}, |
Ed Tanous | 5b6a1f9 | 2018-02-02 11:05:21 -0800 | [diff] [blame] | 36 | {".gif", "image/gif"}, |
| 37 | {".ico", "image/x-icon"}, |
Ed Tanous | ba9f9a6 | 2017-10-11 16:40:35 -0700 | [diff] [blame] | 38 | {".ttf", "application/x-font-ttf"}, |
| 39 | {".svg", "image/svg+xml"}, |
| 40 | {".eot", "application/vnd.ms-fontobject"}, |
| 41 | {".xml", "application/xml"}, |
Ed Tanous | 257f579 | 2018-03-17 14:40:09 -0700 | [diff] [blame] | 42 | {".jpg", "image/jpeg"}, |
| 43 | {".jpeg", "image/jpeg"}, |
Ed Tanous | ba9f9a6 | 2017-10-11 16:40:35 -0700 | [diff] [blame] | 44 | // dev tools don't care about map type, setting to json causes |
| 45 | // browser to show as text |
| 46 | // https://stackoverflow.com/questions/19911929/what-mime-type-should-i-use-for-javascript-source-map-files |
| 47 | {".map", "application/json"}}}; |
Ed Tanous | 9dc2c4d | 2018-03-07 14:51:27 -0800 | [diff] [blame] | 48 | filesystem::path rootpath{"/usr/share/www/"}; |
| 49 | filesystem::recursive_directory_iterator dir_iter(rootpath); |
| 50 | |
| 51 | for (const filesystem::directory_entry& dir : dir_iter) { |
| 52 | filesystem::path absolute_path = dir.path(); |
| 53 | filesystem::path relative_path{ |
| 54 | absolute_path.string().substr(rootpath.string().size() - 1)}; |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 55 | // make sure we don't recurse into certain directories |
| 56 | // note: maybe check for is_directory() here as well... |
Ed Tanous | 5b6a1f9 | 2018-02-02 11:05:21 -0800 | [diff] [blame] | 57 | |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 58 | if (filesystem::is_directory(dir)) { |
| 59 | // don't recurse into hidden directories or symlinks |
| 60 | if (boost::starts_with(dir.path().filename().string(), ".") || |
| 61 | filesystem::is_symlink(dir)) { |
| 62 | dir_iter.disable_recursion_pending(); |
| 63 | } |
| 64 | } else if (filesystem::is_regular_file(dir)) { |
Ed Tanous | 5b6a1f9 | 2018-02-02 11:05:21 -0800 | [diff] [blame] | 65 | std::string extension = relative_path.extension(); |
Ed Tanous | 9dc2c4d | 2018-03-07 14:51:27 -0800 | [diff] [blame] | 66 | filesystem::path webpath = relative_path; |
Ed Tanous | 5b6a1f9 | 2018-02-02 11:05:21 -0800 | [diff] [blame] | 67 | const char* content_encoding = nullptr; |
Ed Tanous | 5b6a1f9 | 2018-02-02 11:05:21 -0800 | [diff] [blame] | 68 | |
| 69 | if (extension == ".gz") { |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 70 | webpath = webpath.replace_extension(""); |
Ed Tanous | 5b6a1f9 | 2018-02-02 11:05:21 -0800 | [diff] [blame] | 71 | // Use the non-gzip version for determining content type |
| 72 | extension = webpath.extension().string(); |
| 73 | content_encoding = "gzip"; |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 74 | } |
| 75 | |
Ed Tanous | 9dc2c4d | 2018-03-07 14:51:27 -0800 | [diff] [blame] | 76 | if (boost::starts_with(webpath.filename().string(), "index.")) { |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 77 | webpath = webpath.parent_path(); |
Ed Tanous | 9dc2c4d | 2018-03-07 14:51:27 -0800 | [diff] [blame] | 78 | if (webpath.string().size() == 0 || webpath.string().back() != '/') { |
Ed Tanous | 15aab54 | 2018-03-28 10:29:31 -0700 | [diff] [blame] | 79 | // insert the non-directory version of this path |
| 80 | routes.insert(webpath); |
Ed Tanous | ba9f9a6 | 2017-10-11 16:40:35 -0700 | [diff] [blame] | 81 | webpath += "/"; |
| 82 | } |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 83 | } |
| 84 | |
Ed Tanous | 9dc2c4d | 2018-03-07 14:51:27 -0800 | [diff] [blame] | 85 | routes.insert(webpath); |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 86 | const char* content_type = nullptr; |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 87 | |
Ed Tanous | 9dc2c4d | 2018-03-07 14:51:27 -0800 | [diff] [blame] | 88 | auto content_type_it = content_types.find(extension.c_str()); |
| 89 | if (content_type_it == content_types.end()) { |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 90 | CROW_LOG_ERROR << "Cannot determine content-type for " << absolute_path |
Ed Tanous | 9dc2c4d | 2018-03-07 14:51:27 -0800 | [diff] [blame] | 91 | << " with extension " << extension; |
| 92 | } else { |
Ed Tanous | 5b6a1f9 | 2018-02-02 11:05:21 -0800 | [diff] [blame] | 93 | content_type = content_type_it->second; |
| 94 | } |
| 95 | |
Ed Tanous | 9dc2c4d | 2018-03-07 14:51:27 -0800 | [diff] [blame] | 96 | app.route_dynamic(webpath)( |
Ed Tanous | 5b6a1f9 | 2018-02-02 11:05:21 -0800 | [diff] [blame] | 97 | [absolute_path, content_type, content_encoding]( |
| 98 | const crow::request& req, crow::response& res) { |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 99 | if (content_type != nullptr) { |
Ed Tanous | 5b6a1f9 | 2018-02-02 11:05:21 -0800 | [diff] [blame] | 100 | res.add_header("Content-Type", content_type); |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 101 | } |
Ed Tanous | ba9f9a6 | 2017-10-11 16:40:35 -0700 | [diff] [blame] | 102 | |
Ed Tanous | 5b6a1f9 | 2018-02-02 11:05:21 -0800 | [diff] [blame] | 103 | if (content_encoding != nullptr) { |
| 104 | res.add_header("Content-Encoding", content_encoding); |
Ed Tanous | ba9f9a6 | 2017-10-11 16:40:35 -0700 | [diff] [blame] | 105 | } |
| 106 | |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 107 | // res.set_header("Cache-Control", "public, max-age=86400"); |
Ed Tanous | 5b6a1f9 | 2018-02-02 11:05:21 -0800 | [diff] [blame] | 108 | std::ifstream inf(absolute_path); |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 109 | if (!inf) { |
| 110 | CROW_LOG_DEBUG << "failed to read file"; |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 111 | res.result(boost::beast::http::status::internal_server_error); |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 112 | res.end(); |
| 113 | return; |
| 114 | } |
| 115 | |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 116 | res.body() = {std::istreambuf_iterator<char>(inf), |
Ed Tanous | 5b6a1f9 | 2018-02-02 11:05:21 -0800 | [diff] [blame] | 117 | std::istreambuf_iterator<char>()}; |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 118 | res.end(); |
| 119 | }); |
| 120 | } |
| 121 | } |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 122 | } // namespace webassets |
Ed Tanous | 5b6a1f9 | 2018-02-02 11:05:21 -0800 | [diff] [blame] | 123 | } // namespace webassets |
| 124 | } // namespace crow |