yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
editor_http_server.h
Go to the documentation of this file.
1#ifndef YAZE_APP_SERVICE_EDITOR_HTTP_SERVER_H_
2#define YAZE_APP_SERVICE_EDITOR_HTTP_SERVER_H_
3
4#if !defined(__EMSCRIPTEN__) && defined(YAZE_HTTP_API_ENABLED)
5
6#include <memory>
7
8#include "absl/status/status.h"
9#include "app/application.h"
11#include "util/log.h"
13
14namespace yaze::app {
15
16// Both Cocoa and the ordinary desktop loop own this server locally. Create it
17// after Initialize succeeds, and Stop it before Application::Shutdown destroys
18// the controller captured by its callbacks.
19inline std::unique_ptr<cli::api::HttpServer> SetupEditorHttpServer(
20 const AppConfig& config) {
21 auto* controller = Application::Instance().GetController();
22 if (!config.enable_api || !controller || !controller->IsActive()) {
23 return nullptr;
24 }
25
26 auto server = std::make_unique<cli::api::HttpServer>();
27 server->SetSymbolProviderSource(
28 [controller]() -> emu::debug::SymbolProvider* {
29 auto* manager = controller->editor_manager();
30 return manager ? &manager->emulator().symbol_provider() : nullptr;
31 });
32 // HTTP callbacks run on service threads; SDL window actions belong to the
33 // UI thread. The server stops before its captured controller is destroyed.
34 auto run_window_action = [controller](void (Controller::*action)()) -> bool {
35 absl::Status status = util::UiThreadQueue::Get().RunOnUiThread(
36 [controller, action]() -> absl::Status {
37 (controller->*action)();
38 return absl::OkStatus();
39 });
40 if (!status.ok()) {
41 LOG_WARN("App", "HTTP window action failed: %s",
42 std::string(status.message()).c_str());
43 }
44 return status.ok();
45 };
46 server->SetWindowActions(
47 [run_window_action]() {
48 return run_window_action(&Controller::ShowWindow);
49 },
50 [run_window_action]() {
51 return run_window_action(&Controller::HideWindow);
52 });
53
54 if (config.editor_control) {
55 server->SetEditorControl([controller](const std::string& request) {
56 return controller->RequestEditorControl(request);
57 });
58 }
59
60 auto status = server->Start(config.api_port, config.editor_control);
61 if (!status.ok()) {
62 LOG_ERROR("App", "Failed to start API server: %s",
63 std::string(status.message()).c_str());
64 return nullptr;
65 }
66 LOG_INFO("App", "API Server started on port %d", config.api_port);
67 return server;
68}
69
70} // namespace yaze::app
71#endif
72#endif
Controller * GetController()
Definition application.h:83
static Application & Instance()
#define LOG_ERROR(category, format,...)
Definition log.h:140
#define LOG_WARN(category, format,...)
Definition log.h:138
#define LOG_INFO(category, format,...)
Definition log.h:136
WindowContent * Get(const std::string &id)
Get a specific panel by its ID.