example_entrypoint.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /* _
  2. * ___ __ _ __ _ _ _(_)
  3. * / __|/ _` |/ _` | | | | |
  4. * \__ \ (_| | (_| | |_| | |
  5. * |___/\__,_|\__, |\__,_|_|
  6. * |___/
  7. *
  8. * Cross-platform library which helps to develop web servers or frameworks.
  9. *
  10. * Copyright (C) 2016-2019 Silvio Clecio <[email protected]>
  11. *
  12. * Sagui library is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU Lesser General Public
  14. * License as published by the Free Software Foundation; either
  15. * version 2.1 of the License, or (at your option) any later version.
  16. *
  17. * Sagui library is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  20. * Lesser General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Lesser General Public
  23. * License along with Sagui library; if not, write to the Free Software
  24. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  25. */
  26. #include <stdlib.h>
  27. #include <stdio.h>
  28. #include <string.h>
  29. #include <sagui.h>
  30. /* NOTE: Error checking has been omitted to make it clear. */
  31. static void r1_route_cb(void *cls, struct sg_route *route) {
  32. fprintf(stdout, "%s: %s\n", sg_route_path(route), (const char *) cls);
  33. fflush(stdout);
  34. }
  35. static void r2_route_cb(void *cls, struct sg_route *route) {
  36. fprintf(stdout, "%s: %s\n", sg_route_path(route), (const char *) cls);
  37. fflush(stdout);
  38. }
  39. int main(void) {
  40. struct sg_router *r1, *r2;
  41. struct sg_route *rts1 = NULL, *rts2 = NULL;
  42. struct sg_entrypoints *entrypoints;
  43. struct sg_entrypoint *entrypoint;
  44. sg_routes_add(&rts1, "/foo", r1_route_cb, "r1-foo-data");
  45. sg_routes_add(&rts1, "/bar", r1_route_cb, "r1-bar-data");
  46. r1 = sg_router_new(rts1);
  47. sg_routes_add(&rts2, "/foo", r2_route_cb, "r2-foo-data");
  48. sg_routes_add(&rts2, "/bar", r2_route_cb, "r2-bar-data");
  49. r2 = sg_router_new(rts2);
  50. entrypoints = sg_entrypoints_new();
  51. sg_entrypoints_add(entrypoints, "/r1", r1);
  52. sg_entrypoints_add(entrypoints, "/r2", r2);
  53. sg_entrypoints_find(entrypoints, &entrypoint, "/r1/foo");
  54. sg_router_dispatch(sg_entrypoint_user_data(entrypoint), "/foo", NULL);
  55. sg_entrypoints_find(entrypoints, &entrypoint, "/r1/bar");
  56. sg_router_dispatch(sg_entrypoint_user_data(entrypoint), "/bar", NULL);
  57. sg_entrypoints_find(entrypoints, &entrypoint, "/r2/foo");
  58. sg_router_dispatch(sg_entrypoint_user_data(entrypoint), "/foo", NULL);
  59. sg_entrypoints_find(entrypoints, &entrypoint, "/r2/bar");
  60. sg_router_dispatch(sg_entrypoint_user_data(entrypoint), "/bar", NULL);
  61. sg_routes_cleanup(&rts1);
  62. sg_routes_cleanup(&rts2);
  63. sg_router_free(r1);
  64. sg_router_free(r2);
  65. sg_entrypoints_free(entrypoints);
  66. return EXIT_SUCCESS;
  67. }