2
0

server.h 1015 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. #pragma once
  4. #include <atomic>
  5. #include <string>
  6. #include "opentelemetry/ext/http/server/http_server.h"
  7. namespace
  8. {
  9. class HttpServer : public HTTP_SERVER_NS::HttpRequestCallback
  10. {
  11. protected:
  12. HTTP_SERVER_NS::HttpServer server_;
  13. std::string server_url_;
  14. uint16_t port_;
  15. std::atomic<bool> is_running_{false};
  16. public:
  17. HttpServer(const std::string &server_name = "test_server", uint16_t port = 8800) : port_(port)
  18. {
  19. server_.setServerName(server_name);
  20. server_.setKeepalive(false);
  21. }
  22. void AddHandler(const std::string &path, HTTP_SERVER_NS::HttpRequestCallback *request_handler)
  23. {
  24. server_.addHandler(path, *request_handler);
  25. }
  26. void Start()
  27. {
  28. if (!is_running_.exchange(true))
  29. {
  30. server_.addListeningPort(port_);
  31. server_.start();
  32. }
  33. }
  34. void Stop()
  35. {
  36. if (is_running_.exchange(false))
  37. {
  38. server_.stop();
  39. }
  40. }
  41. ~HttpServer() { Stop(); }
  42. };
  43. } // namespace