11#include < webframe.hpp>
22
3- #include < hyperpage.hpp>
3+ #include < archive.hpp>
4+
5+ static std::string get_archive_path ()
6+ {
7+ std::string result (ARCHIVE_DIRECTORY );
8+ #ifdef _WIN32
9+ std::replace (result.begin (), result.end (), ' /' , ' \\ ' );
10+ result += " \\ " ;
11+ #else
12+ result += " /" ;
13+ #endif
14+ result += " hyperpage.db" ;
15+ return result;
16+ }
417
518class archive_handler : public webframe ::handler
619{
720public:
8- archive_handler (const std::string& archive_path)
21+ archive_handler () = default ;
22+ ~archive_handler () = default ;
23+ void open (const std::string& archive_path)
924 {
10- _archive = std::make_unique< hyperpage::reader> (archive_path);
25+ _archive. reset ( new hyperpage::reader (archive_path) );
1126 }
1227protected:
1328 void handle_get (const webframe::request* req, webframe::response* res) override
1429 {
15- std::string path = req->get_path ();
16- auto page = _archive->load (path );
30+ std::string page_path = req->get_path ();
31+ auto page = _archive->load (page_path );
1732 if (page) {
1833 res->set_status (200 );
1934 res->set_header (" Content-Type" , page->get_mime_type ());
2035 res->set_body (page->get_content (), page->get_length ());
21- }
22- else
23- {
24- throw webframe::exception::not_found;
36+ }
37+ else {
38+ res->set_status (404 );
39+ res->set_header (" Content-Type" , " text/plain" );
40+ const std::string not_found_msg = " 404 Not Found" ;
41+ res->set_body (reinterpret_cast <const uint8_t *>(not_found_msg.data ()), not_found_msg.size ());
2542 }
2643 }
2744private:
2845 std::unique_ptr<hyperpage::reader> _archive;
2946};
3047
31- class greeting_handler : public webframe ::handler
32- {
33- protected:
34- void handle_post (const webframe::request* req, webframe::response* res) override
35- {
36-
37- }
38- };
3948
4049class example_application : public webframe ::application
4150{
4251public:
43- void configure_router (webframe::router* router) override
52+ void configure_router (webframe::router * router) override
4453 {
45- _archive_handler = std::make_unique<archive_handler>( " path/to/archive " );
46- router->set_default (_archive_handler. get ());
54+ _archive_handler. open ( get_archive_path () );
55+ router->set_default (& _archive_handler);
4756 }
4857private:
49- std::unique_ptr< archive_handler> _archive_handler;
58+ archive_handler _archive_handler;
5059};
5160
52- WEBFRAME_MAIN (example_application)
61+
62+ WEBFRAME_MAIN (example_application)
0 commit comments