From 265edfc0b7fa2ed7392912dfcbb737b74fdfc8b7 Mon Sep 17 00:00:00 2001 From: wutno Date: Mon, 6 Feb 2023 19:08:46 -0500 Subject: [PATCH] Better API backend, add clang-format --- .clang-format | 412 +++++++++++++++++++++++++++++++++++++++++++++ CMakeLists.txt | 10 ++ Includes/WebIo.cpp | 183 ++++++++++++++++++++ Includes/WebIo.h | 79 +++++++++ main.cpp | 159 +---------------- public/blah.js | 2 +- public/index.html | 8 +- 7 files changed, 695 insertions(+), 158 deletions(-) create mode 100644 .clang-format create mode 100644 Includes/WebIo.cpp create mode 100644 Includes/WebIo.h diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..78a5756 --- /dev/null +++ b/.clang-format @@ -0,0 +1,412 @@ +# This file uses configuration options available in clang-format 15.0. +# +# More detailed description of all options: +# https://releases.llvm.org/12.0.0/tools/clang/docs/ClangFormatStyleOptions.html +# +# Note that version of clang-format provided by your distribution might be +# newer and provide additional options, that won't work in 8.x. +# +# This style definition should only be understood as a hint +# for writing new code. The rules are still work-in-progress and do +# not yet exactly match the style we have in the existing code. + + +# Use tabs, but only for indentation (lining up blocks of code). +# Use space for indenting continuations (lining up long statements broken +# into several lines. +# +UseTab: ForIndentation +TabWidth: 8 +IndentWidth: 8 +ContinuationIndentWidth: 8 + +ColumnLimit: 80 + +# C/C++ Language specifics +# +Language: Cpp +# in clang-format 9.x "Cpp11" covers formatting for C++11 and C++14 +# in clang-format 10.x: switch to "c++14" +Standard: c++17 + +# Align qualifiers with the type instead of the variable name +# int* var; // 'var' is a pointer-to-int +# int& var; // 'var' is a reference-to-int +# const int& var; // 'var' is a constant-reference-to-int +PointerAlignment: Left +ReferenceAlignment: Left +QualifierAlignment: Left + +# The extra indent or outdent of class access modifiers, e.g. public: +# +AccessModifierOffset: -8 + +# Align parameters on the open bracket +# +# someLongFunction(argument1, +# argument2); +# +AlignAfterOpenBracket: Align + +# If Consecutive, aligns consecutive C/C++ preprocessor macros. +# +# This will align the C/C++ preprocessor macros of consecutive lines. This will +# result in formattings like +# +# #define SHORT_NAME 42 +# #define LONGER_NAME 0x007f +# #define EVEN_LONGER_NAME (2) +# #define foo(x) (x * x) +# #define bar(y, z) (y + z) +# +AlignConsecutiveMacros: Consecutive + +# Don't align backslash character in macros, as it's unexpected during review +# and does not look right when user changes the tab size character in their editor. +# +AlignEscapedNewlines: DontAlign + +## +# This will align the assignment operators of consecutive lines. +# enum Enum { +# ONE = 1, +# TWO = 2, +# THREE = 3, +# FOUR = 4, +# FIVE = 5, +# SIX = 6, +# SEVEN = 7 +# }; +AlignConsecutiveAssignments: Consecutive + +## +# the operator is un-indented so that the wrapped operand is aligned with the operand on the first line. +# +# int aaa = bbbbbbbbbbbbbbb +# + ccccccccccccccc; +# +AlignOperands: AlignAfterOperator + +## +# // Indents directives after the hash. +# +# #if FOO +# # if BAR +# # include +# # else +# # include +# # endif +# #endif +# +# // This much easier to see what's happening versus: +# +# #if FOO +# #if BAR +# #include +# #else +# #include +# #endif +# #endif +# +IndentPPDirectives: AfterHash + +# Always place function bodies on their own lines because +# it makes a mess when some are on the same line and others are +# on separate lines. +# +AllowShortFunctionsOnASingleLine: Empty + +# Short case labels in switch can be contracted to a single line +# +# switch (x) { +# case 42: return x; +# }; +# +AllowShortCaseLabelsOnASingleLine: true + +# Always place if/else and loops bodies on the subsequent line +# +AllowShortIfStatementsOnASingleLine: false +AllowShortLoopsOnASingleLine: false + +# Always place parameter declarations in a separate line: +# +# template +# T foo() … +# +# NOT: +# +# template T foo() … +# +AlwaysBreakTemplateDeclarations: Yes + +# If true, always break before multiline string literals. +# +# This option means to make multiline string assignments nicely +# lined-up and reduce unnecessary breaks in string literal, e.g.: +# +# // true: +# very_long_var_name = +# "bbbb" +# "cccc"; +# shortname = +# "dddd" +# "eeee"; +# +# // false: +# very_long_var_name = "bbbb" +# "cccc"; +# shortname = "dddd" +# "eeee"; +# +# AlwaysBreakBeforeMultilineStrings: true + +# Insert braces after control statements (``if``, ``else``, ``for``, ``do``, +# and ``while``) in C++ unless the control statements are inside macro +# definitions or the braces would enclose preprocessor directives. +# +# == WARNING == +# Setting this option to `true` could lead to incorrect code formatting due +# to clang-format's lack of complete semantic information. As such, extra +# care should be taken to review code changes made by this option. +# +# false: true: +# +# if (isa(D)) vs. if (isa(D)) { +# handleFunctionDecl(D); handleFunctionDecl(D); +# else if (isa(D)) } else if (isa(D)) { +# handleVarDecl(D); handleVarDecl(D); +# else } else { +# return; return; +# } +# +# while (i--) vs. while (i--) { +# for (auto *A : D.attrs()) for (auto *A : D.attrs()) { +# handleAttr(A); handleAttr(A); +# } +# } +# do vs. do { +# --i; --i; +# while (i); +# +InsertBraces: true + +# Attach braces to surrounding context except break before braces on function +# definitions (also known as K&R indentation style). This is C-derived style, +# but it works very well in C++ edge cases (C++ constructors). +# +# void foo() +# { +# if (true) { +# } else { +# } +# } +# + +# Coming in Clang-format-14 +# AlignArrayOfStructures: Left +# +# if not None, when using initialization for an array of structs aligns the fields into columns. +# +# Possible values: + +# AIAS_Left (in configuration: Left) Align array column and left justify the columns e.g.: +# +# struct test demo[] = +# { +# {56, 23, "hello"}, +# {-1, 93463, "world"}, +# {7, 5, "!!" } +# }; +# +# AIAS_Right (in configuration: Right) Align array column and right justify the columns e.g.: +# +# struct test demo[] = +# { +# {56, 23, "hello"}, +# {-1, 93463, "world"}, +# { 7, 5, "!!"} +# }; +# +# AIAS_None (in configuration: None) Don’t align array initializer columns. + + + + +# Our custom rules are the same as "BreakBeforeBraces: Linux", except +# additional customization for C++ constructs. +# +# Classes are formatted the same way as structs. +# Multiline C++11 lambda expressions are formatted like blocks of code. +# +BreakBeforeBraces: Custom +BraceWrapping: + AfterClass: false + AfterControlStatement: Never + AfterEnum: false + AfterFunction: true + AfterNamespace: false + AfterStruct: false + AfterUnion: false + AfterExternBlock: false + BeforeCatch: false + BeforeElse: false + SplitEmptyFunction: false + SplitEmptyRecord: false + SplitEmptyNamespace: true + +# Never break string literals; programmer will usually do more meaningful +# break (if it's really required). +# +BreakStringLiterals: false + +# When set to false, a function declaration's or function definition's +# parameters (but not function calls) will either all be on the same line +# or will have one line each. +# +# void f(int xxxxxxxxxxxxxxxxxxx, +# int yyyyyyyyyyyyyyyyyyyy, +# int zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz) +# { +# // … +# } +# +# void g(int x, int y, int z) +# { +# // … +# } +# +# Place paramaters on their own lines, like: +# constexpr std::array debuggers_events = { +# SDL_WINDOWEVENT, +# SDL_KEYDOWN, +# SDL_KEYUP, +# SDL_MOUSEMOTION, +# }; +# BinPackParameters: false +BinPackArguments: false +ExperimentalAutoDetectBinPacking: false +AllowAllParametersOfDeclarationOnNextLine: false + +# Emulates dosbox-staging constructor formatting rules; initializer list +# is treated as continuation, therefore initializers are indented with spaces. +# +# Constructor() +# : initializer1(), +# initializer2() +# {} +# +AllowAllConstructorInitializersOnNextLine: false +BreakConstructorInitializers: BeforeColon +ConstructorInitializerAllOnOneLineOrOnePerLine: true +ConstructorInitializerIndentWidth: 8 + +# Use the same indentation level as for the switch statement. +# Switch statement body is always indented one level more than case labels; +# this is followin K&R C-style (case labels are really just goto labels). +# +# switch (foo) { +# case 1: +# bar(); +# break; +# default: +# baz(); +# } +# +IndentCaseLabels: false + +# Don't insert a space after a cast +# +# x = (int32)y; NOT x = (int32) y; +# +SpaceAfterCStyleCast: false + +# Insert spaces before and after assignment operators +# +# int a = 5; NOT int a=5; +# a += 42; a+=42; +# +SpaceBeforeAssignmentOperators: true + +# Put a space before opening parentheses only after control statement keywords. +# +# void f() +# { +# if (true) { +# f(); +# } +# } +# +SpaceBeforeParens: ControlStatements + +# Don't insert spaces inside empty '()' +# +SpaceInEmptyParentheses: false + +# The number of spaces before trailing line comments (// - comments). +# This does not affect trailing block comments (/* - comments). +# +SpacesBeforeTrailingComments: 1 + +# Don't insert spaces in casts +# +# x = (int32) y; NOT x = ( int32 ) y; +# +SpacesInCStyleCastParentheses: false + +# Don't insert spaces after '(' or before ')' +# +# f(arg); NOT f( arg ); +# +SpacesInParentheses: false + +# Don't insert spaces after '[' or before ']' +# +# int a[5]; NOT int a[ 5 ]; +# +SpacesInSquareBrackets: false + +# A list of macros that should be interpreted as foreach loops instead of as +# function calls. +# +# TODO Currently unused, but left as an example in case we'll need such macros. +# +# ForEachMacros: +# - 'for_each_abbrev' +# - 'list_for_each_dir' + +# The maximum number of consecutive empty lines to keep. +# +MaxEmptyLinesToKeep: 1 + +# No empty line at the start of a block. +# +KeepEmptyLinesAtTheStartOfBlocks: false + +# Line breaking penalties +# +# This decides what order things should be done if a line is too long +# +# clang-format iterates through various versions the long line can be formatted +# and selects the one with smallest penalty score. +# +# Small ExcessCharacter penalty prevents breaking line is longer than +# column limit by only few characters. +# +PenaltyBreakAssignment: 100 +PenaltyBreakBeforeFirstCallParameter: 100 +PenaltyBreakComment: 10 +PenaltyBreakFirstLessLess: 0 +PenaltyBreakString: 110 +PenaltyExcessCharacter: 3 +PenaltyReturnTypeOnItsOwnLine: 200 + +# Don't sort "#include" declarations. +# +# clang-format has very rich customization options for grouping and sorting +# "include" directives, but there are still edge cases, so we still need to +# rely on developers doing this manually. +# +# See https://github.com/dosbox-staging/dosbox-staging/issues/196 for details. +# +# SortIncludes: false diff --git a/CMakeLists.txt b/CMakeLists.txt index c263452..6e0770c 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -10,6 +10,7 @@ add_executable(${PROJECT_NAME} Includes/SerIo.cpp Includes/C1231LR.cpp Includes/base64.cpp + Includes/WebIo.cpp ) if (WIN32) @@ -51,11 +52,20 @@ else() target_compile_options(${PROJECT_NAME} PRIVATE -march=native -Wall -Wextra -Wpedantic) endif() +if (WIN32) +target_link_libraries(${PROJECT_NAME} + ${SERIAL_LIBRARIES} + ${CMAKE_THREAD_LIBS_INIT} + spdlog::spdlog + libserialport +) +else() target_link_libraries(${PROJECT_NAME} ${SERIAL_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} spdlog::spdlog ) +endif() add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy_directory diff --git a/Includes/WebIo.cpp b/Includes/WebIo.cpp new file mode 100644 index 0000000..8d1e78f --- /dev/null +++ b/Includes/WebIo.cpp @@ -0,0 +1,183 @@ +/* + YACardEmu + ---------------- + Copyright (C) 2020-2023 wutno (https://github.com/GXTX) + + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along + with this program; if not, write to the Free Software Foundation, Inc., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +*/ + +#include "WebIo.h" + +WebIo::WebIo(CardIo::Settings *card, int port) +{ + m_card = card; + m_port = port; + setupRoutes(); + std::thread(&WebIo::startServer, this).detach(); +} + +WebIo::~WebIo() +{ + svr.stop(); +} + +void WebIo::startServer() +{ + spdlog::info("Starting API server..."); + svr.set_mount_point("/", "public"); + + svr.Get(R"(/api/v1/(\w+))", + [&](const httplib::Request& req, httplib::Response& res) { + Router(req, res); + }); + + svr.Post(R"(/api/v1/(\w+))", + [&](const httplib::Request& req, httplib::Response& res) { + Router(req, res); + }); + + svr.Delete(R"(/api/v1/(\w+))", + [&](const httplib::Request& req, httplib::Response& res) { + Router(req, res); + }); + + if (!svr.listen("0.0.0.0", m_port)) { + spdlog::critical("Failed to start API server!"); + } +} + +void WebIo::Router(const httplib::Request &req, httplib::Response &res) +{ + const auto route = req.matches[1].str(); + + spdlog::debug("WebIo::Router: {0} -> {1}", req.method, route); + + switch (routeValues[route]) + { + case Routes::cards: + res.set_content(generateCardListJSON(m_card->cardPath), + "application/json"); + break; + case Routes::hasCard: + res.set_content(m_card->hasCard ? "true" : "false", + "application/json"); + break; + case Routes::readyCard: + res.set_content(m_card->waitingForCard ? "true" : "false", + "application/json"); + break; + case Routes::insertedCard: insertedCard(req, res); break; + case Routes::dispenser: + if (req.method == "DELETE") { + m_card->reportDispenserEmpty = true; + } else if (req.method == "POST") { + m_card->reportDispenserEmpty = false; + } + + res.set_content(m_card->reportDispenserEmpty ? "empty" : "full", + "text/plain"); + break; + case Routes::stop: svr.stop(); break; + default: + spdlog::warn("WebIo::Router: Unsupported route \"{}\"", req.path); + res.status = 404; + break; + } +} + +void WebIo::insertedCard(const httplib::Request& req, httplib::Response& res) +{ + if (req.method == "DELETE") { + res.set_content("null", "application/json"); + return; + } + + if (req.method == "POST") { + if (req.has_param("loadonly")) { + m_card->insertedCard = true; + } + + if (req.has_param("cardname")) { + m_card->cardName = req.get_param_value("cardname"); + } + } + + if (req.has_param("redirect")) { + res.set_redirect("/"); + return; + } + + res.set_content(" { \"cardname\":\"" + m_card->cardName + "\", \"inserted\":" + + (m_card->insertedCard ? "true" : "false") + " }", + "application/json"); +} + +const std::string WebIo::generateCardListJSON(std::string basepath) +{ + std::string list{"["}; + + for (const auto& entry : ghc::filesystem::directory_iterator(basepath)) { + std::string card{entry.path().string()}; + + auto find = card.find(".track_0"); + + if (find != std::string::npos) { + card.replace(find, 8, ""); + list.append("{\"name\":\""); +#ifdef _WIN32 + list.append(card.substr(card.find_last_of("\\") + 1)); +#else + list.append(card.substr(card.find_last_of("/") + 1)); +#endif + // TODO: Support card images + list.append("\",\"image\":\""); + +#if 0 + auto find2 = card.find(".bin"); + if (find2 != std::string::npos) { + card.replace(find2, 4, ".png"); + } + + std::string base64{}; + + if (ghc::filesystem::exists(card)) { + std::ifstream img(card.c_str(), + std::ifstream::in | + std::ifstream::binary); + + base64.resize(ghc::filesystem::file_size(card)); + + img.read(reinterpret_cast(&base64[0]), + base64.size()); + img.close(); + } + + std::string encoded = base64_encode(base64, false); + list.append("data:image/png;base64, "); + list.append(encoded); +#endif + list.append("\"},"); + } + } + + // remove the errant comma + if (list.compare("[") != 0) { + list.pop_back(); + } + + list.append("]"); + return list; +} diff --git a/Includes/WebIo.h b/Includes/WebIo.h new file mode 100644 index 0000000..69cbb35 --- /dev/null +++ b/Includes/WebIo.h @@ -0,0 +1,79 @@ +/* + YACardEmu + ---------------- + Copyright (C) 2020-2023 wutno (https://github.com/GXTX) + + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along + with this program; if not, write to the Free Software Foundation, Inc., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +*/ + +#ifndef WEBIO_H +#define WEBIO_H + +#include +#include +#include +#include +#include + +#include "CardIo.h" +#include "base64.h" +#include "httplib.h" +#include "spdlog/spdlog.h" +#include "spdlog/fmt/bin_to_hex.h" + +class WebIo +{ +public: + WebIo(CardIo::Settings *card, int port); + ~WebIo(); + + void startServer(); + + int m_port = 0; + CardIo::Settings *m_card = nullptr; +private: + httplib::Server svr = {}; + + void Router(const httplib::Request &req, httplib::Response &res); + const std::string generateCardListJSON(std::string basepath); + + enum class Routes + { + undefined, // All invalid routes will point here + cards, + hasCard, + readyCard, + insertedCard, + dispenser, + stop, + }; + + std::map routeValues; + + void setupRoutes() + { + routeValues["cards"] = Routes::cards; + routeValues["hasCard"] = Routes::hasCard; + routeValues["readyCard"] = Routes::readyCard; + routeValues["insertedCard"] = Routes::insertedCard; + routeValues["dispenser"] = Routes::dispenser; + routeValues["stop"] = Routes::stop; + } + + void insertedCard(const httplib::Request& req, httplib::Response& res); +}; + +#endif //WEBIO_H diff --git a/main.cpp b/main.cpp index 9e09651..90d27fd 100755 --- a/main.cpp +++ b/main.cpp @@ -1,7 +1,7 @@ /* YACardEmu ---------------- - Copyright (C) 2020-2022 wutno (https://github.com/GXTX) + Copyright (C) 2020-2023 wutno (https://github.com/GXTX) This program is free software; you can redistribute it and/or modify @@ -28,7 +28,7 @@ #include "C1231LR.h" #include "SerIo.h" -#include "base64.h" +#include "WebIo.h" #include "httplib.h" #include "mini/ini.h" @@ -36,7 +36,7 @@ #include "ghc/filesystem.hpp" // Globals -static const auto delay{std::chrono::microseconds(250)}; +static constexpr auto delay{std::chrono::microseconds(250)}; std::atomic running{true}; // @@ -47,155 +47,6 @@ void sigHandler(int sig) } } -std::string generateCardListJSON(std::string basepath) -{ - std::string list{"["}; - - for (const auto &entry: ghc::filesystem::directory_iterator(basepath)) { - std::string card{entry.path().string()}; - - auto find = card.find(".track_0"); - - if (find != std::string::npos) { - card.replace(find, 8, ""); - list.append("{\"name\":\""); -#ifdef _WIN32 - list.append(card.substr(card.find_last_of("\\") + 1)); -#else - list.append(card.substr(card.find_last_of("/") + 1)); -#endif - list.append("\",\"image\":\""); - - auto find2 = card.find(".bin"); - if (find2 != std::string::npos) - card.replace(find2, 4, ".png"); - - std::string base64{}; - - if (ghc::filesystem::exists(card)) { - std::ifstream img(card.c_str(), std::ifstream::in | std::ifstream::binary); - - base64.resize(ghc::filesystem::file_size(card)); - - img.read(reinterpret_cast(&base64[0]), base64.size()); - img.close(); - } - - std::string encoded = base64_encode(base64, false); - list.append("data:image/png;base64, "); - list.append(encoded); - list.append("\"},"); - } - } - - // remove the errant comma - if (list.compare("[") != 0) { - list.pop_back(); - } - - list.append("]"); - return list; -} - -void httpServer(int port, C1231LR::Settings *card) -{ - httplib::Server svr; - - svr.set_mount_point("/", "public"); - - svr.Get("/actions", [&card](const httplib::Request &req, httplib::Response &res) { - if (req.has_param("list")) { - res.set_content(generateCardListJSON(card->cardPath), "application/json"); - } else { - if (req.has_param("insert")) { - card->insertedCard = true; - } else if (req.has_param("remove")) { - card->insertedCard = false; - } - - if (req.has_param("cardname")) { - card->cardName = req.get_param_value("cardname"); - } - - if (req.has_param("dispenser")) { - if (req.get_param_value("dispenser").compare("true") == 0) { - card->reportDispenserEmpty = true; - } else { - card->reportDispenserEmpty = false; - } - } - - res.set_redirect("/"); - } - }); - - svr.Get("/stop", [&svr](const httplib::Request &, httplib::Response &) { - spdlog::info("Stopping application..."); - running = false; - svr.stop(); - }); - - // REST API - svr.Get("/api/v1/cards", [&card](const httplib::Request &, httplib::Response &res) { - res.set_content(generateCardListJSON(card->cardPath), "application/json"); - }); - - svr.Get("/api/v1/hasCard", [&card](const httplib::Request &, httplib::Response &res) { - res.set_content(card->hasCard ? "true" : "false", "application/json"); - }); - - svr.Get("/api/v1/readyCard", [&card](const httplib::Request &, httplib::Response &res) { - res.set_content(card->waitingForCard ? "true" : "false", "application/json"); - }); - - svr.Get("/api/v1/insertedCard", [&card](const httplib::Request &, httplib::Response &res) { - res.set_content(" { \"cardName\":\"" + card->cardName + "\", \"inserted\":" + (card->insertedCard ? "true" : "false") + " }", "application/json"); - }); - - svr.Post("/api/v1/insertedCard", [&card](const httplib::Request &req, httplib::Response &res) { - if (!req.has_param("loadonly")) { - card->insertedCard = true; - } - if (req.has_param("cardname")) { - card->cardName = req.get_param_value("cardname"); - } - res.set_content(" { \"cardName\":\"" + card->cardName + "\", \"inserted\":" + (card->insertedCard ? "true" : "false") + " }", "application/json"); - }); - - svr.Delete("/api/v1/insertedCard", [&card](const httplib::Request &, httplib::Response &res) { - card->insertedCard = false; - res.set_content("null", "application/json"); - }); - - svr.Get("/api/v1/dispenser", [&card](const httplib::Request &, httplib::Response &res) { - if (card->reportDispenserEmpty == true) { - res.set_content("empty", "text/plain"); - } else { - res.set_content("full", "text/plain"); - } - }); - - svr.Post("/api/v1/dispenser", [&card](const httplib::Request &, httplib::Response &res) { - card->reportDispenserEmpty = false; - if (card->reportDispenserEmpty == true) { - res.set_content("empty", "text/plain"); - } else { - res.set_content("full", "text/plain"); - } - }); - - svr.Delete("/api/v1/dispenser", [&card](const httplib::Request &, httplib::Response &res) { - card->reportDispenserEmpty = true; - if (card->reportDispenserEmpty == true) { - res.set_content("empty", "text/plain"); - } else { - res.set_content("full", "text/plain"); - } - }); - - svr.listen("0.0.0.0", port); -} - bool readConfig(SerIo::Settings &serial, C1231LR::Settings &card, int *port) { // Read in config values @@ -280,8 +131,8 @@ int main() return 1; } - spdlog::info("Starting API server..."); - std::thread(httpServer, httpPort, &cardHandler->cardSettings).detach(); + //TODO: Make sure the service is actually running + WebIo *web = new WebIo(&cardHandler->cardSettings, httpPort); SerIo::Status serialStatus; CardIo::StatusCode cardStatus; diff --git a/public/blah.js b/public/blah.js index 87ac6e2..7c02220 100644 --- a/public/blah.js +++ b/public/blah.js @@ -41,7 +41,7 @@ function updateCards() { } async function fetchCards() { - await fetch('/actions?list').then(response => { + await fetch('/api/v1/cards').then(response => { if (!response.ok) { throw new Error(response.statusText) } diff --git a/public/index.html b/public/index.html index a314ff9..71efc0e 100644 --- a/public/index.html +++ b/public/index.html @@ -32,11 +32,13 @@
-
- + + +
-
+ +