diff --git a/.gitignore b/.gitignore index c8e9fe5..a73eb30 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,2 @@ -anthracite -src/error_pages/ -src/build/version.cpp +build/ +build_supp/version.cpp diff --git a/src/CMakeLists.txt b/CMakeLists.txt similarity index 54% rename from src/CMakeLists.txt rename to CMakeLists.txt index 719ff21..cd1ae89 100644 --- a/src/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,10 +6,17 @@ set(CMAKE_CXX_STANDARD 23) set(CMAKE_CXX_STANDARD_REQUIRED True) set(CMAKE_CXX_FLAGS_RELEASE "-O3") +add_custom_target(build-version + COMMAND cd ../build_supp && ./version.sh + DEPENDS build_supp/version.txt + COMMENT "Generated supplemental build files (version)" +) add_custom_target(build-supplemental - COMMAND cd ../build_supp && ./version.sh && python3 ./error_gen.py + COMMAND cd ../build_supp && python3 ./error_gen.py COMMAND cp -r ../www . + DEPENDS build_supp/version.txt www/* build_supp/error_gen.py build-version + COMMENT "Generated supplemental build files (default www dir + error pages)" ) add_custom_target(lint @@ -20,10 +27,6 @@ add_custom_target(lint-fix COMMAND clang-tidy *.cpp -fix -fix-errors ) -add_custom_target(format - COMMAND clang-format *.cpp -i -) - add_custom_target(build-docker COMMAND docker build .. -t anthracite ) @@ -34,15 +37,16 @@ add_custom_target(run WORKING_DIRECTORY ${CMAKE_PROJECT_DIR} ) -FILE(GLOB SOURCES **/*.cpp) +FILE(GLOB SOURCES src/**/*.cpp build_supp/version.cpp) -add_library(anthracite anthracite_main.cpp ${SOURCES}) +add_library(anthracite src/anthracite.cpp ${SOURCES}) +add_dependencies(anthracite build-version) -add_executable(anthracite-bin file_main.cpp) +add_executable(anthracite-bin src/file_main.cpp) target_link_libraries(anthracite-bin anthracite) - -add_executable(anthracite-api-bin api_main.cpp) -target_link_libraries(anthracite-api-bin anthracite) - add_dependencies(anthracite-bin build-supplemental) add_dependencies(anthracite-bin anthracite) + +add_executable(anthracite-api-bin src/api_main.cpp) +target_link_libraries(anthracite-api-bin anthracite) + diff --git a/src/build_supp/error_gen.py b/build_supp/error_gen.py similarity index 94% rename from src/build_supp/error_gen.py rename to build_supp/error_gen.py index 4c56751..79bfd7f 100644 --- a/src/build_supp/error_gen.py +++ b/build_supp/error_gen.py @@ -16,7 +16,7 @@ def generate_error_page(error_code, error_title):

{error_code} - {error_title}


Anthracite/{version}

-

This is Open Source Software

+

This is Open Source Software

