diff --git a/bin/main b/bin/main index 646dce5..3583c4d 100755 Binary files a/bin/main and b/bin/main differ diff --git a/src/handler.h b/src/handler.h index 582eaa4..cfb43ae 100644 --- a/src/handler.h +++ b/src/handler.h @@ -1,24 +1,40 @@ -#include -#include -#include -#include "logger.h" +#include "response_builder.h" + +void log_api(const char *url, const char *method) { + printf("[%s] %s\n", method, url); +} enum MHD_Result default_handler(void *cls, struct MHD_Connection *connection, const char *url, const char *method, const char *version, const char *upload_data, size_t *upload_data_size, void **con_cls) { - struct MHD_Response *response; char *url_str = (char *)url; char *method_str = (char *)method; int ret; + struct MHD_Response *response; + HTTP_status status; + char *response_body = NULL; + log_api(url_str, method_str); - response = MHD_create_response_from_buffer(strlen(url_str), (void *)url_str, MHD_RESPMEM_PERSISTENT); + if (strcmp(url_str, "/") == 0) { + response_body = simple_message("Hello, world!"); + status = OK; + } else if (strcmp(url_str, "/users") == 0) { + // mandar para modulo de usuarios + response_body = simple_message("Sorry, this endpoint is not implemented yet"); + status = OK; + } else { + response_body = simple_message("Not found"); + status = NOT_FOUND; + } + + response = HTTP_build_response_JSON(response_body, status); if (!response) return MHD_NO; - ret = MHD_queue_response(connection, MHD_HTTP_OK, response); + ret = MHD_queue_response(connection, status, response); MHD_destroy_response(response); return ret; diff --git a/src/logger.h b/src/logger.h deleted file mode 100644 index 1ae3ab8..0000000 --- a/src/logger.h +++ /dev/null @@ -1,6 +0,0 @@ -#include -#include - -void log_api(const char *url, const char *method) { - printf("[%s] %s\n", method, url); -} \ No newline at end of file diff --git a/src/main.c b/src/main.c index 7a4e9c6..aecb336 100644 --- a/src/main.c +++ b/src/main.c @@ -4,7 +4,6 @@ #include "handler.h" #define PORT 8080 -#define color_green "\033[0;32m" int main() { printf("Starting server on port %d\n", PORT); diff --git a/src/response_builder.h b/src/response_builder.h new file mode 100644 index 0000000..225078d --- /dev/null +++ b/src/response_builder.h @@ -0,0 +1,16 @@ +#include +#include "types.h" + +struct MHD_Response *HTTP_build_response_JSON(const char *message, HTTP_status status) { + struct MHD_Response *response; + + response = MHD_create_response_from_buffer(strlen(message), (void *)message, MHD_RESPMEM_PERSISTENT); + + if (!response) + return NULL; + + MHD_add_response_header(response, "Content-Type", "application/json"); + MHD_add_response_header(response, "Access-Control-Allow-Origin", "*"); + + return response; +} diff --git a/src/types.h b/src/types.h new file mode 100644 index 0000000..23a89ba --- /dev/null +++ b/src/types.h @@ -0,0 +1,24 @@ +#include +#include +#include + +typedef enum { + OK = 200, + BAD_REQUEST = 400, + NOT_FOUND = 404, + INTERNAL_SERVER_ERROR = 500, + NOT_IMPLEMENTED = 501 +} HTTP_status; + +char *simple_message(const char *message_str) { + char *formatted_message = NULL; + + size_t formatted_message_size = strlen(message_str) + 20; + formatted_message = (char *)malloc(formatted_message_size); + + if (formatted_message) { + snprintf(formatted_message, formatted_message_size, "{\"message\": \"%s\"}", message_str); + } + + return formatted_message; +} \ No newline at end of file