SOMHunter Core
network-api.h
Go to the documentation of this file.
1 /* This file is part of SOMHunter.
2  *
3  * Copyright (C) 2021 Frantisek Mejzlik <frankmejzlik@protonmail.com>
4  * Mirek Kratochvil <exa.exa@gmail.com>
5  * Patrik Vesely <prtrikvesely@gmail.com>
6  *
7  * SOMHunter is free software: you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License as published by the Free
9  * Software Foundation, either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * SOMHunter is distributed in the hope that it will be useful, but WITHOUT ANY
13  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
15  * details.
16  *
17  * You should have received a copy of the GNU General Public License along with
18  * SOMHunter. If not, see <https://www.gnu.org/licenses/>.
19  */
20 
21 #ifndef NETWORK_API_H_
22 #define NETWORK_API_H_
23 
24 #include <mutex>
25 // ---
26 #include <cpprest/asyncrt_utils.h>
27 #include <cpprest/containerstream.h>
28 #include <cpprest/filestream.h>
29 #include <cpprest/http_listener.h>
30 #include <cpprest/json.h>
31 #include <cpprest/producerconsumerstream.h>
32 #include <cpprest/uri.h>
33 // ---
34 #include "common.h"
35 #include "query-types.h"
36 
37 using namespace web; //< cpprest
38 using namespace http; //< cpprest
39 using namespace utility; //< cpprest
40 using namespace http::experimental::listener; //< cpprest
41 
42 namespace sh {
43 
44 class Somhunter;
45 
57 class NetworkApi {
58 public:
60  NetworkApi() = delete;
61 
63  NetworkApi(const ApiConfig& API, Somhunter* p_core);
64 
65  // ---
66 
68  void initialize();
69 
71  void run();
72 
74  void terminate();
75 
76 private:
78  static void add_CORS_headers(http_response& res);
79 
80  // ---
81  // *** Handlers ***
82 
83  void handle__api__GET(http_request req);
84  void handle__api__config__GET(http_request req);
85 
86  void handle__config__GET(http_request req);
87  void handle__user__context__GET(http_request req);
88 
89  void handle__dataset__video_detail__GET(http_request req);
90 
91  void handle__search__get_top_display__POST(http_request req);
92  void handle__search__get_som_display__POST(http_request req);
93  void handle__search__get_som_relocation_display__POST(http_request req);
94  void handle__search__keyword_autocomplete__GET(http_request req);
95 
96  void handle__search__reset__POST(http_request req);
97  void handle__search__rescore__POST(http_request req);
98  void handle__search__like_frame__POST(http_request req);
99  void handle__search__bookmark_frame__POST(http_request req);
100  void handle__search__context__POST(http_request req);
101  void handle__search__context__GET(http_request req);
102 
103  void handle__log__scroll__GET(http_request req);
104  void handle__log__text_query_change__GET(http_request req);
105  void handle__log__canvas_query_change__GET(http_request req);
106 
107  void handle__eval_server__submit__POST(http_request req);
108  void handle__eval_server__login__POST(http_request req);
109  void handle__eval_server__logout__POST(http_request req);
110 
111  // *** Helpers ***
112 
123  void push_endpoint(
124  const std::string& path,
125  std::function<void(NetworkApi*, http_request)> GET_handler = std::function<void(NetworkApi*, http_request)>{},
126  std::function<void(NetworkApi*, http_request)> POST_handler = std::function<void(NetworkApi*, http_request)>{},
127  std::function<void(NetworkApi*, http_request)> PUT_handler = std::function<void(NetworkApi*, http_request)>{},
128  std::function<void(NetworkApi*, http_request)> DEL_handler = std::function<void(NetworkApi*, http_request)>{});
129 
136  RescoreMetadata extract_rescore_metadata(web::json::value& body);
137 
144  std::vector<TextualQuery> extract_textual_query(web::json::value& body);
145 
152  std::vector<RelocationQuery> extract_relocation_query(web::json::value& body);
153 
160  std::vector<CanvasQuery> extract_canvas_query(web::json::value& body);
161 
168  Filters extract_filters(web::json::value& body);
169 
170  // *** Synchronization ***
171  std::lock_guard<std::mutex> exclusive_lock() const { return std::lock_guard<std::mutex>{ _req_mtx }; };
172 
173  // ---
174 
177 
180 
182  std::string _base_addr;
183 
185  std::vector<http_listener> _endpoints;
186 
188  mutable std::mutex _req_mtx;
189 };
190 
191 }; // namespace sh
192 
193 #endif // NETWORK_API_H_
Class responsible for listening for HTTP requests and for handlling them using the core.
Definition: network-api.h:57
std::vector< http_listener > _endpoints
Vector of used endpoints.
Definition: network-api.h:185
Somhunter * _p_core
A pointer to the core that this API exposes.
Definition: network-api.h:179
std::mutex _req_mtx
Lock used to synchronize some requests.
Definition: network-api.h:188
ApiConfig _API_config
A private copy of config.
Definition: network-api.h:171
std::lock_guard< std::mutex > exclusive_lock() const
Definition: network-api.h:171
NetworkApi()=delete
Only single ctor overload is acceptable.
std::string _base_addr
Base HTTP API address.
Definition: network-api.h:182
The main C++ API of the SOMHunter Core.
Definition: somhunter.h:54
Definition: common-types.h:33
Definition: settings.h:52