55 */
66
77#include " file_body.hpp"
8- #include " mime_type.hpp"
9- #include " ssvi_http_server.hpp"
10- #include " sema_task.cpp"
8+ #include " ssvim_http_server.hpp"
119
1210#include " SwiftCompleter.h"
1311
@@ -45,7 +43,9 @@ struct service_context {
4543 log_level logging;
4644};
4745
48- namespace beast { // FIXME: ssvi
46+ using namespace beast ::http;
47+
48+ namespace ssvim {
4949namespace http {
5050
5151using socket_type = boost::asio::ip::tcp::socket;
@@ -89,10 +89,11 @@ class endpoint_impl : public std::enable_shared_from_this<endpoint_impl>{
8989 endpoint_fn _start;
9090};
9191
92- using namespace ssvi ;
92+ using namespace ssvim ;
9393
9494endpoint_impl make_slow_test_endpoint ();
9595endpoint_impl make_status_endpoint ();
96+ endpoint_impl make_shutdown_endpoint ();
9697endpoint_impl make_completions_endpoint ();
9798endpoint_impl make_diagnostics_endpoint ();
9899
@@ -141,6 +142,7 @@ class session : public std::enable_shared_from_this<session>
141142 };
142143
143144 insert_endpoint (" /status" , make_status_endpoint ());
145+ insert_endpoint (" /shutdown" , make_shutdown_endpoint ());
144146 insert_endpoint (" /completions" , make_completions_endpoint ());
145147 insert_endpoint (" /diagnostics" , make_diagnostics_endpoint ());
146148 insert_endpoint (" /slow_test" , make_slow_test_endpoint ());
@@ -270,8 +272,26 @@ endpoint_impl make_status_endpoint() {
270272 res.status = 200 ;
271273 res.version = session->request ().version ;
272274 res.fields .insert (" Server" , " ssvi_http_server" );
273- res.fields .insert (" Content-Type" , " text/html" );
275+ res.fields .insert (" Content-Type" , " application/json" );
276+ prepare (res);
277+ session->write (res);
278+ });
279+ }
280+
281+ endpoint_impl make_shutdown_endpoint () {
282+ return endpoint_impl ([&](std::shared_ptr<session> session){
283+ std::cout << " Recieved Shutdown Request" ;
284+ response<string_body> res;
285+ res.status = 200 ;
286+ res.version = session->request ().version ;
287+ res.fields .insert (" Server" , " ssvi_http_server" );
288+ res.fields .insert (" Content-Type" , " application/json" );
274289 prepare (res);
290+ dispatch_after (dispatch_time (DISPATCH_TIME_NOW, 2 * NSEC_PER_SEC),
291+ dispatch_get_main_queue (), ^{
292+ std::cout << " Shutting down..." ;
293+ exit (0 );
294+ });
275295 session->write (res);
276296 });
277297}
@@ -349,7 +369,7 @@ endpoint_impl make_completions_endpoint() {
349369 res.status = 200 ;
350370 res.version = session->request ().version ;
351371 res.fields .insert (" Server" , " ssvi_http_server" );
352- res.fields .insert (" Content-Type" , " text/html " );
372+ res.fields .insert (" Content-Type" , " application/json " );
353373 res.body = candidates;
354374 prepare (res);
355375 session->write (res);
@@ -403,7 +423,7 @@ endpoint_impl make_diagnostics_endpoint() {
403423 res.status = 200 ;
404424 res.version = session->request ().version ;
405425 res.fields .insert (" Server" , " ssvi_http_server" );
406- res.fields .insert (" Content-Type" , " text/html " );
426+ res.fields .insert (" Content-Type" , " application/json " );
407427 res.body = diagnostics;
408428 prepare (res);
409429 session->write (res);
@@ -414,7 +434,8 @@ endpoint_impl make_slow_test_endpoint()
414434{
415435 return endpoint_impl ([](std::shared_ptr<session> session){
416436 // Wait for 10 seconds to write hello world.
417- dispatch_after (dispatch_time (DISPATCH_TIME_NOW, 10 * NSEC_PER_SEC), dispatch_get_main_queue (), ^{
437+ dispatch_after (dispatch_time (DISPATCH_TIME_NOW, 10 * NSEC_PER_SEC),
438+ dispatch_get_main_queue (), ^{
418439 std::cout << " Enter main: " << session->instance_id () << " \n " ;
419440 std::cout << session->request ().url ;
420441 std::cout.flush ();
@@ -423,7 +444,7 @@ endpoint_impl make_slow_test_endpoint()
423444 res.status = 200 ;
424445 res.version = session->request ().version ;
425446 res.fields .insert (" Server" , " ssvi_http_server" );
426- res.fields .insert (" Content-Type" , " text/html " );
447+ res.fields .insert (" Content-Type" , " application/json " );
427448 res.body = " Hello World" ;
428449 prepare (res);
429450 session->write (res);
@@ -438,7 +459,7 @@ error_response(req_type request, std::string message){
438459 res.reason = " Internal Error" ;
439460 res.version = request.version ;
440461 res.fields .insert (" Server" , " http_async_server" );
441- res.fields .insert (" Content-Type" , " text/html " );
462+ res.fields .insert (" Content-Type" , " application/json " );
442463 res.body =
443464 std::string{" An internal error occurred" } + message;
444465 prepare (res);
@@ -452,12 +473,12 @@ not_found_response(req_type request){
452473 res.reason = " Not Found" ;
453474 res.version = request.version ;
454475 res.fields .insert (" Server" , " http_async_server" );
455- res.fields .insert (" Content-Type" , " text/html " );
476+ res.fields .insert (" Content-Type" , " application/json " );
456477 res.body = " Endpoint: '" + request.url + " ' not found" ;
457478 prepare (res);
458479 return res;
459480}
460481
461482} // http
462- } // beast
483+ } // ssvim
463484
0 commit comments