output.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * Copyright (c) 2018 Markus Hennecke <markus-hennecke@markus-hennecke.de>
  3. *
  4. * Permission to use, copy, modify, and distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #include <sys/types.h>
  17. #include <ctype.h>
  18. #include <err.h>
  19. #include <stdarg.h>
  20. #include <stdlib.h>
  21. #include <time.h>
  22. #include <unistd.h>
  23. #include <kcgi.h>
  24. #include "../db.h"
  25. #include "../util.h"
  26. enum key {
  27. KEY_FROM,
  28. KEY_UNTIL,
  29. KEY_DEVICE,
  30. KEY__MAX
  31. };
  32. static const struct kvalid keys[KEY__MAX] = {
  33. { validate_timestamp, "from" },
  34. { validate_timestamp, "until" },
  35. { kvalid_string, "device" },
  36. };
  37. enum pg {
  38. PG_DATA,
  39. PG_STATISTICS,
  40. PG_LATEST,
  41. PG_ERRORS,
  42. PG__MAX
  43. };
  44. static const char *pages[PG__MAX] = {
  45. "data",
  46. "statistics",
  47. "latest",
  48. "errors",
  49. };
  50. static bool (*query_funcs[PG__MAX])(struct kreq *r, char **, const char *,
  51. const char *) = {
  52. db_get_data_json,
  53. db_get_statistics_data_json,
  54. db_get_latest_json,
  55. NULL,
  56. };
  57. int
  58. main(void)
  59. {
  60. struct kreq r;
  61. enum kcgi_err er;
  62. tzset();
  63. if ((pledge("cpath getpw stdio proc wpath rpath dns inet",
  64. NULL)) == (-1))
  65. err(EXIT_FAILURE, "pledge");
  66. er = khttp_parse(&r, keys, KEY__MAX, pages, PG__MAX, PG_DATA);
  67. if (KCGI_OK != er)
  68. return EXIT_FAILURE;
  69. /*
  70. * Accept GET...
  71. */
  72. if (KMETHOD_OPTIONS == r.method) {
  73. khttp_head(&r, kresps[KRESP_ALLOW], "OPTIONS GET");
  74. response(&r, KHTTP_200, r.mime, NULL);
  75. } else if (KMETHOD_GET == r.method) {
  76. char *json = NULL;
  77. const char *from = parameter(&r, KEY_FROM);
  78. const char *until = parameter(&r, KEY_UNTIL);
  79. const char *device = parameter(&r, KEY_DEVICE);
  80. switch (r.page) {
  81. case PG_DATA:
  82. case PG_STATISTICS:
  83. case PG_LATEST:
  84. if (r.fieldnmap[KEY_FROM] == NULL
  85. && r.fieldnmap[KEY_UNTIL] == NULL
  86. && query_funcs[r.page](&r, &json, from, until))
  87. response(&r, KHTTP_200, KMIME_APP_JSON, json);
  88. else
  89. response(&r, KHTTP_503, KMIME_TEXT_PLAIN, NULL);
  90. break;
  91. case PG_ERRORS:
  92. if (r.fieldnmap[KEY_DEVICE] == NULL
  93. && db_get_errors_json(&r, &json, device))
  94. response(&r, KHTTP_200, KMIME_APP_JSON, json);
  95. else
  96. response(&r, KHTTP_503, KMIME_TEXT_PLAIN, NULL);
  97. break;
  98. default:
  99. response(&r, KHTTP_503, KMIME_TEXT_PLAIN,
  100. "0 internal error");
  101. break;
  102. }
  103. } else {
  104. response(&r, KHTTP_404, r.mime, "0 page not found");
  105. }
  106. khttp_free(&r);
  107. return EXIT_SUCCESS;
  108. }