""" diff --git a/src/build_supp/version.sh b/build_supp/version.sh similarity index 81% rename from src/build_supp/version.sh rename to build_supp/version.sh index 36c2f17..359e4b2 100755 --- a/src/build_supp/version.sh +++ b/build_supp/version.sh @@ -1,3 +1,4 @@ echo "#include " > version.cpp +echo "#include \"../src/version.hpp\"" >> version.cpp echo "const std::string ANTHRACITE_VERSION_STRING = \"$(cat version.txt)\";" >> version.cpp echo "const std::string ANTHRACITE_FULL_VERSION_STRING = \"Anthracite/$(cat version.txt)\";" >> version.cpp diff --git a/build_supp/version.txt b/build_supp/version.txt new file mode 100644 index 0000000..d510910 --- /dev/null +++ b/build_supp/version.txt @@ -0,0 +1 @@ +0.3.0-dev diff --git a/src/.gitignore b/src/.gitignore deleted file mode 100644 index 7211b0b..0000000 --- a/src/.gitignore +++ /dev/null @@ -1,13 +0,0 @@ -CMakeLists.txt.user -anthracite-bin -CMakeCache.txt -CMakeFiles -CMakeScripts -Testing -Makefile -cmake_install.cmake -install_manifest.txt -compile_commands.json -CTestTestfile.cmake -_deps -build/ diff --git a/src/anthracite_main.cpp b/src/anthracite.cpp similarity index 98% rename from src/anthracite_main.cpp rename to src/anthracite.cpp index d13daf0..ea77812 100644 --- a/src/anthracite_main.cpp +++ b/src/anthracite.cpp @@ -1,5 +1,5 @@ #include "backends/file_backend.hpp" -#include "./anthracite_main.hpp" +#include "./anthracite.hpp" #include #include #include @@ -9,7 +9,7 @@ #include #include #include "./log/log.hpp" -#include "./socket.hpp" +#include "./socket/socket.hpp" using namespace anthracite; diff --git a/src/anthracite_main.hpp b/src/anthracite.hpp similarity index 90% rename from src/anthracite_main.hpp rename to src/anthracite.hpp index a5c2aba..a4f6d2c 100644 --- a/src/anthracite_main.hpp +++ b/src/anthracite.hpp @@ -1,5 +1,3 @@ -#pragma once - #include "backends/backend.hpp" using namespace anthracite; diff --git a/src/api_main.cpp b/src/api_main.cpp index 6fdc284..15f7fc7 100644 --- a/src/api_main.cpp +++ b/src/api_main.cpp @@ -1,4 +1,4 @@ -#include "./anthracite_main.hpp" +#include "./anthracite.hpp" #include "backends/backend.hpp" #include "http/constants.hpp" #include diff --git a/src/build_supp/version.cpp b/src/build_supp/version.cpp deleted file mode 100644 index 590ee4e..0000000 --- a/src/build_supp/version.cpp +++ /dev/null @@ -1,3 +0,0 @@ -#include -const std::string ANTHRACITE_VERSION_STRING = "0.2.0"; -const std::string ANTHRACITE_FULL_VERSION_STRING = "Anthracite/0.2.0"; diff --git a/src/build_supp/version.txt b/src/build_supp/version.txt deleted file mode 100644 index 0ea3a94..0000000 --- a/src/build_supp/version.txt +++ /dev/null @@ -1 +0,0 @@ -0.2.0 diff --git a/src/file_main.cpp b/src/file_main.cpp index 5ffc071..efbb611 100644 --- a/src/file_main.cpp +++ b/src/file_main.cpp @@ -1,4 +1,4 @@ -#include "./anthracite_main.hpp" +#include "./anthracite.hpp" #include "backends/file_backend.hpp" using namespace anthracite; diff --git a/src/http/response.cpp b/src/http/response.cpp index 646af07..214ba49 100644 --- a/src/http/response.cpp +++ b/src/http/response.cpp @@ -1,5 +1,5 @@ #include "response.hpp" -#include "../build_supp/version.cpp" +#include "../version.hpp" namespace anthracite::http { diff --git a/src/socket.hpp b/src/socket/socket.cpp similarity index 100% rename from src/socket.hpp rename to src/socket/socket.cpp diff --git a/src/socket/socket.hpp b/src/socket/socket.hpp new file mode 100644 index 0000000..790ae9f --- /dev/null +++ b/src/socket/socket.hpp @@ -0,0 +1,90 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace anthracite::socket { + +constexpr int MAX_QUEUE_LENGTH = 100; + +class anthracite_socket { +private: + int server_socket; + int client_socket {}; + std::string client_ip; + struct sockaddr_in client_addr {}; + socklen_t client_addr_len {}; + static constexpr struct timeval timeout_tv { + .tv_sec = 5, .tv_usec = 0 + }; + +public: + anthracite_socket(int port, int max_queue = MAX_QUEUE_LENGTH) + : server_socket(::socket(AF_INET, SOCK_STREAM, 0)) + , client_ip("") + { + struct sockaddr_in address {}; + address.sin_family = AF_INET; + address.sin_port = htons(port); + address.sin_addr.s_addr = INADDR_ANY; + + int reuse_opt = 1; + setsockopt(server_socket, SOL_SOCKET, SO_REUSEADDR, &reuse_opt, sizeof(reuse_opt)); + bind(server_socket, reinterpret_cast(&address), sizeof(address)); + + listen(server_socket, max_queue); + } + + void wait_for_conn() + { + client_ip = ""; + client_socket = accept(server_socket, reinterpret_cast(&client_addr), &client_addr_len); + std::array ip_str { 0 }; + inet_ntop(AF_INET, &client_addr.sin_addr, ip_str.data(), INET_ADDRSTRLEN); + client_ip = std::string(ip_str.data()); + } + + const std::string& get_client_ip() + { + return client_ip; + } + + void close_conn() + { + close(client_socket); + client_socket = -1; + } + + void send_message(std::string& msg) + { + if (client_socket == -1) { + return; + } + send(client_socket, &msg[0], msg.length(), 0); + } + + std::string recv_message(int buffer_size) + { + if (client_socket == -1) { + return ""; + } + + setsockopt(client_socket, SOL_SOCKET, SO_RCVTIMEO, &timeout_tv, sizeof timeout_tv); + std::vector response(buffer_size + 1); + ssize_t result = recv(client_socket, response.data(), buffer_size + 1, 0); + + if (result < 1) { + return ""; + } + + response[buffer_size] = '\0'; + return { response.data() }; + } +}; + +}; diff --git a/src/test_www/images/emma.bmp b/src/test_www/images/emma.bmp deleted file mode 100644 index 30a8027..0000000 Binary files a/src/test_www/images/emma.bmp and /dev/null differ diff --git a/src/test_www/images/favicon.ico b/src/test_www/images/favicon.ico deleted file mode 100644 index edcb3e4..0000000 Binary files a/src/test_www/images/favicon.ico and /dev/null differ diff --git a/src/test_www/images/favicon_anim.ico b/src/test_www/images/favicon_anim.ico deleted file mode 100644 index 7004ff4..0000000 Binary files a/src/test_www/images/favicon_anim.ico and /dev/null differ diff --git a/src/test_www/images/lola.jpeg b/src/test_www/images/lola.jpeg deleted file mode 100644 index 106d145..0000000 Binary files a/src/test_www/images/lola.jpeg and /dev/null differ diff --git a/src/test_www/images/tini.png b/src/test_www/images/tini.png deleted file mode 100644 index 4ccd77e..0000000 Binary files a/src/test_www/images/tini.png and /dev/null differ diff --git a/src/test_www/test.css b/src/test_www/test.css deleted file mode 100644 index 15427d1..0000000 --- a/src/test_www/test.css +++ /dev/null @@ -1,18 +0,0 @@ -.cool-style { - background: linear-gradient(to right, #ef5350, #f48fb1, #7e57c2, #2196f3, #26c6da, #43a047, #eeff41, #f9a825, #ff5722); - -webkit-background-clip: text; - -webkit-text-fill-color: transparent; -} - -.content { - display: flex; - flex-wrap: wrap; - justify-content: center; - align-items: center; -} - -.content img { - max-width: 100%; - height: auto; - margin: 10px; -} diff --git a/src/test_www/test.html b/src/test_www/test.html deleted file mode 100644 index b969beb..0000000 --- a/src/test_www/test.html +++ /dev/null @@ -1,25 +0,0 @@ - - - - Anthracite - - - - -
-

Test Page!

-

Dogs

-
- A border collie - A border collie - A corgi -
-

Trains

-
- -
-
- - diff --git a/src/test_www/videos/train_vid.mp4 b/src/test_www/videos/train_vid.mp4 deleted file mode 100644 index 8db6d05..0000000 Binary files a/src/test_www/videos/train_vid.mp4 and /dev/null differ diff --git a/src/version.hpp b/src/version.hpp new file mode 100644 index 0000000..00c406a --- /dev/null +++ b/src/version.hpp @@ -0,0 +1,6 @@ +#pragma once + +#include + +extern const std::string ANTHRACITE_VERSION_STRING; +extern const std::string ANTHRACITE_FULL_VERSION_STRING; diff --git a/src/www/index.html b/src/www/index.html deleted file mode 100644 index 558da13..0000000 --- a/src/www/index.html +++ /dev/null @@ -1,9 +0,0 @@ - - Anthracite - - -
-

Anthracite is Running!

-

If you are seeing this page, then Anthracite is configured correctly!

-

Add files to the "www" directory to begin serving your website.

-
diff --git a/src/test_www/index.html b/www/index.html similarity index 100% rename from src/test_www/index.html rename to www/index.html