forked from venediktov/CRUD
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
79 lines (69 loc) · 2.68 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
//
// main.cpp
// ~~~~~~~~
//
// Copyright (c) 2003-2013 Christopher M. Kohlhoff (chris at kohlhoff dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
// Modified by Vladimir Venediktov :
// Adding use case for Restful handlers, converting Web server into Retful Webservice
//
#include <iostream>
#include <string>
#include <boost/asio.hpp>
#include "server.hpp"
#include "request_handler.hpp"
#include "../handlers/crud_dispatcher.hpp"
#include "request.hpp"
#include "reply.hpp"
int main(int argc, char* argv[])
{
try
{
// Check command line arguments.
if (argc != 4)
{
std::cerr << "Usage: http_server <address> <port> <doc_root>\n";
std::cerr << " For IPv4, try:\n";
std::cerr << " receiver 0.0.0.0 80 .\n";
std::cerr << " For IPv6, try:\n";
std::cerr << " receiver 0::0 80 .\n";
return 1;
}
// Initialise the server.
// http::server::request_handler handler(argv[3]);
// http::server::server<http::server::request_handler> s(argv[1], argv[2], handler);
typedef http::crud::crud_dispatcher<http::server::request, http::server::reply> restful_dispatcher_t;
restful_dispatcher_t handler(argv[3]) ;
// READ or CREAT/UPDATE "/venue_handler/XEMDP/123"
handler.crud_match(boost::regex("/venue_handler/(\\w+)/(\\d+)") )
.get([](http::server::reply & r, const http::crud::crud_match<boost::cmatch> & match) {
r << "name: " << match[1] << ", instance number: " << match[2]
<< http::server::reply::flush("text") ;
std::cout << "GET request=" << match[0] << std::endl;
})
.post([](http::server::reply & r, const http::crud::crud_match<boost::cmatch> & match) {
r << "name: " << match[1] << ", instance number: " << match[2]
<< http::server::reply::flush("test") ;
std::cout << "POST request=" << match[0] << std::endl;
std::cout << "POST request_data=" << match.data << std::endl;
}) ;
// READ "/venue_handler/FLO/"
handler.crud_match(boost::regex("/venue_handler/(\\w+)") )
.get([](http::server::reply & r, const http::crud::crud_match<boost::cmatch> & match) {
r << "name: " << match[1]
<< http::server::reply::flush("text") ;
std::cout << "GET request=" << match[0] << std::endl;
});
http::server::server<restful_dispatcher_t> s(argv[1], argv[2], handler);
// Run the server until stopped.
s.run();
}
catch (std::exception& e)
{
std::cerr << "exception: " << e.what() << "\n";
}
return 0;
}