sample.cc 899 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. //
  2. // sample.cc
  3. //
  4. // Copyright (c) 2012 Yuji Hirose. All rights reserved.
  5. // The Boost Software License 1.0
  6. //
  7. #include <httpsvrkit.h>
  8. #include <cstdio>
  9. #include <signal.h>
  10. using namespace httpsvrkit;
  11. template<typename Fn> void signal(int sig, Fn fn)
  12. {
  13. static std::function<void ()> signal_handler_;
  14. struct SignalHandler { static void fn(int sig) { signal_handler_(); } };
  15. signal_handler_ = fn;
  16. signal(sig, SignalHandler::fn);
  17. }
  18. int main(void)
  19. {
  20. const char* hi = "/hi";
  21. HTTP_SERVER("localhost", 1234) /* svr_ */ {
  22. GET("/", {
  23. res_.set_redirect(hi);
  24. });
  25. GET("/hi", {
  26. res_.set_content("Hello World!");
  27. });
  28. GET("/dump", {
  29. res_.set_content(dump_request(cxt));
  30. });
  31. signal(SIGINT, [&](){
  32. svr_->stop();
  33. });
  34. }
  35. }
  36. // vim: et ts=4 sw=4 cin cino={1s ff=unix