sample.cc 942 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. //
  2. // sample.cc
  3. //
  4. // Copyright (c) 2012 Yuji Hirose. All rights reserved.
  5. // The Boost Software License 1.0
  6. //
  7. #include <httplib.h>
  8. #include <cstdio>
  9. #include <signal.h>
  10. using namespace httplib;
  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. using namespace httplib;
  21. const char* hi = "/hi";
  22. Server svr("localhost", 1234);
  23. svr.get("/", [=](Connection& c) {
  24. c.response.set_redirect(hi);
  25. });
  26. svr.get("/hi", [](Connection& c) {
  27. c.response.set_content("Hello World!");
  28. });
  29. svr.get("/dump", [](Connection& c) {
  30. c.response.set_content(dump_request(c));
  31. });
  32. signal(SIGINT, [&]() { svr.stop(); });
  33. svr.run();
  34. }
  35. // vim: et ts=4 sw=4 cin cino={1s ff=unix