Skip to content

Commit

Permalink
feat: init handler router
Browse files Browse the repository at this point in the history
  • Loading branch information
PedroFnseca committed Aug 29, 2023
1 parent ac10dd8 commit e70328e
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 14 deletions.
Binary file modified bin/main
Binary file not shown.
30 changes: 23 additions & 7 deletions src/handler.h
Original file line number Diff line number Diff line change
@@ -1,24 +1,40 @@
#include <stdio.h>
#include <string.h>
#include <microhttpd.h>
#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;
Expand Down
6 changes: 0 additions & 6 deletions src/logger.h

This file was deleted.

1 change: 0 additions & 1 deletion src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
16 changes: 16 additions & 0 deletions src/response_builder.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include <microhttpd.h>
#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;
}
24 changes: 24 additions & 0 deletions src/types.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

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;
}

0 comments on commit e70328e

Please sign in to comment.