sample.cc 908 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) {
  22. // svr, req, res
  23. GET("/", {
  24. res.set_redirect(hi);
  25. });
  26. GET("/hi", {
  27. res.set_content("Hello World!");
  28. });
  29. GET("/dump", {
  30. res.set_content(dump_request(cxt));
  31. });
  32. signal(SIGINT, [&](){
  33. svr->stop();
  34. });
  35. }
  36. }
  37. // vim: et ts=4 sw=4 cin cino={1s ff=unix