roar
Loading...
Searching...
No Matches
file_server.hpp
Go to the documentation of this file.
1#pragma once
2
6
7#include <boost/describe/class.hpp>
8
9#include <fstream>
10#include <random>
11#include <algorithm>
12#include <iostream>
13
14using namespace Roar::Literals;
15
17{
18 public:
20 {
21 auto p = Roar::resolvePath("~/roar");
22 if (!std::filesystem::exists(p))
23 std::filesystem::create_directory(p);
24 std::cout << p << "\n";
25
26 p = Roar::resolvePath("%appdata%/roar");
27 if (!std::filesystem::exists(p))
28 std::filesystem::create_directory(p);
29 std::cout << p << "\n";
30 }
31
32 private:
34
35 // Use the ROAR_SERVE macro to define a route that gives access to the filesystem. The given paths that point to the
36 // filesystem may be prefixed with some magic values. ~ and %appdata% are some of them. To see what they resolve
37 // to, see Roar::resolvePath in doxygen.
38 ROAR_SERVE(serve)("/bla", "~/roar");
39
40 // This will resolve to home on linux and to the appdata/Roaming on windows.
41 ROAR_SERVE(serveAppdata)("/blub", "%appdata%/roar");
42
43 std::filesystem::path getServePath()
44 {
45 const auto tempPath = Roar::resolvePath("%temp%/roarFileServerExample");
46 if (!std::filesystem::exists(tempPath))
47 std::filesystem::create_directory(tempPath);
48
49 std::filesystem::create_directory(tempPath / "dir");
50 createDummyFile(tempPath / "dir" / "file1.txt");
51 createDummyFile(tempPath / "dir" / "file2.txt");
52 createDummyFile(tempPath / "file_A.txt");
53 createDummyFile(tempPath / "file_B.txt");
54 createDummyFile(tempPath / "file_C.txt");
55 createDummyFile(tempPath / "file_D.txt");
56 createDummyFile(tempPath / "file_E.txt");
57 createDummyFile(tempPath / "file_F.txt");
58
59 // path is also automatically resolved from here.
60 return "%temp%/roarFileServerExample";
61 }
62 void createDummyFile(std::filesystem::path const& where)
63 {
64 std::ofstream writer{where, std::ios_base::binary};
65 std::string str(letters(gen), 'a');
66 writer << str;
67 }
68
69 ROAR_SERVE(customServeOptions)
70 ({
71 .path = "/",
72 .routeOptions = {.allowUnsecure = false},
73 .serveOptions =
74 {
75 .allowDownload = true,
76 .allowUpload = false,
77 .allowOverwrite = false,
78 .allowDelete = false,
79 .allowDeleteOfNonEmptyDirectories = false,
80 .allowListing = true,
81 .pathProvider = &FileServer::getServePath,
82 },
83 });
84
85 private:
86 std::mt19937 gen{0};
87 std::uniform_int_distribution<int> letters{1, 2_MiB};
88
89 private:
90 // This makes routes visible to the library. For as long as we have to wait for native reflection...
91 BOOST_DESCRIBE_CLASS(FileServer, (), (), (), (roar_serve, roar_serveAppdata, roar_customServeOptions))
92};
Definition file_server.hpp:17
std::uniform_int_distribution< int > letters
Definition file_server.hpp:87
void createDummyFile(std::filesystem::path const &where)
Definition file_server.hpp:62
ROAR_SERVE() serve("/bla", "~/roar")
Definition file_server.cpp:7
ROAR_SERVE() serveAppdata("/blub", "%appdata%/roar")
Definition file_server.cpp:16
std::filesystem::path getServePath()
Definition file_server.hpp:43
FileServer()
Definition file_server.hpp:19
std::mt19937 gen
Definition file_server.hpp:86
ROAR_MAKE_LISTENER(FileServer)
#define ROAR_SERVE(HandlerName)
Definition request_listener.hpp:248
Definition memory.hpp:6
std::filesystem::path resolvePath(std::filesystem::path const &path)
Will replace prefixes like "~" and "%appdata%" with actual directories on linux and windows.
Definition special_paths.cpp:73