Skip to content

Commit

Permalink
Update service page (#172)
Browse files Browse the repository at this point in the history
* Update servicepage.html

* Update errorpage.cpp
  • Loading branch information
maddsua authored Feb 14, 2024
1 parent 1fad21f commit e6b7b77
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 24 deletions.
8 changes: 8 additions & 0 deletions core/html/resources/servicepage.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@
color: black;
background-color: white;
}

@media (prefers-color-scheme: dark) {
body {
color: whitesmoke;
background-color: #171717;
}
}

.message {
display: flex;
flex-direction: row;
Expand Down
51 changes: 28 additions & 23 deletions core/server/errorpage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,34 @@ HTTP::Response Pages::renderErrorPage(HTTP::Status code, const std::string& mess

HTTP::Response Pages::renderErrorPage(HTTP::Status code, const std::string& message, ErrorResponseType type) {

std::string pagecontent;

if (type == ErrorResponseType::HTML) {

pagecontent = renderTemplate(Templates::servicePage, {
{ "svcpage_statuscode", std::to_string(code.code()) },
{ "svcpage_statustext", code.text() },
{ "svcpage_message_text", message }
});

} else {

JSON::Map responseObject = {
{ "ok", false },
{ "status", "failed" },
{ "context", code.text() },
{ "what", message }
};

pagecontent = JSON::stringify(responseObject);
HTTP::Response errorResponse { code };

switch (type) {

case ErrorResponseType::HTML: {
errorResponse.body = renderTemplate(Templates::servicePage, {
{ "svcpage_statuscode", std::to_string(code.code()) },
{ "svcpage_statustext", code.text() },
{ "svcpage_message_text", message }
});
errorResponse.headers.set("content-type", "text/html");
} break;

case ErrorResponseType::JSON: {
errorResponse.body = JSON::stringify(JSON::Map({
{ "ok", false },
{ "status", "failed" },
{ "context", code.text() },
{ "what", message }
}));
errorResponse.headers.set("content-type", "application/json");
} break;

default: {
errorResponse.body = "Backend error: " + message + "\r\nmaddsua/lambda\r\n";
errorResponse.headers.set("content-type", "text/plain");
} break;
}

return Lambda::HTTP::Response(code, {
{ "Content-Type", type == ErrorResponseType::HTML ? "text/html" : "application/json" }
}, pagecontent);
return errorResponse;
}
2 changes: 1 addition & 1 deletion core/server/options.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace Lambda {
};

enum struct ErrorResponseType {
HTML, JSON
Plain, HTML, JSON
};

struct ServeOptions {
Expand Down

0 comments on commit e6b7b77

Please sign in to comment.