main.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
  2. /*
  3. * Duda I/O Benchmark Tests
  4. * ========================
  5. * This web service is made for the performance contest made by
  6. * TechEmpower, mode details here:
  7. *
  8. * http://www.techempower.com/benchmarks
  9. *
  10. * At the moment only Tests 1 & 6 are implemented.
  11. */
  12. #include "webservice.h"
  13. #include "packages/json/json.h"
  14. /* Test Macros (Tn) */
  15. #define JSON_CONTENT_TYPE "Content-Type: application/json"
  16. #define PLAIN_CONTENT_TYPE "Content-Type: text/plain"
  17. #define T6_BODY "Hello, World!"
  18. DUDA_REGISTER("Duda I/O Benchmark Test", "WS Bench");
  19. /*
  20. * Test type 1: JSON serialization
  21. * ===============================
  22. * This test use the JSON API object to compose the JSON response
  23. */
  24. void cb_json(duda_request_t *dr)
  25. {
  26. char *body;
  27. int body_len;
  28. json_t *j_root;
  29. /* Instance the JSON object and compose the content */
  30. j_root = json->create_object();
  31. json->add_to_object(j_root,
  32. "message",
  33. json->create_string("Hello, World!"));
  34. /* Format output to string */
  35. body = json->print_unformatted_gc(dr, j_root);
  36. body_len = strlen(body);
  37. /* Delete the JSON tree */
  38. json->delete(j_root);
  39. /* Compose the response */
  40. response->http_status(dr, 200);
  41. response->http_header_n(dr, JSON_CONTENT_TYPE, sizeof(JSON_CONTENT_TYPE) - 1);
  42. response->print(dr, body, body_len);
  43. response->end(dr, NULL);
  44. }
  45. /*
  46. * Test type 6: Plaintext
  47. * ======================
  48. */
  49. void cb_plaintext(duda_request_t *dr)
  50. {
  51. response->http_status(dr, 200);
  52. response->http_header_n(dr, PLAIN_CONTENT_TYPE, sizeof(PLAIN_CONTENT_TYPE) - 1);
  53. response->print(dr, T6_BODY, sizeof(T6_BODY) - 1);
  54. response->end(dr, NULL);
  55. }
  56. int duda_main()
  57. {
  58. /* load packages */
  59. duda_load_package(json, "json");
  60. /* let this web service own the virtual host */
  61. conf->service_root();
  62. /* set callbacks */
  63. map->static_add("/json", "cb_json"); /* Test #1 */
  64. map->static_add("/plaintext", "cb_plaintext"); /* Test #6 */
  65. return 0;
  66. }