<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Sergey Inozemcev</title>
    <description>The latest articles on DEV Community by Sergey Inozemcev (@seemstechies).</description>
    <link>https://dev.to/seemstechies</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F695069%2Fd8185b9b-348a-435b-b0e9-47b37364800b.png</url>
      <title>DEV Community: Sergey Inozemcev</title>
      <link>https://dev.to/seemstechies</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/seemstechies"/>
    <language>en</language>
    <item>
      <title>Server with oat++. Installation. CmakeLists.txt</title>
      <dc:creator>Sergey Inozemcev</dc:creator>
      <pubDate>Wed, 07 Sep 2022 02:02:28 +0000</pubDate>
      <link>https://dev.to/seemstechies/server-with-oat-installation-cmakeliststxt-4gl3</link>
      <guid>https://dev.to/seemstechies/server-with-oat-installation-cmakeliststxt-4gl3</guid>
      <description>&lt;p&gt;I've always found it exciting to use C++ for back-end of my projects, but information on how to do this has to be pieced together. Difficulties are also added by the fact that most of the articles are intended for experienced developers, and much of them is omitted in the expectation that developers are quite familiar with server programming, the C++ language, and build systems.&lt;br&gt;
I will venture to write a series of articles about producing CRUD api for "Farm-fellows"(service for subscribing on farm foods), as if you are encountering many topics for the first time. First of all, I am writing these articles in order to structure the knowledge gained in my head, but if it helps you become more determined to use c++ for the server, I will be very happy.&lt;br&gt;
To create a server, I will use the oat ++ framework. I will be using Ubuntu Linux as my operating system. To save data, I will first use sqlite. Later I will also try to use mongo and postgres.&lt;br&gt;
As a build system, I will use Cmake. You also need git to be installed to clone oat++ and its modules.&lt;/p&gt;

&lt;p&gt;So let's start.&lt;br&gt;
First of all we need oat++ itself and two oat++ modules oatpp-sqlite and oatpp-swagger (for good looking representation of our future CRUD api)&lt;/p&gt;

&lt;p&gt;&lt;code&gt;cd "some/temp/path/for/repositories"&lt;br&gt;
git clone https://github.com/oatpp/oatpp.git&lt;br&gt;
cd oatpp&lt;br&gt;
mkdir build &amp;amp;&amp;amp; cd build&lt;br&gt;
cmake ..&lt;br&gt;
(sudo) make install&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;cd ../../ (back to repository root folder)&lt;br&gt;
git clone https://github.com/oatpp/oatpp-sqlite.git&lt;br&gt;
cd oatpp&lt;br&gt;
mkdir build &amp;amp;&amp;amp; cd build&lt;br&gt;
cmake ..&lt;br&gt;
(sudo) make install&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Note: You can also use -DOATPP_SQLITE_AMALGAMATION=ON to install oatpp-sqlite together with SQLite amalgamation in which case you don't need to install SQLite&lt;/p&gt;

&lt;p&gt;&lt;code&gt;cd ../../ (back to repository root folder)&lt;br&gt;
git clone https://github.com/oatpp/oatpp-swagger.git&lt;br&gt;
cd oatpp&lt;br&gt;
mkdir build &amp;amp;&amp;amp; cd build&lt;br&gt;
cmake ..&lt;br&gt;
(sudo) make install&lt;br&gt;
&lt;/code&gt;&lt;br&gt;
Oat++ and it's modules now will be added in /usr/local/oatpp-1.3.1/ folder as oatpp, oatpp-swagger, oatpp-sqlite includes&lt;/p&gt;

&lt;p&gt;You also need to add sqlite if you didn't use amalgamation sqlite files while making oatpp-sqlite module, so let's install it too.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;sudo apt update&lt;br&gt;
sudo apt install sqlite3&lt;br&gt;
sqlite3 --version&lt;br&gt;
sudo apt install sqlitebrowser&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now we ready to start code our project &lt;/p&gt;

&lt;p&gt;&lt;code&gt;cd /some/path/for/projects/root&lt;br&gt;
mkdir farm-fellows&lt;br&gt;
cd farm-fellows&lt;br&gt;
touch CMakeLists.txt&lt;br&gt;
mkdir src&lt;br&gt;
cd src&lt;br&gt;
touch App.cpp&lt;br&gt;
mkdir component&lt;br&gt;
cd component &lt;br&gt;
touch AppComponent.hpp&lt;br&gt;
cd ../../&lt;br&gt;
&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;So, for very begining we need at least two source files, one is App.cpp with main() function and AppComponent.hpp with several oatpp components which needs for running http server.&lt;/p&gt;

&lt;p&gt;Let's edit AppComponent.hpp first&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#ifndef AppComponent_hpp
#define AppComponent_hpp

#include "oatpp/core/macro/component.hpp"
#include "oatpp/web/server/HttpConnectionHandler.hpp"
#include "oatpp/network/tcp/server/ConnectionProvider.hpp"
#include "oatpp/parser/json/mapping/ObjectMapper.hpp"

class AppComponent {
public:

/**
*  Create ObjectMapper component to serialize/deserialize DTOs in Contoller's API
*/

OATPP_CREATE_COMPONENT(std::shared_ptr&amp;lt;oatpp::data::mapping::ObjectMapper&amp;gt;, apiObjectMapper)([] {
        return oatpp::parser::json::mapping::ObjectMapper::createShared();
    }());

/**
*  Create ConnectionProvider component which listens on the port
*/
    OATPP_CREATE_COMPONENT(std::shared_ptr&amp;lt;oatpp::network::ServerConnectionProvider&amp;gt;, serverConnectionProvider)([] {
        return oatpp::network::tcp::server::ConnectionProvider::createShared({SERVER_HOST, SERVER_PORT, oatpp::network::Address::IP_4});
    }());

