You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
WebCC/examples/file_upload_server.cc

67 lines
1.5 KiB
C++

#include <iostream>
#include <string>
#include "webcc/logger.h"
#include "webcc/response_builder.h"
#include "webcc/server.h"
// -----------------------------------------------------------------------------
class FileUploadView : public webcc::View {
public:
webcc::ResponsePtr Handle(webcc::RequestPtr request) override {
if (request->method() == "POST") {
return Post(request);
}
return {};
}
private:
webcc::ResponsePtr Post(webcc::RequestPtr request) {
std::cout << "form parts: " << request->form_parts().size() << std::endl;
for (auto& part : request->form_parts()) {
std::cout << "name: " << part->name() << std::endl;
std::cout << "data: " << std::endl << part->data() << std::endl;
}
return webcc::ResponseBuilder{}.Created().Body("OK")();
}
};
// -----------------------------------------------------------------------------
void Help(const char* argv0) {
std::cout << "Usage: " << argv0 << " <port>" << std::endl;
std::cout << " E.g.," << std::endl;
std::cout << " " << argv0 << " 8080" << std::endl;
}
int main(int argc, char* argv[]) {
if (argc < 2) {
Help(argv[0]);
return 1;
}
WEBCC_LOG_INIT("", webcc::LOG_CONSOLE);
std::uint16_t port = static_cast<std::uint16_t>(std::atoi(argv[1]));
std::size_t workers = 2;
try {
webcc::Server server(port, workers);
server.Route("/upload", std::make_shared<FileUploadView>(), { "POST" });
server.Run();
} catch (const std::exception& e) {
std::cerr << e.what() << std::endl;
return 1;
}
return 0;
}