blob: 6384f3abc3ba655b7bbbe039415658201a6f3e82 [file] [log] [blame]
Ed Tanous911ac312017-08-15 09:37:42 -07001#pragma once
2
3#include <experimental/filesystem>
4#include <fstream>
5#include <string>
6#include <crow/app.h>
Ed Tanous9dc2c4d2018-03-07 14:51:27 -08007#include <crow/http_codes.h>
Ed Tanous911ac312017-08-15 09:37:42 -07008#include <crow/http_request.h>
9#include <crow/http_response.h>
10#include <crow/routing.h>
11#include <boost/algorithm/string/replace.hpp>
12#include <boost/container/flat_set.hpp>
13
14namespace crow {
15namespace webassets {
16
17namespace filesystem = std::experimental::filesystem;
Ed Tanousba9f9a62017-10-11 16:40:35 -070018
19struct cmp_str {
20 bool operator()(const char* a, const char* b) const {
21 return std::strcmp(a, b) < 0;
22 }
23};
Ed Tanous911ac312017-08-15 09:37:42 -070024
25static boost::container::flat_set<std::string> routes;
26
27template <typename... Middlewares>
28void request_routes(Crow<Middlewares...>& app) {
Ed Tanousba9f9a62017-10-11 16:40:35 -070029 const static boost::container::flat_map<const char*, const char*, cmp_str>
30 content_types{
31 {{".css", "text/css;charset=UTF-8"},
32 {".html", "text/html;charset=UTF-8"},
33 {".js", "text/html;charset=UTF-8"},
34 {".png", "image/png;charset=UTF-8"},
35 {".woff", "application/x-font-woff"},
36 {".woff2", "application/x-font-woff2"},
Ed Tanous5b6a1f92018-02-02 11:05:21 -080037 {".gif", "image/gif"},
38 {".ico", "image/x-icon"},
Ed Tanousba9f9a62017-10-11 16:40:35 -070039 {".ttf", "application/x-font-ttf"},
40 {".svg", "image/svg+xml"},
41 {".eot", "application/vnd.ms-fontobject"},
42 {".xml", "application/xml"},
Ed Tanous257f5792018-03-17 14:40:09 -070043 {".jpg", "image/jpeg"},
44 {".jpeg", "image/jpeg"},
Ed Tanousba9f9a62017-10-11 16:40:35 -070045 // dev tools don't care about map type, setting to json causes
46 // browser to show as text
47 // https://stackoverflow.com/questions/19911929/what-mime-type-should-i-use-for-javascript-source-map-files
48 {".map", "application/json"}}};
Ed Tanous9dc2c4d2018-03-07 14:51:27 -080049 filesystem::path rootpath{"/usr/share/www/"};
50 filesystem::recursive_directory_iterator dir_iter(rootpath);
51
52 for (const filesystem::directory_entry& dir : dir_iter) {
53 filesystem::path absolute_path = dir.path();
54 filesystem::path relative_path{
55 absolute_path.string().substr(rootpath.string().size() - 1)};
Ed Tanous911ac312017-08-15 09:37:42 -070056 // make sure we don't recurse into certain directories
57 // note: maybe check for is_directory() here as well...
Ed Tanous5b6a1f92018-02-02 11:05:21 -080058
Ed Tanous911ac312017-08-15 09:37:42 -070059 if (filesystem::is_directory(dir)) {
60 // don't recurse into hidden directories or symlinks
61 if (boost::starts_with(dir.path().filename().string(), ".") ||
62 filesystem::is_symlink(dir)) {
63 dir_iter.disable_recursion_pending();
64 }
65 } else if (filesystem::is_regular_file(dir)) {
Ed Tanous5b6a1f92018-02-02 11:05:21 -080066 std::string extension = relative_path.extension();
Ed Tanous9dc2c4d2018-03-07 14:51:27 -080067 filesystem::path webpath = relative_path;
Ed Tanous5b6a1f92018-02-02 11:05:21 -080068 const char* content_encoding = nullptr;
Ed Tanous5b6a1f92018-02-02 11:05:21 -080069
70 if (extension == ".gz") {
Ed Tanous911ac312017-08-15 09:37:42 -070071 webpath = webpath.replace_extension("");
Ed Tanous5b6a1f92018-02-02 11:05:21 -080072 // Use the non-gzip version for determining content type
73 extension = webpath.extension().string();
74 content_encoding = "gzip";
Ed Tanous911ac312017-08-15 09:37:42 -070075 }
76
Ed Tanous9dc2c4d2018-03-07 14:51:27 -080077 if (boost::starts_with(webpath.filename().string(), "index.")) {
Ed Tanous911ac312017-08-15 09:37:42 -070078 webpath = webpath.parent_path();
Ed Tanous9dc2c4d2018-03-07 14:51:27 -080079 if (webpath.string().size() == 0 || webpath.string().back() != '/') {
Ed Tanous15aab542018-03-28 10:29:31 -070080 // insert the non-directory version of this path
81 routes.insert(webpath);
Ed Tanousba9f9a62017-10-11 16:40:35 -070082 webpath += "/";
83 }
Ed Tanous911ac312017-08-15 09:37:42 -070084 }
85
Ed Tanous9dc2c4d2018-03-07 14:51:27 -080086 routes.insert(webpath);
Ed Tanous911ac312017-08-15 09:37:42 -070087 const char* content_type = nullptr;
Ed Tanous911ac312017-08-15 09:37:42 -070088
Ed Tanous9dc2c4d2018-03-07 14:51:27 -080089 auto content_type_it = content_types.find(extension.c_str());
90 if (content_type_it == content_types.end()) {
91 CROW_LOG_ERROR << "Cannot determine content-type for " << webpath
92 << " with extension " << extension;
93 } else {
Ed Tanous5b6a1f92018-02-02 11:05:21 -080094 content_type = content_type_it->second;
95 }
96
Ed Tanous9dc2c4d2018-03-07 14:51:27 -080097 app.route_dynamic(webpath)(
Ed Tanous5b6a1f92018-02-02 11:05:21 -080098 [absolute_path, content_type, content_encoding](
99 const crow::request& req, crow::response& res) {
Ed Tanous911ac312017-08-15 09:37:42 -0700100 if (content_type != nullptr) {
Ed Tanous5b6a1f92018-02-02 11:05:21 -0800101 res.add_header("Content-Type", content_type);
Ed Tanous911ac312017-08-15 09:37:42 -0700102 }
Ed Tanousba9f9a62017-10-11 16:40:35 -0700103
Ed Tanous5b6a1f92018-02-02 11:05:21 -0800104 if (content_encoding != nullptr) {
105 res.add_header("Content-Encoding", content_encoding);
Ed Tanousba9f9a62017-10-11 16:40:35 -0700106 }
107
Ed Tanous911ac312017-08-15 09:37:42 -0700108 // res.set_header("Cache-Control", "public, max-age=86400");
Ed Tanous5b6a1f92018-02-02 11:05:21 -0800109 std::ifstream inf(absolute_path);
Ed Tanous911ac312017-08-15 09:37:42 -0700110 if (!inf) {
111 CROW_LOG_DEBUG << "failed to read file";
Ed Tanous9dc2c4d2018-03-07 14:51:27 -0800112 res.code = static_cast<int>(HttpRespCode::NOT_FOUND);
Ed Tanous5b6a1f92018-02-02 11:05:21 -0800113 res.code = static_cast<int>(HttpRespCode::INTERNAL_ERROR);
Ed Tanous911ac312017-08-15 09:37:42 -0700114 res.end();
115 return;
116 }
117
Ed Tanous5b6a1f92018-02-02 11:05:21 -0800118 res.body = {std::istreambuf_iterator<char>(inf),
119 std::istreambuf_iterator<char>()};
Ed Tanous911ac312017-08-15 09:37:42 -0700120 res.end();
121 });
122 }
123 }
Ed Tanous911ac312017-08-15 09:37:42 -0700124} // namespace webassets
Ed Tanous5b6a1f92018-02-02 11:05:21 -0800125} // namespace webassets
126} // namespace crow