Skip to content

Commit

Permalink
[C++] #3 Finish dockerize
Browse files Browse the repository at this point in the history
  • Loading branch information
seunghyukcho committed Nov 22, 2019
1 parent 601e1de commit 265afa2
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 12 deletions.
1 change: 1 addition & 0 deletions cpp/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
cmake-build-debug/
5 changes: 2 additions & 3 deletions cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
cmake_minimum_required(VERSION 3.15)
cmake_minimum_required(VERSION 3.10)
project(BrustaCPlusPlus)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

add_subdirectory(src)

26 changes: 26 additions & 0 deletions cpp/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
FROM ubuntu:18.04 AS build-env

RUN apt-get update -y
RUN apt-get install -y software-properties-common
RUN add-apt-repository ppa:ubuntu-toolchain-r/test
RUN apt-get update -y
RUN apt-get install -y git curl unzip tar cmake g++

WORKDIR /home

RUN git clone https://github.com/microsoft/vcpkg.git
RUN /home/vcpkg/bootstrap-vcpkg.sh
RUN /home/vcpkg/vcpkg install cpp-httplib jsoncpp
RUN mkdir -p /home/brusta

WORKDIR /home/brusta
ADD . /home/brusta

RUN mkdir -p build
RUN cd build && cmake -DCMAKE_TOOLCHAIN_FILE=/home/vcpkg/scripts/buildsystems/vcpkg.cmake ..
RUN cd build && make

WORKDIR /home/brusta/build/src
EXPOSE 8080

CMD ["./brusta"]
6 changes: 4 additions & 2 deletions cpp/src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
include_directories(include ../includes)

find_package(jsoncpp CONFIG REQUIRED)
add_executable(BrustaCPlusPlus
find_package(Threads)
add_executable(brusta
main.cpp
handler.cpp predict.cpp)
target_link_libraries(BrustaCPlusPlus PRIVATE jsoncpp_lib)

target_link_libraries(brusta ${CMAKE_THREAD_LIBS_INIT} jsoncpp_lib)
10 changes: 4 additions & 6 deletions cpp/src/handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,31 @@

#include "predict.h"
#include "handler.h"
#include <iostream>

static std::string VectorJsonify(const std::string& name, const std::vector<int>& input) {
std::string ret = "{\"" + name + "\": [";
std::string ret = "\"" + name + "\": [";

int sz = input.size();
for(int i = 0; i < sz; i++) {
ret += std::to_string(input[i]) + (i < sz - 1 ? "," : "]}");
ret += std::to_string(input[i]) + (i < sz - 1 ? "," : "]");
}

return ret;
}

void AliveCheckHandler(const httplib::Request& req, httplib::Response& res) {
res.set_content(R"({"status": "server is now running"})", "application/json");
res.status = 400;
}

void PredictHandler(const httplib::Request& req, httplib::Response& res) {
std::vector<std::vector<int> > inputs;
std::vector<int> output;

if(!ValidateInput(req.body, inputs)) {
res.set_content(R"({"message": "wrong input format", "status": 400})", "application/json");
res.set_content(R"({"message": "wrong input format"})", "application/json");
res.status = 400;
} else {
RunModel(inputs, output);
res.set_content(VectorJsonify("output", output), "application/json");
res.set_content("{" + VectorJsonify("output", output) + "}", "application/json");
}
}
2 changes: 1 addition & 1 deletion cpp/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ int main(int argc, char **argv) {
server.Get("/", AliveCheckHandler);
server.Post("/predict", PredictHandler);

server.listen("localhost", 8080);
server.listen("0.0.0.0", 8080);

return 0;
}

0 comments on commit 265afa2

Please sign in to comment.