From c0c7d8836671240a9aa18878394587b28ba84cbe Mon Sep 17 00:00:00 2001 From: Nathan Osman Date: Sat, 5 Aug 2017 06:23:06 -0700 Subject: [PATCH] Add server auth example. --- doc/index.md | 2 +- examples/auth/CMakeLists.txt | 14 +++++++++- examples/auth/client.cpp | 2 -- examples/auth/server.cpp | 50 ++++++++++++++++++++++++++++++++++++ 4 files changed, 64 insertions(+), 4 deletions(-) create mode 100644 examples/auth/server.cpp diff --git a/doc/index.md b/doc/index.md index 939f96f..b1b3e8d 100644 --- a/doc/index.md +++ b/doc/index.md @@ -21,7 +21,7 @@ QHttpEngine is designed in a portable way, so it may run on other compilers and ## Build Instructions -QHttpEngine uses CMake for building the library. The library recognizes four options during configuration, all of which are disabled by default (the library is built as a shared library): +QHttpEngine uses CMake for building the library. The library recognizes three options during configuration, all of which are disabled by default (the library is built as a shared library): - `BUILD_DOC` - (requires Doxygen) generates documentation from the comments in the source code - `BUILD_EXAMPLES` - builds the sample applications that demonstrate how to use QHttpEngine diff --git a/examples/auth/CMakeLists.txt b/examples/auth/CMakeLists.txt index 61563b1..8647ae3 100644 --- a/examples/auth/CMakeLists.txt +++ b/examples/auth/CMakeLists.txt @@ -1,7 +1,19 @@ # Client add_executable(authclient client.cpp) target_link_libraries(authclient qhttpengine) -target_compile_features(authclient PRIVATE cxx_lambdas) +set_target_properties(authclient PROPERTIES + CXX_STANDARD 11 +) install(TARGETS authclient RUNTIME DESTINATION "${EXAMPLE_DIR}" ) + +# Server +add_executable(authserver server.cpp) +target_link_libraries(authserver qhttpengine) +set_target_properties(authserver PROPERTIES + CXX_STANDARD 11 +) +install(TARGETS authserver + RUNTIME DESTINATION "${EXAMPLE_DIR}" +) diff --git a/examples/auth/client.cpp b/examples/auth/client.cpp index 9816bd3..16182be 100644 --- a/examples/auth/client.cpp +++ b/examples/auth/client.cpp @@ -20,8 +20,6 @@ * IN THE SOFTWARE. */ -#include - #include #include #include diff --git a/examples/auth/server.cpp b/examples/auth/server.cpp new file mode 100644 index 0000000..f280244 --- /dev/null +++ b/examples/auth/server.cpp @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2017 Nathan Osman + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + */ + +#include +#include +#include + +#include +#include +#include +#include + +int main(int argc, char * argv[]) +{ + QCoreApplication a(argc, argv); + + QHttpEngine::QObjectHandler handler; + QHttpEngine::Server server(&handler); + if (!server.listen(QHostAddress::LocalHost)) { + qCritical("unable to bind to a local port"); + a.exit(1); + } + + QHttpEngine::LocalAuthMiddleware middleware; + middleware.setData(QVariantMap{ + {"port", server.serverPort()}, + }); + handler.addMiddleware(&middleware); + + return a.exec(); +}