Sese Framework  x.y.z
A cross-platform framework
载入中...
搜索中...
未找到
Controller.h
浏览该文件的文档.
1// Copyright 2024 libsese
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
23#pragma once
24
26
27#include <set>
28#include <vector>
29#include <functional>
30#include <utility>
31
32namespace sese::net::http {
33
35class Servlet {
36public:
37 using Ptr = std::shared_ptr<Servlet>;
38 using Callback = std::function<void(HttpServletContext &)>;
39
40 Servlet();
41
42 Servlet(RequestType expect, const std::string &url);
43
45
46 [[nodiscard]] const std::string &getUri() const { return uri; }
47
48 [[nodiscard]] RequestType getExpectType() const { return expect_type; }
49
50 [[nodiscard]] auto &getExpectQueryArgs() const { return expect_query_args; }
51
52 [[nodiscard]] auto &getExpectHeaders() const { return expect_headers; }
53
54 void requiredQueryArg(const std::string &arg);
55
56 void requiredHeader(const std::string &arg);
57
58 void invoke(HttpServletContext &ctx) const;
59
61 setCallback(std::move(callback));
62 return *this;
63 }
64
65private:
67 std::string uri;
71 std::set<std::string> expect_query_args;
73 std::set<std::string> expect_headers;
76};
77
80public:
81 using Ptr = std::shared_ptr<Controller>;
82
83 Controller() = default;
84 virtual ~Controller() = default;
85
86 virtual void init() = 0;
87
88 auto begin() { return servlets.begin(); }
89 auto end() { return servlets.end(); }
90
91protected:
92 std::vector<Servlet> servlets;
93};
94
95} // namespace sese::net::http
96
100#define SESE_CTRL(name, ...) \
101 class name final : public sese::net::http::Controller { \
102 public: \
103 using RequestType = sese::net::http::RequestType; \
104 name() : Controller() { \
105 } \
106 void init() override; \
107 \
108 private: \
109 __VA_ARGS__; \
110 }; \
111 void name::init()
112
118#define SESE_URL(name, method, url) \
119 this->servlets.emplace_back(method, url); \
120 sese::net::http::Servlet &name = servlets[servlets.size() - 1]; \
121 name = [this](sese::net::http::HttpServletContext &ctx)