Medical Imaging Interaction Toolkit  2026.06.00
Medical Imaging Interaction Toolkit
mitkRestServer.h
Go to the documentation of this file.
1 /*============================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center (DKFZ)
6 All rights reserved.
7 
8 Use of this source code is governed by a 3-clause BSD license that can be
9 found in the LICENSE file.
10 
11 ============================================================================*/
12 
13 #ifndef mitkRestServer_h
14 #define mitkRestServer_h
15 
16 #include <MitkRESTAPIExports.h>
17 #include <mitkIRestServerService.h>
19 #include <mitkWeakPointer.h>
20 
21 #include <memory>
22 #include <thread>
23 #include <atomic>
24 #include <mutex>
25 #include <set>
26 #include <deque>
27 #include <chrono>
28 #include <unordered_map>
29 
30 // Forward declaration for httplib
31 namespace httplib {
32  class Server;
33  class Request;
34  class Response;
35 }
36 
37 namespace mitk
38 {
39  // Forward declarations
40  class DataStorageBridge;
41  class HealthController;
42  class DataStorageController;
43  class SwaggerController;
44  class RenderingController;
45  class RenderWindowBridge;
46 
54  {
55  public:
57  ~RestServer() override;
58 
59  // Non-copyable, non-movable (due to thread)
60  RestServer(const RestServer&) = delete;
61  RestServer& operator=(const RestServer&) = delete;
62  RestServer(RestServer&&) = delete;
64 
65  bool Start() override;
66  void Stop() override;
67  bool IsRunning() const override;
68 
69  void SetConfig(const RestServerConfig& config) override;
71  std::optional<RestServerConfig> GetRunningConfig() const override;
72 
73  void SetDataStorage(DataStorage* dataStorage) override;
74  DataStorage::Pointer GetDataStorage() const override;
75 
85 
87 
88  std::optional<std::string> GetServerUrl() const override;
89  std::optional<std::string> GetLastError() const override;
90 
91  std::vector<std::string> GetClientIPs() const override;
92  std::optional<RequestInfo> GetLastRequestInfo() const override;
93 
94  void SetLogLimit(std::optional<unsigned int> limit) override;
95  std::optional<unsigned int> GetLogLimit() const override;
96  std::vector<RequestInfo> GetRequestLog() const override;
97  void ClearRequestLog() override;
98  uint64_t GetRequestLogVersion() const override;
99 
100  std::optional<int64_t> GetUptimeSeconds() const override;
101 
102  private:
103  void RecordRequest(const std::string& endpoint, const std::string& method,
104  int responseCode, const std::string& clientIP);
105  void RegisterRoutes();
106  void ServerThreadFunc();
107 
110  void SyncDispatcherToController();
111 
121  bool CheckClientAccess(const httplib::Request& req, httplib::Response& res);
122 
132  bool CheckRateLimit(const httplib::Request& req, httplib::Response& res);
133 
144  bool CheckAuthentication(const httplib::Request& req, httplib::Response& res);
145 
146  // Server components
147  std::unique_ptr<httplib::Server> m_Server;
148  std::unique_ptr<std::thread> m_ServerThread;
149 
150  // Business logic
151  std::unique_ptr<DataStorageBridge> m_Bridge;
152  std::unique_ptr<RenderWindowBridge> m_RenderWindowBridge;
153  std::unique_ptr<HealthController> m_HealthController;
154  std::unique_ptr<DataStorageController> m_DataStorageController;
155  std::unique_ptr<SwaggerController> m_SwaggerController;
156  std::unique_ptr<RenderingController> m_RenderingController;
158 
159  // State
160  mutable std::mutex m_Mutex;
161  RestServerConfig m_PendingConfig;
162  std::optional<RestServerConfig> m_RunningConfig;
163  std::atomic<bool> m_Running{false};
164  std::optional<std::string> m_LastError;
165 
166  // Request tracking
167  std::set<std::string> m_ClientIPs;
168 
169  // Request logging
170  std::deque<RequestInfo> m_RequestLog;
171  std::optional<unsigned int> m_LogLimit;
172  uint64_t m_RequestLogVersion{0};
173 
174  // Temporary directory for data serialization
175  std::string m_TempDirectory;
176 
177  // Server start time for uptime tracking
178  std::optional<std::chrono::steady_clock::time_point> m_StartTime;
179 
180  // Rate limiting data (protected by m_Mutex)
181  std::unordered_map<std::string, std::deque<std::chrono::steady_clock::time_point>> m_RateLimitMap;
182  uint64_t m_RateLimitCheckCount{0};
183 
191  bool SetupTempDirectory();
192 
202  static void CleanupTempDirectory(const std::string& tempDir);
203  };
204 }
205 
206 #endif
#define MITKRESTAPI_EXPORT
Abstract data management class that organizes DataNode objects in a directed acyclic graph.
Bridge between the REST API server and the Qt/render-window layer.
HTTP REST server implementation.
std::optional< std::string > GetLastError() const override
Get the last error message.
std::optional< unsigned int > GetLogLimit() const override
Get the current request log limit.
DataStorage::Pointer GetDataStorage() const override
Get the currently connected DataStorage.
bool Start() override
Start the REST server with the pending configuration.
void ClearRequestLog() override
Clear all logged requests.
std::optional< std::string > GetServerUrl() const override
Get the full URL where the server is listening.
std::vector< RequestInfo > GetRequestLog() const override
Get all logged requests.
std::optional< RequestInfo > GetLastRequestInfo() const override
Get information about the last request processed.
RenderWindowBridge * GetRenderWindowBridge() override
Return the server-owned RenderWindowBridge.
void SetLogLimit(std::optional< unsigned int > limit) override
Set the request log limit.
RestServerConfig GetPendingConfig() const override
Get the pending server configuration.
~RestServer() override
RestServer(const RestServer &)=delete
std::vector< std::string > GetClientIPs() const override
Get the list of client IP addresses that have sent requests.
void SetConfig(const RestServerConfig &config) override
Set the pending server configuration.
bool IsRunning() const override
Check if the server is currently running.
void SetDispatcher(StorageThreadDispatcherBase *dispatcher)
Set the thread dispatcher for DataStorage operations.
std::optional< int64_t > GetUptimeSeconds() const override
Get the server uptime in seconds.
RestServer(RestServer &&)=delete
RestServer & operator=(const RestServer &)=delete
void SetDataStorage(DataStorage *dataStorage) override
Set the DataStorage for REST API operations.
uint64_t GetRequestLogVersion() const override
Get the request log version counter.
void Stop() override
Stop the REST server.
RestServer & operator=(RestServer &&)=delete
std::optional< RestServerConfig > GetRunningConfig() const override
Get the configuration the server is currently running with.
Abstract base class for dispatching tasks to the thread that owns DataStorage.
A weak pointer that observes an itk::Object without preventing its deletion.
Find image slices visible on a given plane.
Service interface for the REST API server.
Configuration parameters for the REST API server.