From 4dd36db312645239cf6d1bfa9e8283c1dc802795 Mon Sep 17 00:00:00 2001 From: "Denis V. Dedkov" Date: Wed, 29 May 2024 11:22:00 +0200 Subject: [PATCH] Initial commit --- .gitignore | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++ CMakeLists.txt | 35 +++++++++++++++++++++++ Dockerfile | 17 ++++++++++++ main.cpp | 53 +++++++++++++++++++++++++++++++++++ 4 files changed, 180 insertions(+) create mode 100644 .gitignore create mode 100644 CMakeLists.txt create mode 100644 Dockerfile create mode 100644 main.cpp diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d0b2fe7 --- /dev/null +++ b/.gitignore @@ -0,0 +1,75 @@ +# This file is used to ignore files which are generated +# ---------------------------------------------------------------------------- + +*~ +*.autosave +*.a +*.core +*.moc +*.o +*.obj +*.orig +*.rej +*.so +*.so.* +*_pch.h.cpp +*_resource.rc +*.qm +.#* +*.*# +core +!core/ +tags +.DS_Store +.directory +*.debug +Makefile* +*.prl +*.app +moc_*.cpp +ui_*.h +qrc_*.cpp +Thumbs.db +*.res +*.rc +/.qmake.cache +/.qmake.stash + +# qtcreator generated files +*.pro.user* +CMakeLists.txt.user* + +# xemacs temporary files +*.flc + +# Vim temporary files +.*.swp + +# Visual Studio generated files +*.ib_pdb_index +*.idb +*.ilk +*.pdb +*.sln +*.suo +*.vcproj +*vcproj.*.*.user +*.ncb +*.sdf +*.opensdf +*.vcxproj +*vcxproj.* + +# MinGW generated files +*.Debug +*.Release + +# Python byte code +*.pyc + +# Binaries +# -------- +*.dll +*.exe + +build/ diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..f85f831 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,35 @@ +cmake_minimum_required(VERSION 3.14) + +project(noolite-srv LANGUAGES CXX) + +set(CMAKE_CXX_STANDARD 17) +set(CMAKE_CXX_STANDARD_REQUIRED ON) + +add_executable(noolite-srv + main.cpp +) + +include(FetchContent) +# cpp-httplib +FetchContent_Declare( + httplib + GIT_REPOSITORY https://gogs.dended.keenetic.pro/ded/cpp-httplib.git + GIT_TAG master +) +FetchContent_MakeAvailable(httplib) + +# noolite +FetchContent_Declare( + noolite + GIT_REPOSITORY https://gogs.dended.keenetic.pro/ded/noolitelib.git + GIT_TAG master +) +FetchContent_MakeAvailable(noolite) + +target_link_libraries(noolite-srv httplib noolite) + +include(GNUInstallDirs) +install(TARGETS noolite-srv + LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} + RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} +) diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..3640ccc --- /dev/null +++ b/Dockerfile @@ -0,0 +1,17 @@ +FROM alpine:latest AS builder +LABEL stage=builder + +RUN apk add --no-cache git cmake clang ninja libusb-dev + +WORKDIR /build +COPY . . + +RUN cmake CMakeLists.txt -DCMAKE_GENERATOR:STRING=Ninja -DCMAKE_BUILD_TYPE:STRING=MinSizeRel -DCMAKE_CXX_COMPILER:FILEPATH=/usr/bin/clang++-17 && cmake --build . --target all + +FROM alpine:latest + +run apk add --no-cache libstdc++ libusb + +COPY --from=builder /build/noolite-srv /bin/noolite-srv + +CMD ["/bin/noolite-srv"] diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..7ae4fa9 --- /dev/null +++ b/main.cpp @@ -0,0 +1,53 @@ +#include +#include +#include + +namespace App { + +constexpr auto DefaultPort = 8888; +constexpr auto DefaultHost = "0.0.0.0"; + +static httplib::Server *srv = nullptr; + +} + +void signalHandler(int signum) { + std::cout << "\n\nInterrupt signal (" << signum << ") received.\n"; + + if (App::srv) { + App::srv->stop(); + delete App::srv; + } + + exit(signum); +} + +int main(int argc, char *argv[]) +{ + std::signal(SIGINT, signalHandler); + + App::srv = new httplib::Server(); + + App::srv->Get("/", [](const httplib::Request &, httplib::Response &res) { + noolitelib::Noolite adapter; + if (adapter.sendCommand(0, noolitelib::Switch)) { + res.set_content("{\"Result\":\"Successful\"}", "application/json"); + } else { + res.set_content("{\"Result\":\"Failed\"}", "application/json"); + } + }); + + auto host = App::DefaultHost; + auto port = App::DefaultPort; + + if (argc > 1) { + host = argv[1]; + } + if (argc > 2) { + port = atoi(argv[2]); + } + + App::srv->listen(host, port); + + return 0; +}