Skip to content

Commit

Permalink
Add basic http server
Browse files Browse the repository at this point in the history
  • Loading branch information
mikek-mnx committed Jun 14, 2024
1 parent ddad8d9 commit ff93bf9
Show file tree
Hide file tree
Showing 7 changed files with 14,363 additions and 4 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ set(MODULES
src/WavFileWriter.cpp
src/madlld-1.1p1/bstdfile.c
src/pdjson/pdjson.c
src/HttpServer.cpp
)

set(SRCS
Expand Down
15 changes: 15 additions & 0 deletions src/HttpServer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include "HttpServer.h"
#include <iostream>
#include "crow/crow_all.h"

void HttpServer::run() {
std::cout << "Starting Http server" << std::endl;

crow::SimpleApp app;

CROW_ROUTE(app, "/")([](){
return "Hello!";
});

app.port(18080).multithreaded().run();
}
6 changes: 6 additions & 0 deletions src/HttpServer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#pragma once

class HttpServer {
public:
void run();
};
7 changes: 7 additions & 0 deletions src/OptionHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include "FileFormat.h"
#include "FileUtil.h"
#include "GdImageRenderer.h"
#include "HttpServer.h"
#include "Mp3AudioFileReader.h"
#include "Log.h"
#include "Options.h"
Expand Down Expand Up @@ -688,6 +689,12 @@ bool OptionHandler::run(const Options& options)

setLogLevel(options.getQuiet());

if(options.serverMode()) {
HttpServer httpServer{};
httpServer.run();
return true;
}

bool success = true;

try {
Expand Down
18 changes: 14 additions & 4 deletions src/Options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ Options::Options() :
amplitude_scale_(1.0),
png_compression_level_(-1), // default
raw_sample_rate_(0),
raw_channels_(0)
raw_channels_(0),
server_mode_(false)
{
}

Expand Down Expand Up @@ -268,6 +269,9 @@ bool Options::parseCommandLine(int argc, const char* const* argv)
"format for raw audio input "
"(s8, u8, s16le, s16be, s24le, s24be, s32le, s32be, "
"f32le, f32be, f64le, f64be)"
)(
"server",
"run in server mode"
);

po::variables_map variables_map;
Expand All @@ -279,11 +283,13 @@ bool Options::parseCommandLine(int argc, const char* const* argv)

help_ = variables_map.count("help") != 0;
version_ = variables_map.count("version") != 0;
server_mode_ = hasOptionValue(variables_map, "server");

if (help_ || version_) {
if (help_ || version_ || server_mode_) {
return true;
}


quiet_ = variables_map.count("quiet") != 0;

split_channels_ = variables_map.count("split-channels") != 0;
Expand Down Expand Up @@ -324,11 +330,15 @@ bool Options::parseCommandLine(int argc, const char* const* argv)
bool has_raw_channels = hasOptionValue(variables_map, "raw-channels");
bool has_raw_format = hasOptionValue(variables_map, "raw-format");

if (input_filename_.empty() && !has_input_format_) {
reportError("Must specify either input filename or input format");
if (input_filename_.empty() && !has_input_format_ && !server_mode_) {
reportError("Must specify either input filename, input format or start in server mode");
return false;
}

if(server_mode_) {
return true;
}

input_format_ = has_input_format_ ?
FileFormat::fromString(input_format) :
getFormatFromFileExtension(input_filename_);
Expand Down
4 changes: 4 additions & 0 deletions src/Options.h
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ class Options

void reportError(const std::string& message) const;

bool serverMode() const {return server_mode_;}

private:
void handleAmplitudeScaleOption(const std::string& option_value);
void handleZoomOption(const std::string& option_value);
Expand Down Expand Up @@ -198,6 +200,8 @@ class Options
int raw_sample_rate_;
int raw_channels_;
std::string raw_format_;

bool server_mode_;
};

//------------------------------------------------------------------------------
Expand Down
Loading

0 comments on commit ff93bf9

Please sign in to comment.