ZRColaWS: Initial skeleton

Signed-off-by: Simon Rozman <simon@rozman.si>
This commit is contained in:
Simon Rozman 2022-09-15 18:18:33 +02:00
parent a6f0357ad8
commit a7c7a3f40c
8 changed files with 197 additions and 0 deletions

View File

@ -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).

3
ZRColaWS/.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
/*.d
/*.o
/zrcolad

21
ZRColaWS/Makefile Normal file
View File

@ -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)

32
ZRColaWS/appcomponent.h Normal file
View File

@ -0,0 +1,32 @@
/*
SPDX-License-Identifier: GPL-3.0-or-later
Copyright © 2022 Amebis
*/
#pragma once
#include <oatpp/core/macro/component.hpp>
#include <oatpp/network/tcp/server/ConnectionProvider.hpp>
#include <oatpp/parser/json/mapping/ObjectMapper.hpp>
#include <oatpp/web/server/HttpConnectionHandler.hpp>
class AppComponent
{
public:
OATPP_CREATE_COMPONENT(std::shared_ptr<oatpp::network::ServerConnectionProvider>, serverConnectionProvider)([] {
return oatpp::network::tcp::server::ConnectionProvider::createShared({"localhost", 8000, oatpp::network::Address::IP_4});
}());
OATPP_CREATE_COMPONENT(std::shared_ptr<oatpp::web::server::HttpRouter>, httpRouter)([] {
return oatpp::web::server::HttpRouter::createShared();
}());
OATPP_CREATE_COMPONENT(std::shared_ptr<oatpp::network::ConnectionHandler>, serverConnectionHandler)([] {
OATPP_COMPONENT(std::shared_ptr<oatpp::web::server::HttpRouter>, router);
return oatpp::web::server::HttpConnectionHandler::createShared(router);
}());
OATPP_CREATE_COMPONENT(std::shared_ptr<oatpp::data::mapping::ObjectMapper>, apiObjectMapper)([] {
return oatpp::parser::json::mapping::ObjectMapper::createShared();
}());
};

42
ZRColaWS/controller.h Normal file
View File

@ -0,0 +1,42 @@
/*
SPDX-License-Identifier: GPL-3.0-or-later
Copyright © 2022 Amebis
*/
#pragma once
#include "dto.h"
#include <oatpp/core/macro/codegen.hpp>
#include <oatpp/core/macro/component.hpp>
#include <oatpp/web/server/api/ApiController.hpp>
#include OATPP_CODEGEN_BEGIN(ApiController)
class MyController : public oatpp::web::server::api::ApiController
{
public:
MyController(OATPP_COMPONENT(std::shared_ptr<ObjectMapper>, 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<UserDto>, 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)

28
ZRColaWS/dto.h Normal file
View File

@ -0,0 +1,28 @@
/*
SPDX-License-Identifier: GPL-3.0-or-later
Copyright © 2022 Amebis
*/
#pragma once
#include <oatpp/core/data/mapping/type/Object.hpp>
#include <oatpp/core/macro/codegen.hpp>
#include <oatpp/core/Types.hpp>
#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)

30
ZRColaWS/test.html Normal file
View File

@ -0,0 +1,30 @@
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="Content-Language" content="sl">
</head>
<script type="text/javascript">
function submitJSON() {
var form = document.users;
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState == XMLHttpRequest.DONE)
form["res"].value = xhr.responseText;
}
xhr.open("POST", "http://localhost:8000/users", true);
xhr.setRequestHeader('Content-Type', 'application/json; charset=UTF-8');
xhr.send(form["req"].value);
}
</script>
<body>
<form name="users">
<h1>users</h1>
<p>
<textarea rows="10" name="req" cols="63">{"id":0, "name":"joe"}</textarea>
>>
<textarea rows="10" name="res" cols="63"></textarea>
</p>
<p><input type="submit" value="Submit" onclick="submitJSON(); return false"><input type="reset" value="Reset"></p>
</form>
</body>

28
ZRColaWS/zrcolaws.cpp Normal file
View File

@ -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 <oatpp/network/Server.hpp>
using namespace std;
int main()
{
oatpp::base::Environment::init();
AppComponent components;
OATPP_COMPONENT(std::shared_ptr<oatpp::web::server::HttpRouter>, router);
auto myController = std::make_shared<MyController>();
router->addController(myController);
OATPP_COMPONENT(std::shared_ptr<oatpp::network::ConnectionHandler>, connectionHandler);
OATPP_COMPONENT(std::shared_ptr<oatpp::network::ServerConnectionProvider>, 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;
}