Skip to content

Commit

Permalink
oak: Refactor server.hpp to move impls to source file
Browse files Browse the repository at this point in the history
  • Loading branch information
cassava committed Apr 19, 2023
1 parent a183df3 commit 3171837
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 14 deletions.
20 changes: 6 additions & 14 deletions oak/include/oak/server.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,20 +89,14 @@ class ServerImplHandler {
*/
class Server {
public:
Server(const std::string& addr, int port)
: listen_addr_(addr), listen_port_(port), listen_threads_(3), listening_(false) {}

Server() : listen_addr_("127.0.0.1"), listen_port_(8080), listen_threads_(3), listening_(false) {}
Server(const std::string& addr, int port);
Server();

/**
* When a Server goes out of scope, it will stop listening for you if you
* haven't done so already.
*/
~Server() {
if (this->is_listening()) {
this->stop();
}
}
~Server();

/**
* Set the number of threads used for listening to connections.
Expand Down Expand Up @@ -138,9 +132,7 @@ class Server {
/**
* Return endpoint data in json format.
*/
cloe::Json endpoints_to_json(const std::vector<std::string>& endpoints) const {
return handler_.endpoints_to_json(endpoints);
};
cloe::Json endpoints_to_json(const std::vector<std::string>& endpoints) const;

/**
* Stop the server.
Expand All @@ -150,7 +142,7 @@ class Server {
/**
* Return a list of all registered endpoints.
*/
std::vector<std::string> endpoints() const { return handler_.endpoints(); }
std::vector<std::string> endpoints() const;

protected:
friend StaticRegistrar;
Expand All @@ -160,7 +152,7 @@ class Server {
/**
* Add a handler with the route muxer in the internal handler routine.
*/
void add_handler(const std::string& key, Handler h) { handler_.add(key, h); }
void add_handler(const std::string& key, Handler h);

private:
// Configuration
Expand Down
24 changes: 24 additions & 0 deletions oak/src/oak/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -289,4 +289,28 @@ void Server::stop() {
listening_ = false;
}

void Server::add_handler(const std::string& key, Handler h) {
handler_->add(key, std::move(h));
}

std::vector<std::string> Server::endpoints() const {
return handler_->endpoints();
}

cloe::Json Server::endpoints_to_json(const std::vector<std::string>& endpoints) const {
return handler_->endpoints_to_json(endpoints);
}

Server::Server(const std::string& addr, int port)
: listen_addr_(addr), listen_port_(port), listen_threads_(3), listening_(false) {}

Server::Server()
: listen_addr_("127.0.0.1"), listen_port_(8080), listen_threads_(3), listening_(false) {}

Server::~Server() {
if (this->is_listening()) {
this->stop();
}
}

} // namespace oak

0 comments on commit 3171837

Please sign in to comment.