Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
bastianhjaeger committed Nov 17, 2020
0 parents commit b3536e2
Show file tree
Hide file tree
Showing 20 changed files with 23,648 additions and 0 deletions.
35 changes: 35 additions & 0 deletions .github/workflows/debug.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
name: ElastiCpp-Debug

on:
push:
branches: [ master ]
pull_request:
branches: [ master ]

jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Configure sysctl limits for Elasticsearch
run: |
sudo swapoff -a
sudo sysctl -w vm.swappiness=1
sudo sysctl -w fs.file-max=262144
sudo sysctl -w vm.max_map_count=262144
- name: Runs Elasticsearch
uses: elastic/elastic-github-actions/elasticsearch@master
with:
stack-version: 7.6.0
- name: Install other dependencies
run: sudo apt-get install libcurl4-openssl-dev
- name: Install gtest manually
run: sudo apt-get install libgtest-dev && ELASTIC_BUILD_DIR=$(pwd) && cd /usr/src/gtest && sudo cmake CMakeLists.txt && sudo make && sudo cp *.a /usr/lib && sudo ln -s /usr/lib/libgtest.a /usr/local/lib/libgtest.a && sudo ln -s /usr/lib/libgtest_main.a /usr/local/lib/libgtest_main.a
- name: Return to build dir
run: cd $ELASTIC_BUILD_DIR
- uses: actions/checkout@v1
- name: configure
run: mkdir build && cd build && cmake -DCMAKE_BUILD_TYPE=Debug -DCMAKE_CXX_FLAGS="-Werror" ..
- name: make
run: cd build && make
- name: Run Test
run: /home/runner/work/elasticpp/elasticpp/build/test/elasticpp.test
35 changes: 35 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
name: ElastiCpp-Release

on:
push:
branches: [ master ]
pull_request:
branches: [ master ]

jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Configure sysctl limits for Elasticsearch
run: |
sudo swapoff -a
sudo sysctl -w vm.swappiness=1
sudo sysctl -w fs.file-max=262144
sudo sysctl -w vm.max_map_count=262144
- name: Runs Elasticsearch
uses: elastic/elastic-github-actions/elasticsearch@master
with:
stack-version: 7.6.0
- name: Install other dependencies
run: sudo apt-get install libcurl4-openssl-dev
- name: Install gtest manually
run: sudo apt-get install libgtest-dev && ELASTIC_BUILD_DIR=$(pwd) && cd /usr/src/gtest && sudo cmake CMakeLists.txt && sudo make && sudo cp *.a /usr/lib && sudo ln -s /usr/lib/libgtest.a /usr/local/lib/libgtest.a && sudo ln -s /usr/lib/libgtest_main.a /usr/local/lib/libgtest_main.a
- name: Return to build dir
run: cd $ELASTIC_BUILD_DIR
- uses: actions/checkout@v1
- name: configure
run: mkdir build && cd build && cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_CXX_FLAGS="-Werror" ..
- name: make
run: cd build && make
- name: Run Test
run: /home/runner/work/elasticpp/elasticpp/build/test/elasticpp.test
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.project
.cprojects
build/
43 changes: 43 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
cmake_minimum_required (VERSION 3.10.2)
project (elasticpp)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O3 -g")


execute_process (
COMMAND
/bin/bash -c "printf $(git describe --tags)$(git diff --quiet || echo '-dirty')"
OUTPUT_VARIABLE
git_version)
if(NOT DEFINED ${git_version})
set(git_version "v0.0.0")
endif(NOT DEFINED ${git_version})
string(FIND ${git_version} "-" end_of_version)
if(${end_of_version} LESS 1)
string(SUBSTRING ${git_version} 1 -1 ELASTICPP_VERSION_SHORT)
else()
MATH(EXPR end_of_version "${end_of_version}-1")
string(SUBSTRING ${git_version} 1 ${end_of_version} ELASTICPP_VERSION_SHORT)
endif(${end_of_version} LESS 1)


string(REPLACE "." ";" VERSION_LIST ${ELASTICPP_VERSION_SHORT})
list(GET VERSION_LIST 0 ELASTICPP_VERSION_MAJOR)
list(GET VERSION_LIST 1 ELASTICPP_VERSION_MINOR)
list(GET VERSION_LIST 2 ELASTICPP_VERSION_PATCH)
set(ELASTICPP_VERSION ${git_version})

