From 483eb2f56657e8e7f419ab1a4fab8dce9ade8609 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sat, 27 Apr 2024 20:24:20 +0200 Subject: Adding upstream version 14.2.21. Signed-off-by: Daniel Baumann --- src/seastar/apps/httpd/CMakeLists.txt | 37 ++++++++++++++ src/seastar/apps/httpd/demo.json | 73 ++++++++++++++++++++++++++++ src/seastar/apps/httpd/main.cc | 91 +++++++++++++++++++++++++++++++++++ 3 files changed, 201 insertions(+) create mode 100644 src/seastar/apps/httpd/CMakeLists.txt create mode 100644 src/seastar/apps/httpd/demo.json create mode 100644 src/seastar/apps/httpd/main.cc (limited to 'src/seastar/apps/httpd') diff --git a/src/seastar/apps/httpd/CMakeLists.txt b/src/seastar/apps/httpd/CMakeLists.txt new file mode 100644 index 00000000..d8c48197 --- /dev/null +++ b/src/seastar/apps/httpd/CMakeLists.txt @@ -0,0 +1,37 @@ +# +# This file is open source software, licensed to you under the terms +# of the Apache License, Version 2.0 (the "License"). See the NOTICE file +# distributed with this work for additional information regarding copyright +# ownership. You may not use this file except in compliance with the License. +# +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# + +# +# Copyright (C) 2018 Scylladb, Ltd. +# + +seastar_generate_swagger ( + TARGET app_httpd_swagger + VAR app_httpd_swagger_file + IN_FILE ${CMAKE_CURRENT_SOURCE_DIR}/demo.json + OUT_FILE ${CMAKE_CURRENT_BINARY_DIR}/demo.json.hh) + +seastar_add_app (httpd + SOURCES + ${app_httpd_swagger_file} + main.cc) + +target_include_directories (app_httpd + PRIVATE ${CMAKE_CURRENT_BINARY_DIR}) + +add_dependencies (app_httpd app_httpd_swagger) diff --git a/src/seastar/apps/httpd/demo.json b/src/seastar/apps/httpd/demo.json new file mode 100644 index 00000000..12261c45 --- /dev/null +++ b/src/seastar/apps/httpd/demo.json @@ -0,0 +1,73 @@ +{ + "apiVersion": "0.0.1", + "swaggerVersion": "1.2", + "basePath": "{{Protocol}}://{{Host}}", + "resourcePath": "/hello", + "produces": [ + "application/json" + ], + "apis": [ + { + "path": "/hello/world/{var1}/{var2}", + "operations": [ + { + "method": "GET", + "summary": "Returns the number of seconds since the system was booted", + "type": "long", + "nickname": "hello_world", + "produces": [ + "application/json" + ], + "parameters": [ + { + "name":"var2", + "description":"Full path of file or directory", + "required":true, + "allowMultiple":true, + "type":"string", + "paramType":"path" + }, + { + "name":"var1", + "description":"Full path of file or directory", + "required":true, + "allowMultiple":false, + "type":"string", + "paramType":"path" + }, + { + "name":"query_enum", + "description":"The operation to perform", + "required":true, + "allowMultiple":false, + "type":"string", + "paramType":"query", + "enum":["VAL1", "VAL2", "VAL3"] + } + ] + } + ] + } + ], + "models" : { + "my_object": { + "id": "my_object", + "description": "Demonstrate an object", + "properties": { + "var1": { + "type": "string", + "description": "The first parameter in the path" + }, + "var2": { + "type": "string", + "description": "The second parameter in the path" + }, + "enum_var" : { + "type": "string", + "description": "Demonstrate an enum returned, note this is not the same enum type of the request", + "enum":["VAL1", "VAL2", "VAL3"] + } + } + } + } +} diff --git a/src/seastar/apps/httpd/main.cc b/src/seastar/apps/httpd/main.cc new file mode 100644 index 00000000..62af9434 --- /dev/null +++ b/src/seastar/apps/httpd/main.cc @@ -0,0 +1,91 @@ +/* + * This file is open source software, licensed to you under the terms + * of the Apache License, Version 2.0 (the "License"). See the NOTICE file + * distributed with this work for additional information regarding copyright + * ownership. You may not use this file except in compliance with the License. + * + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +/* + * Copyright 2015 Cloudius Systems + */ + +#include +#include +#include +#include +#include "demo.json.hh" +#include + +namespace bpo = boost::program_options; + +using namespace seastar; +using namespace httpd; + +class handl : public httpd::handler_base { +public: + virtual future > handle(const sstring& path, + std::unique_ptr req, std::unique_ptr rep) { + rep->_content = "hello"; + rep->done("html"); + return make_ready_future>(std::move(rep)); + } +}; + +void set_routes(routes& r) { + function_handler* h1 = new function_handler([](const_req req) { + return "hello"; + }); + function_handler* h2 = new function_handler([](std::unique_ptr req) { + return make_ready_future("json-future"); + }); + r.add(operation_type::GET, url("/"), h1); + r.add(operation_type::GET, url("/jf"), h2); + r.add(operation_type::GET, url("/file").remainder("path"), + new directory_handler("/")); + demo_json::hello_world.set(r, [] (const_req req) { + demo_json::my_object obj; + obj.var1 = req.param.at("var1"); + obj.var2 = req.param.at("var2"); + demo_json::ns_hello_world::query_enum v = demo_json::ns_hello_world::str2query_enum(req.query_parameters.at("query_enum")); + // This demonstrate enum conversion + obj.enum_var = v; + return obj; + }); +} + +int main(int ac, char** av) { + app_template app; + app.add_options()("port", bpo::value()->default_value(10000), + "HTTP Server port"); + return app.run_deprecated(ac, av, [&] { + auto&& config = app.configuration(); + uint16_t port = config["port"].as(); + auto server = new http_server_control(); + auto rb = make_shared("apps/httpd/"); + server->start().then([server] { + return server->set_routes(set_routes); + }).then([server, rb]{ + return server->set_routes([rb](routes& r){rb->set_api_doc(r);}); + }).then([server, rb]{ + return server->set_routes([rb](routes& r) {rb->register_function(r, "demo", "hello world application");}); + }).then([server, port] { + return server->listen(port); + }).then([server, port] { + std::cout << "Seastar HTTP server listening on port " << port << " ...\n"; + engine().at_exit([server] { + return server->stop(); + }); + }); + + }); +} -- cgit v1.2.3