Added example/one_time_request.cc.
This commit is contained in:
parent
c1a09daf15
commit
0b657d28cf
1
.gitignore
vendored
1
.gitignore
vendored
@ -24,6 +24,7 @@ test/*.srl
|
|||||||
|
|
||||||
*.swp
|
*.swp
|
||||||
|
|
||||||
|
build/
|
||||||
Debug
|
Debug
|
||||||
Release
|
Release
|
||||||
*.vcxproj.user
|
*.vcxproj.user
|
||||||
|
@ -1,8 +1,7 @@
|
|||||||
#CXX = clang++
|
#CXX = clang++
|
||||||
CXXFLAGS = -O2 -std=c++11 -I.. -Wall -Wextra -pthread
|
CXXFLAGS = -O2 -std=c++11 -I.. -Wall -Wextra -pthread
|
||||||
|
|
||||||
PREFIX = /usr/local
|
PREFIX ?= $(shell brew --prefix)
|
||||||
#PREFIX = $(shell brew --prefix)
|
|
||||||
|
|
||||||
OPENSSL_DIR = $(PREFIX)/opt/openssl@3
|
OPENSSL_DIR = $(PREFIX)/opt/openssl@3
|
||||||
OPENSSL_SUPPORT = -DCPPHTTPLIB_OPENSSL_SUPPORT -I$(OPENSSL_DIR)/include -L$(OPENSSL_DIR)/lib -lssl -lcrypto
|
OPENSSL_SUPPORT = -DCPPHTTPLIB_OPENSSL_SUPPORT -I$(OPENSSL_DIR)/include -L$(OPENSSL_DIR)/lib -lssl -lcrypto
|
||||||
@ -51,6 +50,9 @@ ssecli : ssecli.cc ../httplib.h Makefile
|
|||||||
benchmark : benchmark.cc ../httplib.h Makefile
|
benchmark : benchmark.cc ../httplib.h Makefile
|
||||||
$(CXX) -o benchmark $(CXXFLAGS) benchmark.cc $(OPENSSL_SUPPORT) $(ZLIB_SUPPORT) $(BROTLI_SUPPORT)
|
$(CXX) -o benchmark $(CXXFLAGS) benchmark.cc $(OPENSSL_SUPPORT) $(ZLIB_SUPPORT) $(BROTLI_SUPPORT)
|
||||||
|
|
||||||
|
one_time_request : one_time_request.cc ../httplib.h Makefile
|
||||||
|
$(CXX) -o one_time_request $(CXXFLAGS) one_time_request.cc $(OPENSSL_SUPPORT) $(ZLIB_SUPPORT) $(BROTLI_SUPPORT)
|
||||||
|
|
||||||
pem:
|
pem:
|
||||||
openssl genrsa 2048 > key.pem
|
openssl genrsa 2048 > key.pem
|
||||||
openssl req -new -key key.pem | openssl x509 -days 3650 -req -signkey key.pem > cert.pem
|
openssl req -new -key key.pem | openssl x509 -days 3650 -req -signkey key.pem > cert.pem
|
||||||
|
56
example/one_time_request.cc
Normal file
56
example/one_time_request.cc
Normal file
@ -0,0 +1,56 @@
|
|||||||
|
#include <httplib.h>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
using namespace httplib;
|
||||||
|
|
||||||
|
const char *HOST = "localhost";
|
||||||
|
const int PORT = 1234;
|
||||||
|
|
||||||
|
void one_time_request_server(const char *label) {
|
||||||
|
std::thread th;
|
||||||
|
Server svr;
|
||||||
|
|
||||||
|
svr.Get("/hi", [&](const Request & /*req*/, Response &res) {
|
||||||
|
res.set_content(std::string("Hello from ") + label, "text/plain");
|
||||||
|
|
||||||
|
// Stop server
|
||||||
|
th = std::thread([&]() { svr.stop(); });
|
||||||
|
});
|
||||||
|
|
||||||
|
svr.listen(HOST, PORT);
|
||||||
|
th.join();
|
||||||
|
|
||||||
|
std::cout << label << " ended..." << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
void send_request(const char *label) {
|
||||||
|
Client cli(HOST, PORT);
|
||||||
|
|
||||||
|
std::cout << "Send " << label << " request" << std::endl;
|
||||||
|
auto res = cli.Get("/hi");
|
||||||
|
|
||||||
|
if (res) {
|
||||||
|
std::cout << res->body << std::endl;
|
||||||
|
} else {
|
||||||
|
std::cout << "Request error: " + to_string(res.error()) << std::endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(void) {
|
||||||
|
auto th1 = std::thread([&]() { one_time_request_server("Server #1"); });
|
||||||
|
auto th2 = std::thread([&]() { one_time_request_server("Server #2"); });
|
||||||
|
|
||||||
|
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
||||||
|
|
||||||
|
send_request("1st");
|
||||||
|
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
||||||
|
|
||||||
|
send_request("2nd");
|
||||||
|
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
||||||
|
|
||||||
|
send_request("3rd");
|
||||||
|
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
||||||
|
|
||||||
|
th1.join();
|
||||||
|
th2.join();
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user