 /**
 *  Create Router component
 */
    OATPP_CREATE_COMPONENT(std::shared_ptr&amp;lt;oatpp::web::server::HttpRouter&amp;gt;, httpRouter)([] {
        return oatpp::web::server::HttpRouter::createShared();
    }());

 /**
 *  Create ConnectionHandler component which uses Router component to route requests
 */
    OATPP_CREATE_COMPONENT(std::shared_ptr&amp;lt;oatpp::network::ConnectionHandler&amp;gt;, serverConnectionHandler)([] {
        OATPP_COMPONENT(std::shared_ptr&amp;lt;oatpp::web::server::HttpRouter&amp;gt;, router); // get Router component
            return oatpp::web::server::HttpConnectionHandler::createShared(router);
    }());
};

#endif
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Oat++ using macros OATPP_CREATE_COMPONENT with shared_ptr of component instance, we use lambda as argument for callback, when oatpp is deciding to declare new instance of component.&lt;/p&gt;

&lt;p&gt;Now we can add our components to App (src/App.cpp):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#include &amp;lt;iostream&amp;gt;

#include "./component/AppComponent.hpp"
#include "oatpp/network/Server.hpp"

void run() {

  AppComponent components; // Create scope Environment components

  /* Get connection handler component */
  OATPP_COMPONENT(std::shared_ptr&amp;lt;oatpp::network::ConnectionHandler&amp;gt;, connectionHandler);

  /* Get connection provider component */
  OATPP_COMPONENT(std::shared_ptr&amp;lt;oatpp::network::ServerConnectionProvider&amp;gt;, connectionProvider);

  /* create server */
  oatpp::network::Server server(connectionProvider,
                                connectionHandler);

  OATPP_LOGD("Server", "Running on port %s...", connectionProvider-&amp;gt;getProperty("port").toString()-&amp;gt;c_str());

  server.run();

}

/**
 *  main
 */
int main(int argc, const char * argv[]) {

  oatpp::base::Environment::init();

  run();

  oatpp::base::Environment::destroy();

  return 0;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we can edit our CMakeLists.txt&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cmake_minimum_required(VERSION 3.1)
project(farm-fellows)

set(CMAKE_CXX_STANDARD 17)

add_library(farm-fellows-lib
    src/component/AppComponent.hpp)

## include directories

target_include_directories(farm-fellows-lib PUBLIC src)

## link libs

## Oat++

find_package(oatpp          1.3.0 REQUIRED)
find_package(oatpp-swagger  1.3.0 REQUIRED)
find_package(oatpp-sqlite   1.3.0 REQUIRED)

target_link_libraries(farm-fellows-lib
        PUBLIC oatpp::oatpp
        PUBLIC oatpp::oatpp-swagger
        PUBLIC oatpp::oatpp-sqlite
)

## Sqlite3

find_package(SQLite3 REQUIRED)

target_link_libraries(farm-fellows-lib
        PUBLIC SQLite::SQLite3
)

if(CMAKE_SYSTEM_NAME MATCHES Linux)
    find_package(Threads REQUIRED)
    target_link_libraries(farm-fellows-lib INTERFACE Threads::Threads ${CMAKE_DL_LIBS})
endif()



add_definitions(
    # define host
    -DSERVER_HOST="127.0.0.0"

    # define port
    -DSERVER_PORT=8000

)

## add executables

add_executable(farm-fellows-exe src/App.cpp)
target_link_libraries(farm-fellows-exe farm-fellows-lib)

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In add_definitions we added two constants which used for ConnectionProvider instance initialization.&lt;/p&gt;

&lt;p&gt;Now we can make our very basic server:&lt;br&gt;
(you should be in project root path /farm-fellows)&lt;/p&gt;

&lt;p&gt;&lt;code&gt;mkdir build &amp;amp;&amp;amp; cd build&lt;br&gt;
cmake ..&lt;br&gt;
make&lt;br&gt;
./farm-fellows-exe&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;open 127.0.0.0:8000 in your browser. because of no controllers matching with responses yet we should see as response:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;server=oatpp/1.3.0&lt;br&gt;
code=404&lt;br&gt;
description=Not Found&lt;br&gt;
message=No mapping for HTTP-method: 'GET', URL: '/'&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>cpp</category>
      <category>oatpp</category>
      <category>backend</category>
      <category>cmake</category>
    </item>
    <item>
      <title>Fear and loathing in frontend</title>
      <dc:creator>Sergey Inozemcev</dc:creator>
      <pubDate>Sat, 28 Aug 2021 16:42:45 +0000</pubDate>
      <link>https://dev.to/seemstechies/fear-and-loathing-in-frontend-4aci</link>
      <guid>https://dev.to/seemstechies/fear-and-loathing-in-frontend-4aci</guid>
      <description>&lt;p&gt;We had two bags of react/redux, seventy-five pellets of react npm dependencies, five sheets of high-powered postcss preprocessors, a saltshaker half-full of craco overrides, and a whole multi colored collection of context, profile, environment, lint webpack plugins... Also, a quart of babel, a quart of decorators, a case of typed script promisses, a pint of raw ether jest, and two dozen of mocha tests. Not that we needed all that for the trip, but once you get into a serious frontend collection, the tendency is to push it as far as you can. The only thing that really worried me was the tailwind styled components. There is nothing in the world more helpless and irresponsible and depraved than a man puts all css generation in javascript, and I knew we'd get into that rotten stuff pretty soon.&lt;/p&gt;

</description>
      <category>typescript</category>
      <category>react</category>
      <category>webpack</category>
      <category>css</category>
    </item>
  </channel>
</rss>
