blob: e44c04c8fa1765c4e76fa68607c3be3a3461bf39 [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"},
43 // dev tools don't care about map type, setting to json causes
44 // browser to show as text
45 // https://stackoverflow.com/questions/19911929/what-mime-type-should-i-use-for-javascript-source-map-files
46 {".map", "application/json"}}};
Ed Tanous9dc2c4d2018-03-07 14:51:27 -080047 filesystem::path rootpath{"/usr/share/www/"};
48 filesystem::recursive_directory_iterator dir_iter(rootpath);
49
50 for (const filesystem::directory_entry& dir : dir_iter) {
51 filesystem::path absolute_path = dir.path();
52 filesystem::path relative_path{
53 absolute_path.string().substr(rootpath.string().size() - 1)};
Ed Tanous911ac312017-08-15 09:37:42 -070054 // make sure we don't recurse into certain directories
55 // note: maybe check for is_directory() here as well...
Ed Tanous5b6a1f92018-02-02 11:05:21 -080056
Ed Tanous911ac312017-08-15 09:37:42 -070057 if (filesystem::is_directory(dir)) {
58 // don't recurse into hidden directories or symlinks
59 if (boost::starts_with(dir.path().filename().string(), ".") ||
60 filesystem::is_symlink(dir)) {
61 dir_iter.disable_recursion_pending();
62 }
63 } else if (filesystem::is_regular_file(dir)) {
Ed Tanous5b6a1f92018-02-02 11:05:21 -080064 std::string extension = relative_path.extension();
Ed Tanous9dc2c4d2018-03-07 14:51:27 -080065 filesystem::path webpath = relative_path;
Ed Tanous5b6a1f92018-02-02 11:05:21 -080066 const char* content_encoding = nullptr;
Ed Tanous5b6a1f92018-02-02 11:05:21 -080067
68 if (extension == ".gz") {
Ed Tanous911ac312017-08-15 09:37:42 -070069 webpath = webpath.replace_extension("");
Ed Tanous5b6a1f92018-02-02 11:05:21 -080070 // Use the non-gzip version for determining content type
71 extension = webpath.extension().string();
72 content_encoding = "gzip";
Ed Tanous911ac312017-08-15 09:37:42 -070073 }
74
Ed Tanous9dc2c4d2018-03-07 14:51:27 -080075 if (boost::starts_with(webpath.filename().string(), "index.")) {
Ed Tanous911ac312017-08-15 09:37:42 -070076 webpath = webpath.parent_path();
Ed Tanous9dc2c4d2018-03-07 14:51:27 -080077 if (webpath.string().size() == 0 || webpath.string().back() != '/') {
Ed Tanousba9f9a62017-10-11 16:40:35 -070078 webpath += "/";
79 }
Ed Tanous911ac312017-08-15 09:37:42 -070080 }
81
Ed Tanous9dc2c4d2018-03-07 14:51:27 -080082 routes.insert(webpath);
Ed Tanous911ac312017-08-15 09:37:42 -070083 const char* content_type = nullptr;
Ed Tanous911ac312017-08-15 09:37:42 -070084
Ed Tanous9dc2c4d2018-03-07 14:51:27 -080085 auto content_type_it = content_types.find(extension.c_str());
86 if (content_type_it == content_types.end()) {
87 CROW_LOG_ERROR << "Cannot determine content-type for " << webpath
88 << " with extension " << extension;
89 } else {
Ed Tanous5b6a1f92018-02-02 11:05:21 -080090 content_type = content_type_it->second;
91 }
92
Ed Tanous9dc2c4d2018-03-07 14:51:27 -080093 app.route_dynamic(webpath)(
Ed Tanous5b6a1f92018-02-02 11:05:21 -080094 [absolute_path, content_type, content_encoding](
95 const crow::request& req, crow::response& res) {
Ed Tanous911ac312017-08-15 09:37:42 -070096 if (content_type != nullptr) {
Ed Tanous5b6a1f92018-02-02 11:05:21 -080097 res.add_header("Content-Type", content_type);
Ed Tanous911ac312017-08-15 09:37:42 -070098 }
Ed Tanousba9f9a62017-10-11 16:40:35 -070099
Ed Tanous5b6a1f92018-02-02 11:05:21 -0800100 if (content_encoding != nullptr) {
101 res.add_header("Content-Encoding", content_encoding);
Ed Tanousba9f9a62017-10-11 16:40:35 -0700102 }
103
Ed Tanous911ac312017-08-15 09:37:42 -0700104 // res.set_header("Cache-Control", "public, max-age=86400");
Ed Tanous5b6a1f92018-02-02 11:05:21 -0800105 std::ifstream inf(absolute_path);
Ed Tanous911ac312017-08-15 09:37:42 -0700106 if (!inf) {
107 CROW_LOG_DEBUG << "failed to read file";
Ed Tanous9dc2c4d2018-03-07 14:51:27 -0800108 res.code = static_cast<int>(HttpRespCode::NOT_FOUND);
Ed Tanous5b6a1f92018-02-02 11:05:21 -0800109 res.code = static_cast<int>(HttpRespCode::INTERNAL_ERROR);
Ed Tanous911ac312017-08-15 09:37:42 -0700110 res.end();
111 return;
112 }
113
Ed Tanous5b6a1f92018-02-02 11:05:21 -0800114 res.body = {std::istreambuf_iterator<char>(inf),
115 std::istreambuf_iterator<char>()};
Ed Tanous911ac312017-08-15 09:37:42 -0700116 res.end();
117 });
118 }
119 }
Ed Tanous911ac312017-08-15 09:37:42 -0700120} // namespace webassets
Ed Tanous5b6a1f92018-02-02 11:05:21 -0800121} // namespace webassets
122} // namespace crow