]>
git.proxmox.com Git - ceph.git/blob - ceph/src/seastar/apps/httpd/main.cc
2 * This file is open source software, licensed to you under the terms
3 * of the Apache License, Version 2.0 (the "License"). See the NOTICE file
4 * distributed with this work for additional information regarding copyright
5 * ownership. You may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing,
12 * software distributed under the License is distributed on an
13 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 * KIND, either express or implied. See the License for the
15 * specific language governing permissions and limitations
19 * Copyright 2015 Cloudius Systems
22 #include <seastar/http/httpd.hh>
23 #include <seastar/http/handlers.hh>
24 #include <seastar/http/function_handlers.hh>
25 #include <seastar/http/file_handler.hh>
26 #include "demo.json.hh"
27 #include <seastar/http/api_docs.hh>
29 namespace bpo
= boost::program_options
;
31 using namespace seastar
;
32 using namespace httpd
;
34 class handl
: public httpd::handler_base
{
36 virtual future
<std::unique_ptr
<reply
> > handle(const sstring
& path
,
37 std::unique_ptr
<request
> req
, std::unique_ptr
<reply
> rep
) {
38 rep
->_content
= "hello";
40 return make_ready_future
<std::unique_ptr
<reply
>>(std::move(rep
));
44 void set_routes(routes
& r
) {
45 function_handler
* h1
= new function_handler([](const_req req
) {
48 function_handler
* h2
= new function_handler([](std::unique_ptr
<request
> req
) {
49 return make_ready_future
<json::json_return_type
>("json-future");
51 r
.add(operation_type::GET
, url("/"), h1
);
52 r
.add(operation_type::GET
, url("/jf"), h2
);
53 r
.add(operation_type::GET
, url("/file").remainder("path"),
54 new directory_handler("/"));
55 demo_json::hello_world
.set(r
, [] (const_req req
) {
56 demo_json::my_object obj
;
57 obj
.var1
= req
.param
.at("var1");
58 obj
.var2
= req
.param
.at("var2");
59 demo_json::ns_hello_world::query_enum v
= demo_json::ns_hello_world::str2query_enum(req
.query_parameters
.at("query_enum"));
60 // This demonstrate enum conversion
66 int main(int ac
, char** av
) {
68 app
.add_options()("port", bpo::value
<uint16_t>()->default_value(10000),
70 return app
.run_deprecated(ac
, av
, [&] {
71 auto&& config
= app
.configuration();
72 uint16_t port
= config
["port"].as
<uint16_t>();
73 auto server
= new http_server_control();
74 auto rb
= make_shared
<api_registry_builder
>("apps/httpd/");
75 return server
->start().then([server
] {
76 return server
->set_routes(set_routes
);
78 return server
->set_routes([rb
](routes
& r
){rb
->set_api_doc(r
);});
80 return server
->set_routes([rb
](routes
& r
) {rb
->register_function(r
, "demo", "hello world application");});
81 }).then([server
, port
] {
82 return server
->listen(port
);
83 }).then([server
, port
] {
84 std::cout
<< "Seastar HTTP server listening on port " << port
<< " ...\n";
85 engine().at_exit([server
] {
86 return server
->stop();