-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
82 lines (67 loc) · 2.47 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <cpprest/http_listener.h>
#include <cpprest/json.h>
#include <iostream>
#include "scanner/DocumentScanner.h"
#include "scanner/ScannedDocument.h"
#include "scanner/SaneScanner.h"
using namespace web;
using namespace http;
using namespace utility;
using namespace http::experimental::listener;
ScannedDocument doc;
auto saneScanner = std::make_unique<SaneScanner>();
DocumentScanner scanner = DocumentScanner(std::move(saneScanner));
int dpi = 150;
void handle_create_document(http_request request)
{
auto query_params = web::uri::split_query(request.request_uri().query());
dpi = stoi(query_params[U("dpi")]);
doc = ScannedDocument();
// Your code for creating a document here, using the dpi value
request.reply(status_codes::OK);
}
void handle_add_page(http_request request)
{
// Your code for adding a page here
const char *device_name = "pixma:04A91908";
ScannedPage page = scanner.scan(device_name, dpi);
doc.addPage(page);
request.reply(status_codes::OK);
}
void handle_get_pdf_document(http_request request)
{
// Your code for getting the PDF document here
std::vector<uchar> pdfData = doc.getPdfData();
concurrency::streams::container_buffer<std::vector<uchar>> buffer(std::move(pdfData));
auto stream = buffer.create_istream();
request.reply(status_codes::OK, stream, "application/pdf").wait();
}
int main(int argc, char *argv[])
{
utility::string_t port = U("8080"); // Default port
if (argc == 2) {
port = utility::conversions::to_string_t(argv[1]);
} else if (argc > 2) {
std::cerr << "Usage: " << argv[0] << " [port]" << std::endl;
return 1;
}
utility::string_t address = U("http://0.0.0.0:");
address.append(port);
http::experimental::listener::http_listener listener(address);
listener.support([](http_request request)
{
ucout << request.to_string() << std::endl;
auto path = request.relative_uri().path();
if (path == "/documents" && request.method() == methods::POST)
handle_create_document(request);
else if (path == "/documents/pages")
handle_add_page(request);
else if (path == "/documents" && request.method() == methods::GET)
handle_get_pdf_document(request);
else
request.reply(status_codes::NotFound);
});
listener.open().wait();
ucout << utility::string_t(U("Listening for requests at: ")) << listener.uri().to_string() << std::endl;
while (true);
}