]>
Commit | Line | Data |
---|---|---|
11fdf7f2 TL |
1 | /* |
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. | |
6 | * | |
7 | * You may obtain a copy of the License at | |
8 | * | |
9 | * http://www.apache.org/licenses/LICENSE-2.0 | |
10 | * | |
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 | |
16 | * under the License. | |
17 | */ | |
18 | /* | |
19 | * Copyright 2015 Cloudius Systems | |
20 | */ | |
21 | ||
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> | |
28 | ||
29 | namespace bpo = boost::program_options; | |
30 | ||
31 | using namespace seastar; | |
32 | using namespace httpd; | |
33 | ||
34 | class handl : public httpd::handler_base { | |
35 | public: | |
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"; | |
39 | rep->done("html"); | |
40 | return make_ready_future<std::unique_ptr<reply>>(std::move(rep)); | |
41 | } | |
42 | }; | |
43 | ||
44 | void set_routes(routes& r) { | |
45 | function_handler* h1 = new function_handler([](const_req req) { | |
46 | return "hello"; | |
47 | }); | |
48 | function_handler* h2 = new function_handler([](std::unique_ptr<request> req) { | |
49 | return make_ready_future<json::json_return_type>("json-future"); | |
50 | }); | |
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 | |
61 | obj.enum_var = v; | |
62 | return obj; | |
63 | }); | |
64 | } | |
65 | ||
66 | int main(int ac, char** av) { | |
67 | app_template app; | |
68 | app.add_options()("port", bpo::value<uint16_t>()->default_value(10000), | |
69 | "HTTP Server port"); | |
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/"); | |
9f95a23c | 75 | return server->start().then([server] { |
11fdf7f2 TL |
76 | return server->set_routes(set_routes); |
77 | }).then([server, rb]{ | |
78 | return server->set_routes([rb](routes& r){rb->set_api_doc(r);}); | |
79 | }).then([server, rb]{ | |
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(); | |
87 | }); | |
88 | }); | |
89 | ||
90 | }); | |
91 | } |