From a7c7a3f40c5dd51082eb0cc07538e8442fd88c47 Mon Sep 17 00:00:00 2001 From: Simon Rozman Date: Thu, 15 Sep 2022 18:18:33 +0200 Subject: [PATCH] ZRColaWS: Initial skeleton Signed-off-by: Simon Rozman --- README.md | 13 +++++++++++++ ZRColaWS/.gitignore | 3 +++ ZRColaWS/Makefile | 21 +++++++++++++++++++++ ZRColaWS/appcomponent.h | 32 +++++++++++++++++++++++++++++++ ZRColaWS/controller.h | 42 +++++++++++++++++++++++++++++++++++++++++ ZRColaWS/dto.h | 28 +++++++++++++++++++++++++++ ZRColaWS/test.html | 30 +++++++++++++++++++++++++++++ ZRColaWS/zrcolaws.cpp | 28 +++++++++++++++++++++++++++ 8 files changed, 197 insertions(+) create mode 100644 ZRColaWS/.gitignore create mode 100644 ZRColaWS/Makefile create mode 100644 ZRColaWS/appcomponent.h create mode 100644 ZRColaWS/controller.h create mode 100644 ZRColaWS/dto.h create mode 100644 ZRColaWS/test.html create mode 100644 ZRColaWS/zrcolaws.cpp diff --git a/README.md b/README.md index 82a0df4..00b96f3 100644 --- a/README.md +++ b/README.md @@ -56,6 +56,19 @@ Use Microsoft NMAKE to build the project. The resulting files can be found in ou The `/ls` flag can be appended to the commands above to reduce NMAKE's verbosity. You can combine multiple targets (e.g. nmake Unregister Clean). Please, see NMAKE reference for further reading. +## Building ZRCola webservice + +### Building Environment Requirements +- Linux +- _gcc_, _make_ +- [oat++ 1.3.0](https://oatpp.io/) + +### oat++ +ZRCola webservice is using oat++ library. You have to compile the library and install it to `/usr/local`. + +### Building +Use `make -C ZRColaWS all` to build the project. + ## Translating ZRCola Instructions how to translate ZRCola to your language can be found [here](LOCALIZATION.md). diff --git a/ZRColaWS/.gitignore b/ZRColaWS/.gitignore new file mode 100644 index 0000000..3ebdf7c --- /dev/null +++ b/ZRColaWS/.gitignore @@ -0,0 +1,3 @@ +/*.d +/*.o +/zrcolad diff --git a/ZRColaWS/Makefile b/ZRColaWS/Makefile new file mode 100644 index 0000000..e173e95 --- /dev/null +++ b/ZRColaWS/Makefile @@ -0,0 +1,21 @@ +CPPFLAGS := $(CPPFLAGS) -I../lib/libZRCola/include -I../lib/stdex/include -I/usr/local/include/oatpp-1.3.0/oatpp +LDFLAGS := $(LDFLAGS) -lstdc++ -L../lib/libZRCola/lib -lZRCola -L/usr/local/lib64/oatpp-1.3.0 -loatpp +SRCS := zrcolaws.cpp + +include ../include/props.mak + +.PHONY: all +all: zrcolad + +zrcolad: ../lib/libZRCola/lib/libZRCola.a $(OBJS) + $(CC) $(OBJS) -o $@ $(LDFLAGS) + +../lib/libZRCola/lib/libZRCola.a: + $(MAKE) $(MFLAGS) -C ../lib/libZRCola/build + +.PHONY: clean +clean: + -rm -r *.{d,o} zrcolad + +include ../include/targets.mak +-include $(DEPS) diff --git a/ZRColaWS/appcomponent.h b/ZRColaWS/appcomponent.h new file mode 100644 index 0000000..ab06483 --- /dev/null +++ b/ZRColaWS/appcomponent.h @@ -0,0 +1,32 @@ +/* + SPDX-License-Identifier: GPL-3.0-or-later + Copyright © 2022 Amebis +*/ + +#pragma once + +#include +#include +#include +#include + +class AppComponent +{ +public: + OATPP_CREATE_COMPONENT(std::shared_ptr, serverConnectionProvider)([] { + return oatpp::network::tcp::server::ConnectionProvider::createShared({"localhost", 8000, oatpp::network::Address::IP_4}); + }()); + + OATPP_CREATE_COMPONENT(std::shared_ptr, httpRouter)([] { + return oatpp::web::server::HttpRouter::createShared(); + }()); + + OATPP_CREATE_COMPONENT(std::shared_ptr, serverConnectionHandler)([] { + OATPP_COMPONENT(std::shared_ptr, router); + return oatpp::web::server::HttpConnectionHandler::createShared(router); + }()); + + OATPP_CREATE_COMPONENT(std::shared_ptr, apiObjectMapper)([] { + return oatpp::parser::json::mapping::ObjectMapper::createShared(); + }()); +}; diff --git a/ZRColaWS/controller.h b/ZRColaWS/controller.h new file mode 100644 index 0000000..4c5f0bd --- /dev/null +++ b/ZRColaWS/controller.h @@ -0,0 +1,42 @@ +/* + SPDX-License-Identifier: GPL-3.0-or-later + Copyright © 2022 Amebis +*/ + +#pragma once + +#include "dto.h" +#include +#include +#include + +#include OATPP_CODEGEN_BEGIN(ApiController) + +class MyController : public oatpp::web::server::api::ApiController +{ +public: + MyController(OATPP_COMPONENT(std::shared_ptr, objectMapper)) : oatpp::web::server::api::ApiController(objectMapper) {} + + ENDPOINT("GET", "/hello", hello) + { + auto dto = MessageDto::createShared(); + dto->statusCode = 0; + dto->message = "Hello World!"; + return createDtoResponse(Status::CODE_200, dto); + } + + ADD_CORS(postUsers) + ENDPOINT("POST", "/users", postUsers, BODY_DTO(Object, user)) + { + auto dto = MessageDto::createShared(); + dto->statusCode = 0; + std::string msg; + msg = "Hello "; + msg += user->name ? user->name->c_str() : "unknown"; + msg += "!"; + dto->message = msg; + return createDtoResponse(Status::CODE_200, dto); + } +}; + +#include OATPP_CODEGEN_END(ApiController) diff --git a/ZRColaWS/dto.h b/ZRColaWS/dto.h new file mode 100644 index 0000000..48b211f --- /dev/null +++ b/ZRColaWS/dto.h @@ -0,0 +1,28 @@ +/* + SPDX-License-Identifier: GPL-3.0-or-later + Copyright © 2022 Amebis +*/ + +#pragma once + +#include +#include +#include + +#include OATPP_CODEGEN_BEGIN(DTO) + +class MessageDto : public oatpp::DTO +{ + DTO_INIT(MessageDto, DTO) + DTO_FIELD(Int32, statusCode); + DTO_FIELD(String, message); +}; + +class UserDto : public oatpp::DTO +{ + DTO_INIT(UserDto, DTO) + DTO_FIELD(Int64, id); + DTO_FIELD(String, name); +}; + +#include OATPP_CODEGEN_END(DTO) diff --git a/ZRColaWS/test.html b/ZRColaWS/test.html new file mode 100644 index 0000000..06b3c8b --- /dev/null +++ b/ZRColaWS/test.html @@ -0,0 +1,30 @@ + + + + + + + +
+

users

+

+ + >> + +

+

+
+ + diff --git a/ZRColaWS/zrcolaws.cpp b/ZRColaWS/zrcolaws.cpp new file mode 100644 index 0000000..e335d40 --- /dev/null +++ b/ZRColaWS/zrcolaws.cpp @@ -0,0 +1,28 @@ +/* + SPDX-License-Identifier: GPL-3.0-or-later + Copyright © 2022 Amebis +*/ + +#include "../include/version.h" +#include "appcomponent.h" +#include "controller.h" +#include + +using namespace std; + +int main() +{ + oatpp::base::Environment::init(); + AppComponent components; + OATPP_COMPONENT(std::shared_ptr, router); + auto myController = std::make_shared(); + router->addController(myController); + OATPP_COMPONENT(std::shared_ptr, connectionHandler); + OATPP_COMPONENT(std::shared_ptr, connectionProvider); + oatpp::network::Server server(connectionProvider, connectionHandler); + OATPP_LOGI("ZRColaWS", "Server " PRODUCT_VERSION_STR " running on %s:%s", + connectionProvider->getProperty("host").getData(), connectionProvider->getProperty("port").getData()); + server.run(); + oatpp::base::Environment::destroy(); + return 0; +}