tutorial-17-http_server.cc 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*
  2. Copyright (c) 2023 sogou, Inc.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. #include <signal.h>
  14. #include "workflow/WFFacilities.h"
  15. #include "srpc/http_server.h"
  16. #include "srpc/rpc_types.h"
  17. #include "srpc/rpc_metrics_filter.h"
  18. #include "srpc/rpc_trace_filter.h"
  19. static WFFacilities::WaitGroup wait_group(1);
  20. srpc::RPCMetricsPull exporter;
  21. srpc::RPCTraceDefault trace_log;
  22. //srpc::RPCTraceOpenTelemetry otel("http://127.0.0.1:4318");
  23. static void sig_handler(int signo)
  24. {
  25. wait_group.done();
  26. }
  27. void process(WFHttpTask *task)
  28. {
  29. fprintf(stderr, "http server get request_uri: %s\n",
  30. task->get_req()->get_request_uri());
  31. task->get_resp()->append_output_body("<html>Hello from server!</html>");
  32. }
  33. int main()
  34. {
  35. signal(SIGINT, sig_handler);
  36. signal(SIGTERM, sig_handler);
  37. srpc::HttpServer server(process);
  38. exporter.init(8080); /* export port for prometheus */
  39. exporter.create_histogram("echo_request_size", "Echo request size",
  40. {1, 10, 100});
  41. exporter.create_summary("echo_test_quantiles", "Test quantile",
  42. {{0.5, 0.05}, {0.9, 0.01}});
  43. server.add_filter(&trace_log);
  44. // server.add_filter(&otel);
  45. server.add_filter(&exporter);
  46. if (server.start(1412) == 0)
  47. {
  48. printf("HTTP protocol server is running on 1412\n"
  49. "Try ./http_client to send requests.\n\n");
  50. wait_group.wait();
  51. server.stop();
  52. }
  53. else
  54. perror("server start");
  55. exporter.deinit();
  56. return 0;
  57. }