add_definitions(-D${PROJECT_NAME}_VERSION=\"${git_version}\")
add_definitions(-D${PROJECT_NAME}_VERSION_MAJOR=\"${ELASTICPP_VERSION_MAJOR}\")
add_definitions(-D${PROJECT_NAME}_VERSION_MINOR=\"${ELASTICPP_VERSION_MINOR}\")
add_definitions(-D${PROJECT_NAME}_VERSION_PATCH=\"${ELASTICPP_VERSION_PATCH}\")


set(main_lib_dest "lib")
set(include_dest "include/${PROJECT_NAME}")
set(lib_dest "${main_lib_dest}")
set(tool_dest "bin")

add_subdirectory(src)
add_subdirectory(test)
54 changes: 54 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# General
![ElastiCpp-Release](https://github.com/bastianhjaeger/elasticpp/workflows/ElastiCpp-Release/badge.svg?branch=master)
![ElastiCpp-Debug](https://github.com/bastianhjaeger/elasticpp/workflows/ElastiCpp-Debug/badge.svg?branch=master)

Interface to manipulate elasticsearch by REST API from c++.

# Installation
```bash
git clone [email protected]:bastianhjaeger/elasticpp.git
cd elasticpp
mkdir build && cd build
cmake -DCMAKE_BUILD_TYPE=Release ..
make -j8
sudo make install
```

# Usage

## plain functions
```c++
#include <elasticpp/elasticpp.h>

const bool singleCall = true;
const bool verbose = true;

const std::string sResponse
= elastic_cpp("http://localhost:9200/test", "", "", PUT, singleCall, verbose);
const json jResponse
= elastic_cpp("http://localhost:9200/test", "", json(), PUT);
```

## class implementation
```c++
#include <elasticpp/ElastiCpp.h>

const int verbose = 0;
const int shards = 1;
const int replicas = 1;
const std::string url = "test_index";

ElastiCpp ec("http://localhost:9200/", "user:password");

bool success = ec.createIndex(url, shards, replicas, verbose);

const json response = ec.getIndex(url, verbose);
std::cout << "response: " << response.dump(3) << std::endl;

success = ec.deleteIndex(url, verbose);

// or any randon message/query
const json response2 = ec.send("index_*/_search", R"({"query": {"match_all": {}}})");
```
# Othres
Thanks to JSON for Modern C++ (version 3.7.2) https://github.com/nlohmann/json
25 changes: 25 additions & 0 deletions include/elasticpp/ElastiCpp.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#pragma once

#include <elasticpp/elasticpp.h>

namespace elasticpp {

class ElastiCpp {
friend class ElastiCppTest;
public:
ElastiCpp(const std::string& _baseUrl, const std::string& _credentials = "");
~ElastiCpp();

bool createIndex(const std::string& _index, int _shards = 1, int _replicas = 1, int _verbose = 0) const;
bool deleteIndex(const std::string& _index, int _verbose = 0) const;
json getIndex(const std::string& _index, int _verbose = 0) const;

json send(const std::string& _url, const json& _message, ELASTICPP_CURL_CMD _cmdType = POST,
int _verbose = 0) const;

private:
const std::string baseUrl_; // e.g. "http://mydomain.com:9200/"
const std::string credentials_; // e.g. "elastic:awesomepassword"
};

} // namespace elasticpp
42 changes: 42 additions & 0 deletions include/elasticpp/defines.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#pragma once

#include <stdio.h>
#include <sstream>


#if __GNUC__ > 7
#include <filesystem>
namespace fs = std::filesystem;
#else
#include <experimental/filesystem>
namespace fs = std::experimental::filesystem;
#endif


#define ERR_STRING "\033[1;31m[ERROR]\033[0m"
#define DBG_STRING "\033[1;32m[DEBUG]\033[0m"
#define INFO_STRING "\033[1;34m[INFO] \033[0m"

#if 1
#define error(msg) \
std::cerr << ERR_STRING; \
std::cerr << " " << msg << std::endl;
#else
#define error(msg)
#endif

#if 0
#define debug(msg) \
std::cout << DBG_STRING; \
std::cout << " " << msg << std::endl;
#else
#define debug(msg)
#endif

#if 1
#define info(msg) \
std::cout << INFO_STRING; \
std::cout << " " << msg << std::endl;
#else
#define info(msg)
#endif
23 changes: 23 additions & 0 deletions include/elasticpp/elasticpp.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#pragma once

#include <iostream>

#include <elasticpp/json.hpp>
using json = nlohmann::json;

namespace elasticpp {

enum ELASTICPP_CURL_CMD {
POST, PUT, GET, DELETE
};

void elastic_cpp_curl_cleanup();

void elastic_cpp_curl_init(const std::string& _credentials);

std::string elastic_cpp(const std::string& _url, const std::string& _credentials, const std::string& _message,
ELASTICPP_CURL_CMD _cmdType = POST, bool _singleCall = false, int _verbose = 0);
json elastic_cpp(const std::string& _url, const std::string& _credentials, const json& _message,
ELASTICPP_CURL_CMD _cmdType = POST, bool _singleCall = false, int _verbose = 0);

} // namespace elasticpp
Loading

0 comments on commit b3536e2

Please sign in to comment.