Line data Source code
1 : #ifndef THINGER_HTTP_ROUTE_REGISTRAR_HPP
2 : #define THINGER_HTTP_ROUTE_REGISTRAR_HPP
3 :
4 : #include <string>
5 : #include "route.hpp"
6 : #include "../../common/http_request.hpp"
7 :
8 : namespace thinger::http {
9 :
10 : // Route registration methods, one per HTTP method, taking any route callback (see
11 : // route_callable): synchronous or coroutine, with or without the JSON body. They return
12 : // the route, to document it or set its schema. Derived provides
13 : // route& make_route(method, const std::string& path), creating the route to register.
14 : template<typename Derived>
15 : class route_registrar {
16 : public:
17 : template<route_callable F>
18 23513 : route& get(const std::string& path, F&& handler) {
19 23513 : return add(method::GET, path, std::forward<F>(handler));
20 : }
21 :
22 : template<route_callable F>
23 6574 : route& post(const std::string& path, F&& handler) {
24 6574 : return add(method::POST, path, std::forward<F>(handler));
25 : }
26 :
27 : template<route_callable F>
28 718 : route& put(const std::string& path, F&& handler) {
29 718 : return add(method::PUT, path, std::forward<F>(handler));
30 : }
31 :
32 : // delete is a keyword
33 : template<route_callable F>
34 447 : route& del(const std::string& path, F&& handler) {
35 447 : return add(method::DELETE, path, std::forward<F>(handler));
36 : }
37 :
38 : template<route_callable F>
39 495 : route& patch(const std::string& path, F&& handler) {
40 495 : return add(method::PATCH, path, std::forward<F>(handler));
41 : }
42 :
43 : template<route_callable F>
44 155 : route& head(const std::string& path, F&& handler) {
45 155 : return add(method::HEAD, path, std::forward<F>(handler));
46 : }
47 :
48 : template<route_callable F>
49 206 : route& options(const std::string& path, F&& handler) {
50 206 : return add(method::OPTIONS, path, std::forward<F>(handler));
51 : }
52 :
53 : private:
54 : template<typename F>
55 32108 : route& add(method http_method, const std::string& path, F&& handler) {
56 32108 : return static_cast<Derived&>(*this).make_route(http_method, path) = std::forward<F>(handler);
57 : }
58 : };
59 :
60 : } // namespace thinger::http
61 :
62 : #endif // THINGER_HTTP_ROUTE_REGISTRAR_HPP
|