Skip to content
This repository was archived by the owner on Jan 23, 2026. It is now read-only.

Commit 15bf22c

Browse files
committed
network interaction has been moved to a separate class
1 parent 17a62f5 commit 15bf22c

3 files changed

Lines changed: 30 additions & 25 deletions

File tree

include/Network/CrowNetworkServer.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#pragma once
22

3+
#include "../Barcode/BarcodeFactory.h"
4+
#include "../Barcode/IBarcodeGenerator.h"
35
#include "INetworkServer.h"
46
#include "crow_all.h"
57

src/Network/CrowNetworkServer.cpp

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,33 @@
11
#include "../../include/Network/CrowNetworkServer.h"
22

3-
void CrowNetworkServer::configure(int port) { port_ = port; }
3+
void CrowNetworkServer::configure(int port) {
4+
port_ = port;
5+
6+
CROW_ROUTE(app_, "/barcode")([](const crow::request &req) {
7+
auto type = req.url_params.get("type");
8+
auto text = req.url_params.get("text");
9+
auto margin = req.url_params.get("margin");
10+
auto size = req.url_params.get("size");
11+
std::unique_ptr<IBarcodeGenerator> generator = BarcodeFactory::create(type);
12+
13+
std::string matrix;
14+
crow::response resp;
15+
try {
16+
matrix = generator->generate(text, std::stoi(margin), std::stoi(size));
17+
18+
resp.code = 200;
19+
resp.add_header("Content-Type", "image/svg+xml");
20+
resp.body = std::move(matrix);
21+
} catch (const std::invalid_argument &ex) {
22+
std::cerr << ex.what() << std::endl;
23+
resp.code = 400;
24+
resp.add_header("Content-Type", "text/html");
25+
resp.body = std::move(matrix);
26+
}
27+
28+
return resp;
29+
});
30+
}
431

532
void CrowNetworkServer::start() { app_.port(port_).multithreaded().run(); }
633

src/main.cpp

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -15,29 +15,5 @@ int main() {
1515
CrowNetworkServer server;
1616
server.configure(50329);
1717

18-
CROW_ROUTE(server.getApp(), "/barcode")([](const crow::request &req) {
19-
auto type = req.url_params.get("type");
20-
auto text = req.url_params.get("text");
21-
auto margin = req.url_params.get("margin");
22-
auto size = req.url_params.get("size");
23-
std::unique_ptr<IBarcodeGenerator> generator = BarcodeFactory::create(type);
24-
25-
std::string matrix;
26-
crow::response resp;
27-
try {
28-
matrix = generator->generate(text, std::stoi(margin), std::stoi(size));
29-
30-
resp.code = 200;
31-
resp.add_header("Content-Type", "image/svg+xml");
32-
resp.body = std::move(matrix);
33-
} catch (const std::invalid_argument &ex) {
34-
std::cerr << ex.what() << std::endl;
35-
resp.code = 400;
36-
resp.add_header("Content-Type", "text/html");
37-
resp.body = std::move(matrix);
38-
}
39-
40-
return resp;
41-
});
4218
server.start();
4319
}

0 commit comments

Comments
 (0)