Skip to content

Commit 7934b9e

Browse files
committed
Add JSON response and update server settings
1 parent 859c89e commit 7934b9e

3 files changed

Lines changed: 43 additions & 5 deletions

File tree

‎example/main.cpp‎

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,35 @@ int port;
1616

1717
// Handler to handle the `GET` request on `/hello/world`
1818
void helloWorldHandler(Request &req, Response &res) {
19-
res.status(StatusCode::OK).send("Hello World!");
19+
json::object resp;
20+
resp["framework"] = "Expresso";
21+
resp["repository"] = "https://github.com/coding-cpp/expresso";
22+
resp["language"] = "C++";
23+
resp["creator"] = "Adit Jain";
24+
25+
json::object submodules;
26+
submodules.push_back(json::object());
27+
submodules[0]["repository"] = "https://github.com/coding-cpp/logger";
28+
submodules[0]["work"] = "Logging library for C++";
29+
submodules[0]["location"] = "lib/logger";
30+
submodules[0]["name"] = "Logger";
31+
32+
json::object nexusSubmodule;
33+
nexusSubmodule["repository"] = "https://github.com/coding-cpp/nexus";
34+
nexusSubmodule["work"] = "Thread pool library for C++";
35+
submodules.push_back(nexusSubmodule);
36+
submodules[1]["location"] = "lib/nexus";
37+
submodules[1]["name"] = "Nexus";
38+
39+
submodules.push_back(json::object());
40+
resp["submodules"] = submodules;
41+
42+
resp["submodules"][2]["repository"] = "https://github.com/coding-cpp/json";
43+
resp["submodules"][2]["work"] = "JSON library for C++";
44+
resp["submodules"][2]["location"] = "lib/json";
45+
resp["submodules"][2]["name"] = "JSON";
46+
47+
res.json(resp).status(200);
2048
return;
2149
}
2250

@@ -26,7 +54,7 @@ int main(int argc, char **argv) {
2654
port = std::stoi(process.getEnv("PORT"));
2755

2856
// Running a server with max 10 connections and 4 threads
29-
Server app(10, 4);
57+
Server app(10000, 100);
3058

3159
// Router, for routing requests starting with /hello
3260
Router world;

‎include/expresso/core/response.h‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <string>
77

88
#include <logger/log.h>
9+
#include <json/object.h>
910

1011
#include <expresso/core/status_code.h>
1112
#include <expresso/sys.h>
@@ -38,7 +39,8 @@ class Response {
3839
std::string get(std::string headerName);
3940

4041
Response status(int code);
41-
void send(std::string response);
42+
Response send(std::string response);
43+
Response json(json::object response);
4244

4345
// Send absolute path to the file, not relative
4446
void sendFile(std::string path);

‎src/core/response.cpp‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,19 @@ expresso::core::Response expresso::core::Response::status(int code) {
3737
return *this;
3838
}
3939

40-
void expresso::core::Response::send(std::string response) {
40+
expresso::core::Response expresso::core::Response::send(std::string response) {
4141
this->message = std::regex_replace(response, std::regex("\n"), "\r\n");
4242
this->message += "\r\n";
43+
this->set("Content-Type", "text/plain");
4344

44-
return;
45+
return *this;
46+
}
47+
48+
expresso::core::Response expresso::core::Response::json(json::object response) {
49+
this->message = response.dumps(0);
50+
this->set("Content-Type", "application/json");
51+
52+
return *this;
4553
}
4654

4755
void expresso::core::Response::sendFile(std::string path) {

0 commit comments

Comments
 (0)