22
33#include < archive.hpp>
44
5+ #include < yyjson.h>
6+
57static std::string get_archive_path ()
68{
79 std::string result (ARCHIVE_DIRECTORY );
@@ -18,8 +20,6 @@ static std::string get_archive_path()
1820class archive_handler : public webframe ::handler
1921{
2022public:
21- archive_handler () = default ;
22- ~archive_handler () = default ;
2323 void open (const std::string& archive_path)
2424 {
2525 _archive.reset (new hyperpage::reader (archive_path));
@@ -28,6 +28,9 @@ class archive_handler : public webframe::handler
2828 void handle_get (const webframe::request* req, webframe::response* res) override
2929 {
3030 std::string page_path = req->get_path ();
31+ if (page_path.empty () || page_path == " /" ) {
32+ page_path = " /index.html" ;
33+ }
3134 auto page = _archive->load (page_path);
3235 if (page) {
3336 res->set_status (200 );
@@ -45,6 +48,37 @@ class archive_handler : public webframe::handler
4548 std::unique_ptr<hyperpage::reader> _archive;
4649};
4750
51+ class greeting_ipc_handler : public webframe ::handler
52+ {
53+ protected:
54+ void handle_post (const webframe::request* req, webframe::response* res) override
55+ {
56+ std::unique_ptr<yyjson_doc, decltype (&yyjson_doc_free)> request_body (nullptr , &yyjson_doc_free);
57+ std::unique_ptr<yyjson_mut_doc, decltype (&yyjson_mut_doc_free)> response_body (nullptr , &yyjson_mut_doc_free);
58+ request_body.reset (yyjson_read (reinterpret_cast <const char *>(req->get_body ().first ), req->get_body ().second , 0 ));
59+ if (!request_body || !yyjson_is_obj (request_body->root )) {
60+ throw webframe::exception::bad_request;
61+ }
62+ yyjson_val* name_val = yyjson_obj_get (request_body->root , " name" );
63+ if (!name_val || !yyjson_is_str (name_val)) {
64+ throw webframe::exception::bad_request;
65+ }
66+ const char * name = yyjson_get_str (name_val);
67+
68+ response_body.reset (yyjson_mut_doc_new (nullptr ));
69+ yyjson_mut_val* root = yyjson_mut_obj (response_body.get ());
70+ const std::string greeting = " Hello, " + std::string (name) + " !" ;
71+ yyjson_mut_obj_add_strcpy (response_body.get (), root, " greeting" , greeting.c_str ());
72+ yyjson_mut_doc_set_root (response_body.get (), root);
73+
74+ size_t response_len;
75+ const char *response_data = yyjson_mut_write (response_body.get (), 0 , &response_len);
76+ res->set_status (200 );
77+ res->set_header (" Content-Type" , " application/json" );
78+ res->set_body (reinterpret_cast <const uint8_t *>(response_data), response_len);
79+ free (const_cast <char *>(response_data));
80+ }
81+ };
4882
4983class example_application : public webframe ::application
5084{
@@ -53,9 +87,11 @@ class example_application : public webframe::application
5387 {
5488 _archive_handler.open (get_archive_path ());
5589 router->set_default (&_archive_handler);
90+ router->add_route (" /greetingIPC" , &_greeting_ipc_handler);
5691 }
5792private:
5893 archive_handler _archive_handler;
94+ greeting_ipc_handler _greeting_ipc_handler;
5995};
6096
6197
0 commit comments