Skip to content

Commit

Permalink
Drop --verbose option from server (#46)
Browse files Browse the repository at this point in the history
This option currently only affects outputs to stderr from the file service
endpoint, and is largely redundant with what the file server would return to the
user anyway.  It makes some sense to remove this option so that users don't
expect verbose output from the tuber endpoint by enabling it.
  • Loading branch information
arahlin authored Sep 13, 2024
1 parent 3b99915 commit 2f4be29
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 48 deletions.
41 changes: 4 additions & 37 deletions src/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,6 @@ namespace fs = std::filesystem;
* about symbol visibility here, we follow its lead to squash warnings. */
#define DLL_LOCAL __attribute__((visibility("hidden")))

/* Verbosity is expressed as a bit mask:
* 0: none (default)
* 1: report unexpected or unusual cases
* 2: very noisy
*/
static enum class Verbose {
NONE = 0, /* default */
UNEXPECTED = 1, /* report unexected or unusual cases */
NOISY = 2, /* message onslaught */
} verbose;

/* Operators for log levels */
inline constexpr int operator&(Verbose const& x, Verbose const& y) {
return static_cast<int>(x) & static_cast<int>(y);
}

/* MIME types */
static const std::string MIME_JSON="application/json";
static const std::string MIME_CBOR="application/cbor";
Expand Down Expand Up @@ -128,23 +112,15 @@ class DLL_LOCAL file_resource : public http_resource {
path /= "index.html";

/* Serve 404 if the resource does not exist, or we couldn't find it */
if(!fs::is_regular_file(path)) {
if(verbose & Verbose::UNEXPECTED)
std::cerr << "Unable or unwilling to serve missing or non-file resource " << path.string() << '\n';

if(!fs::is_regular_file(path))
return std::make_shared<string_response>("No such file or directory.\n", http::http_utils::http_not_found);
}

/* Figure out a MIME type to use */
std::string mime_type = MIME_DEFAULT;
auto it = MIME_TYPES.find(path.extension().string());
if(it != MIME_TYPES.end())
mime_type = it->second;

if(verbose & Verbose::NOISY)
std::cerr << "Serving " << req.get_path() << " with " << path.string()
<< " using MIME type " << mime_type << '\n';

/* Construct response and return it */
auto response = std::make_shared<file_response>(path.string(), http::http_utils::http_ok, mime_type);
response->with_header(http::http_utils::http_header_cache_control,
Expand All @@ -163,14 +139,8 @@ static void sigint(int signo) {
ws->stop();
}

void run_server(py::object handler, int port=80, const std::string &webroot="/var/www", int max_age=3600, int verbose_level=0)
static void run_server(py::object handler, int port=80, const std::string &webroot="/var/www", int max_age=3600)
{
/*
* Parse command-line arguments
*/

verbose = static_cast<Verbose>(verbose_level);

/* Can only run one server at a time */
if (ws)
throw std::runtime_error("Tuber server already running!");
Expand Down Expand Up @@ -229,9 +199,6 @@ PYBIND11_MODULE(_tuber_runtime, m) {
"webroot : str\n"
" Location to serve static content\n"
"max_age : int\n"
" Maximum cache residency for static (file) assets\n"
"verbose : int\n"
" Verbosity level (0-2)\n",
py::arg("handler"), py::arg("port")=80, py::arg("webroot")="/var/www/",
py::arg("max_age")=3600, py::arg("verbose")=0);
" Maximum cache residency for static (file) assets\n",
py::arg("handler"), py::arg("port")=80, py::arg("webroot")="/var/www/", py::arg("max_age")=3600);
}
13 changes: 2 additions & 11 deletions tuber/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -575,7 +575,7 @@ def __call__(self, *args, **kwargs):
return self.handle(*args, **kwargs)


def run(registry, json_module="json", port=80, webroot="/var/www/", max_age=3600, validate=False, verbose=0):
def run(registry, json_module="json", port=80, webroot="/var/www/", max_age=3600, validate=False):
"""
Run tuber server with the given registry.
Expand All @@ -593,8 +593,6 @@ def run(registry, json_module="json", port=80, webroot="/var/www/", max_age=3600
Maximum cache residency for static (file) assets
validate : bool
If True, validate incoming and outgoing data packets using jsonschema
verbose : int
Verbosity level (0-2)
"""
# setup environment
os.environ["TUBER_SERVER"] = "1"
Expand All @@ -609,13 +607,7 @@ def run(registry, json_module="json", port=80, webroot="/var/www/", max_age=3600
handler = RequestHandler(registry, json_module, validate=validate)

# run
run_server(
handler,
port=port,
webroot=webroot,
max_age=max_age,
verbose=verbose,
)
run_server(handler, port=port, webroot=webroot, max_age=max_age)


def load_registry(filename):
Expand Down Expand Up @@ -670,7 +662,6 @@ def main(registry=None):
P.add_argument(
"--validate", action="store_true", help="Validate incoming and outgoing data packets using jsonschema"
)
P.add_argument("-v", "--verbose", type=int, default=0)
args = P.parse_args()

# setup environment
Expand Down

0 comments on commit 2f4be29

Please sign in to comment.