| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- /*
- * sample http server
- * more detail see examples/httpd
- *
- */
- #include "HttpServer.h"
- #include "hssl.h"
- /*
- * #define TEST_HTTPS 1
- *
- * @build ./configure --with-openssl && make clean && make
- *
- * @server bin/http_server_test
- *
- * @client curl -v http://127.0.0.1:8080/ping
- * curl -v https://127.0.0.1:8443/ping --insecure
- * bin/curl -v http://127.0.0.1:8080/ping
- * bin/curl -v https://127.0.0.1:8443/ping
- *
- */
- #define TEST_HTTPS 0
- int main() {
- HV_MEMCHECK;
- HttpService router;
- router.GET("/ping", [](HttpRequest* req, HttpResponse* resp) {
- return resp->String("pong");
- });
- router.GET("/data", [](HttpRequest* req, HttpResponse* resp) {
- static char data[] = "0123456789";
- return resp->Data(data, 10);
- });
- router.GET("/paths", [&router](HttpRequest* req, HttpResponse* resp) {
- return resp->Json(router.Paths());
- });
- router.POST("/echo", [](HttpRequest* req, HttpResponse* resp) {
- resp->content_type = req->content_type;
- resp->body = req->body;
- return 200;
- });
- http_server_t server;
- server.service = &router;
- server.port = 8080;
- #if TEST_HTTPS
- server.https_port = 8443;
- hssl_ctx_init_param_t param;
- memset(¶m, 0, sizeof(param));
- param.crt_file = "cert/server.crt";
- param.key_file = "cert/server.key";
- if (hssl_ctx_init(¶m) == NULL) {
- fprintf(stderr, "SSL certificate verify failed!\n");
- return -20;
- }
- #endif
- // uncomment to test multi-processes
- // server.worker_processes = 4;
- // uncomment to test multi-threads
- // server.worker_threads = 4;
- #if 1
- http_server_run(&server);
- #else
- // test http_server_stop
- http_server_run(&server, 0);
- sleep(10);
- http_server_stop(&server);
- #endif
- return 0;
- }
|