-
Notifications
You must be signed in to change notification settings - Fork 183
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
585 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
file(GLOB_RECURSE maa_restful_src *.h *.hpp *.cpp) | ||
|
||
add_executable(MaaRestful ${maa_restful_src}) | ||
|
||
target_include_directories( | ||
MaaRestful | ||
INTERFACE ../../include | ||
PRIVATE . ../include ../../include) | ||
|
||
target_link_libraries(MaaRestful MaaFramework MaaToolKit MaaUtils HeaderOnlyLibraries Boost::system) | ||
|
||
install( | ||
TARGETS MaaRestful | ||
RUNTIME DESTINATION bin | ||
LIBRARY DESTINATION bin | ||
ARCHIVE DESTINATION lib) | ||
|
||
source_group(TREE ${CMAKE_CURRENT_SOURCE_DIR} FILES ${maa_restful_src}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
#pragma once | ||
|
||
#include "Helper/Forward.hpp" | ||
|
||
#include <meojson/json.hpp> | ||
|
||
MAA_RESTFUL_NS_BEGIN | ||
|
||
struct Context | ||
{ | ||
Context(http::request<http::string_body>&& req, http::response<http::string_body>& res) | ||
: req_(std::move(req)), res_(res) | ||
{} | ||
|
||
void json_body(const json::object& value) | ||
{ | ||
res_.set(http::field::content_type, "application/json"); | ||
res_.body() = value.to_string(); | ||
} | ||
|
||
void bad_request(const std::string& why) | ||
{ | ||
res_.result(http::status::bad_request); | ||
json_body({ { "error", why } }); | ||
} | ||
|
||
void init() | ||
{ | ||
res_.set(http::field::server, BOOST_BEAST_VERSION_STRING); | ||
res_.keep_alive(req_.keep_alive()); | ||
} | ||
|
||
http::request<http::string_body> req_; | ||
http::response<http::string_body>& res_; | ||
}; | ||
|
||
MAA_RESTFUL_NS_END |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
#include "Handle/DeviceDispatcher.hpp" | ||
#include "MaaToolKit/MaaToolKitAPI.h" | ||
#include "Utils/Format.hpp" | ||
|
||
MAA_RESTFUL_NS_BEGIN | ||
|
||
bool DeviceDispatcher::handle(Context& ctx, std::vector<std::string_view> url_segs) | ||
{ | ||
auto seg0 = url_segs[0]; | ||
|
||
if (seg0 == "devices") { | ||
if (url_segs.size() == 1) { | ||
// switch (ctx.req_.method()) { | ||
// case http::verb::get: | ||
// // TODO: return all devices | ||
// ctx.json_body({ { "count", MaaToolKitFindDevice() } }); | ||
// break; | ||
// default: | ||
// ctx.bad_request(MAA_FMT::format("bad verb {}", std::string_view(ctx.req_.method_string()))); | ||
// } | ||
// return true; | ||
} | ||
else { | ||
auto seg1 = url_segs[1]; | ||
|
||
if (seg1 == "find") { | ||
switch (ctx.req_.method()) { | ||
case http::verb::put: | ||
ctx.json_body({ { "count", MaaToolKitFindDevice() } }); | ||
break; | ||
default: | ||
ctx.bad_request(MAA_FMT::format("bad verb {}", std::string_view(ctx.req_.method_string()))); | ||
} | ||
return true; | ||
} | ||
} | ||
} | ||
else if (seg0 == "device") { | ||
if (url_segs.size() == 1) { | ||
ctx.bad_request(MAA_FMT::format("id expected")); | ||
return true; | ||
} | ||
else { | ||
auto seg1 = url_segs[1]; | ||
|
||
int id = std::stoul(std::string(seg1)); | ||
|
||
if (url_segs.size() == 2) { | ||
switch (ctx.req_.method()) { | ||
case http::verb::get: | ||
ctx.json_body({ { "name", MaaToolKitGetDeviceName(id) }, | ||
{ "adb_path", MaaToolKitGetDeviceAdbPath(id) }, | ||
{ "adb_serial", MaaToolKitGetDeviceAdbSerial(id) }, | ||
{ "controller_type", MaaToolKitGetDeviceAdbControllerType(id) }, | ||
{ "adb_config", MaaToolKitGetDeviceAdbConfig(id) } }); | ||
break; | ||
default: | ||
ctx.bad_request(MAA_FMT::format("bad verb {}", std::string_view(ctx.req_.method_string()))); | ||
} | ||
return true; | ||
} | ||
else { | ||
// TODO: implement partial query | ||
} | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
MAA_RESTFUL_NS_END |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#pragma once | ||
|
||
#include "Handle/Dispatcher.hpp" | ||
|
||
MAA_RESTFUL_NS_BEGIN | ||
|
||
struct DeviceDispatcher : public Dispatcher<void> | ||
{ | ||
virtual bool handle(Context& ctx, std::vector<std::string_view> url_segs) override; | ||
}; | ||
|
||
MAA_RESTFUL_NS_END |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
#include "Handle/Dispatcher.hpp" | ||
#include "Handle/RootDispatcher.hpp" | ||
#include "MaaFramework/MaaAPI.h" | ||
#include "Utils/Format.hpp" | ||
#include <iostream> | ||
#include <ranges> | ||
|
||
MAA_RESTFUL_NS_BEGIN | ||
|
||
http::message_generator handle_request(http::request<http::string_body>&& req) | ||
{ | ||
http::response<http::string_body> res { http::status::ok, req.version() }; | ||
|
||
std::string url = req.target(); | ||
auto url_segs_rng = std::string_view(url) | std::views::split('/'); | ||
std::vector<std::string_view> url_segs; | ||
for (auto seg : url_segs_rng) { | ||
std::string_view part(seg.begin(), seg.end()); | ||
if (part.length() == 0) { | ||
continue; | ||
} | ||
url_segs.push_back(part); | ||
} | ||
|
||
Context ctx(std::move(req), res); | ||
ctx.init(); | ||
|
||
RootDispatcher rd; | ||
if (!rd.handle(ctx, url_segs)) { | ||
ctx.bad_request(MAA_FMT::format("unknown path {}", std::string_view(ctx.req_.target()))); | ||
} | ||
|
||
res.prepare_payload(); | ||
|
||
return res; | ||
|
||
// auto const bad_request = [&req](beast::string_view why) { | ||
// http::response<http::string_body> res { http::status::bad_request, req.version() }; | ||
// res.set(http::field::server, BOOST_BEAST_VERSION_STRING); | ||
// res.set(http::field::content_type, "text/html"); | ||
// res.keep_alive(req.keep_alive()); | ||
// res.body() = std::string(why); | ||
// res.prepare_payload(); | ||
// return res; | ||
// }; | ||
|
||
// // auto const not_found = [&req](beast::string_view target) { | ||
// // http::response<http::string_body> res { http::status::not_found, req.version() }; | ||
// // res.set(http::field::server, BOOST_BEAST_VERSION_STRING); | ||
// // res.set(http::field::content_type, "text/html"); | ||
// // res.keep_alive(req.keep_alive()); | ||
// // res.body() = "The resource '" + std::string(target) + "' was not found."; | ||
// // res.prepare_payload(); | ||
// // return res; | ||
// // }; | ||
|
||
// // auto const server_error = [&req](beast::string_view what) { | ||
// // http::response<http::string_body> res { http::status::internal_server_error, req.version() }; | ||
// // res.set(http::field::server, BOOST_BEAST_VERSION_STRING); | ||
// // res.set(http::field::content_type, "text/html"); | ||
// // res.keep_alive(req.keep_alive()); | ||
// // res.body() = "An error occurred: '" + std::string(what) + "'"; | ||
// // res.prepare_payload(); | ||
// // return res; | ||
// // }; | ||
|
||
// if (req.method() != http::verb::get) { | ||
// return bad_request("Unknown HTTP-method"); | ||
// } | ||
|
||
// http::response<http::string_body> res { http::status::ok, req.version() }; | ||
// res.set(http::field::server, BOOST_BEAST_VERSION_STRING); | ||
// res.set(http::field::content_type, "text/plain"); | ||
// res.body() = req.target(); | ||
// res.prepare_payload(); | ||
// res.keep_alive(req.keep_alive()); | ||
// return res; | ||
} | ||
|
||
MAA_RESTFUL_NS_END |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#pragma once | ||
|
||
#include "Handle/Context.hpp" | ||
|
||
#include <string> | ||
#include <vector> | ||
|
||
MAA_RESTFUL_NS_BEGIN | ||
|
||
struct DispatcherBase | ||
{ | ||
virtual ~DispatcherBase() {} | ||
}; | ||
|
||
template <typename Env> | ||
struct Dispatcher | ||
{ | ||
Dispatcher(Env env) : env_(env) {} | ||
|
||
virtual bool handle(Context& ctx, std::vector<std::string_view> url_segs) = 0; | ||
|
||
Env env_; | ||
}; | ||
|
||
template <> | ||
struct Dispatcher<void> | ||
{ | ||
virtual bool handle(Context& ctx, std::vector<std::string_view> url_segs) = 0; | ||
}; | ||
|
||
http::message_generator handle_request(http::request<http::string_body>&& req); | ||
|
||
MAA_RESTFUL_NS_END |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
#include "Handle/RootDispatcher.hpp" | ||
#include "MaaFramework/MaaAPI.h" | ||
#include "Utils/Format.hpp" | ||
|
||
MAA_RESTFUL_NS_BEGIN | ||
|
||
bool RootDispatcher::handle(Context& ctx, std::vector<std::string_view> url_segs) | ||
{ | ||
if (url_segs.size() == 0) { | ||
switch (ctx.req_.method()) { | ||
case http::verb::get: | ||
ctx.json_body({ { "msg", "hello world!" } }); | ||
break; | ||
default: | ||
ctx.bad_request(MAA_FMT::format("bad verb {}", std::string_view(ctx.req_.method_string()))); | ||
} | ||
return true; | ||
} | ||
else { | ||
auto seg0 = url_segs[0]; | ||
|
||
if (seg0 == "version") { | ||
switch (ctx.req_.method()) { | ||
case http::verb::get: | ||
ctx.json_body({ { "version", MaaVersion() } }); | ||
break; | ||
default: | ||
ctx.bad_request(MAA_FMT::format("bad verb {}", std::string_view(ctx.req_.method_string()))); | ||
} | ||
return true; | ||
} | ||
|
||
return device_.handle(ctx, url_segs); | ||
} | ||
} | ||
|
||
MAA_RESTFUL_NS_END |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#pragma once | ||
|
||
#include "Handle/DeviceDispatcher.hpp" | ||
|
||
MAA_RESTFUL_NS_BEGIN | ||
|
||
struct RootDispatcher : public Dispatcher<void> | ||
{ | ||
virtual bool handle(Context& ctx, std::vector<std::string_view> url_segs) override; | ||
|
||
DeviceDispatcher device_; | ||
}; | ||
|
||
MAA_RESTFUL_NS_END |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#pragma once | ||
|
||
#include "Conf/Conf.h" | ||
#include "Utils/Boost.hpp" | ||
|
||
namespace beast = boost::beast; | ||
namespace http = beast::http; | ||
namespace asio = boost::asio; | ||
using tcp = asio::ip::tcp; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#pragma once | ||
|
||
#include "Helper/Forward.hpp" | ||
#include "Utils/Logger.h" | ||
|
||
MAA_RESTFUL_NS_BEGIN | ||
|
||
inline void fail(beast::error_code ec, char const* what) | ||
{ | ||
LogError << what << ec.message(); | ||
} | ||
|
||
MAA_RESTFUL_NS_END |
Oops, something went wrong.