-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b5deb3f
commit 36175c3
Showing
10 changed files
with
342 additions
and
269 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
#include "headers/response_builder.h" | ||
#include "headers/user_handler.h" | ||
#include <setjmp.h> | ||
#include "headers/handler.h" | ||
|
||
jmp_buf exceptionBuffer; | ||
|
||
#define TRY if (setjmp(exceptionBuffer) == 0) | ||
#define CATCH else | ||
|
||
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) { | ||
|
||
char *url_str = (char *)url; | ||
char *method_str = (char *)method; | ||
int ret; | ||
|
||
struct MHD_Response *response; | ||
HTTP_response response_api; | ||
|
||
log_api(url_str, method_str); | ||
|
||
TRY { | ||
if (strcmp(url_str, "/") == 0) { | ||
response_api = (HTTP_response){ | ||
.body = simple_message("Hello world!"), | ||
.status = OK | ||
}; | ||
} | ||
|
||
else if (validate_route(url_str, "/users")) { | ||
response_api = user_router(url_str, method_str, upload_data); | ||
} | ||
|
||
else { | ||
response_api = (HTTP_response){ | ||
.body = simple_message("Not found"), | ||
.status = NOT_FOUND | ||
}; | ||
} | ||
} CATCH { | ||
response_api = (HTTP_response){ | ||
.body = simple_message("Internal server error"), | ||
.status = INTERNAL_SERVER_ERROR | ||
}; | ||
|
||
printf("Internal server error"); | ||
} | ||
|
||
response = HTTP_build_response_JSON(response_api.body); | ||
|
||
if (!response) | ||
return MHD_NO; | ||
|
||
ret = MHD_queue_response(connection, response_api.status, response); | ||
MHD_destroy_response(response); | ||
|
||
return ret; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,62 +1,18 @@ | ||
#include "headers/response_builder.h" | ||
#include "headers/user_handler.h" | ||
#ifndef HANDLER_H | ||
#define HANDLER_H | ||
|
||
#include <setjmp.h> | ||
|
||
jmp_buf exceptionBuffer; | ||
extern jmp_buf exceptionBuffer; | ||
|
||
#define TRY if (setjmp(exceptionBuffer) == 0) | ||
#define CATCH else | ||
|
||
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) { | ||
char *url_str = (char *)url; | ||
char *method_str = (char *)method; | ||
int ret; | ||
|
||
struct MHD_Response *response; | ||
HTTP_response response_api; | ||
|
||
log_api(url_str, method_str); | ||
|
||
TRY { | ||
if (strcmp(url_str, "/") == 0) { | ||
response_api = (HTTP_response){ | ||
.body = simple_message("Hello world!"), | ||
.status = OK | ||
}; | ||
} | ||
|
||
else if (validate_route(url_str, "/users")) { | ||
response_api = user_router(url_str, method_str, upload_data); | ||
} | ||
|
||
else { | ||
response_api = (HTTP_response){ | ||
.body = simple_message("Not found"), | ||
.status = NOT_FOUND | ||
}; | ||
} | ||
} CATCH { | ||
response_api = (HTTP_response){ | ||
.body = simple_message("Internal server error"), | ||
.status = INTERNAL_SERVER_ERROR | ||
}; | ||
|
||
printf("Internal server error"); | ||
} | ||
|
||
response = HTTP_build_response_JSON(response_api.body); | ||
|
||
if (!response) | ||
return MHD_NO; | ||
void log_api(const char *url, const char *method); | ||
|
||
ret = MHD_queue_response(connection, response_api.status, response); | ||
MHD_destroy_response(response); | ||
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); | ||
|
||
return ret; | ||
} | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,9 @@ | ||
#include <microhttpd.h> | ||
#include "headers/utils.h" | ||
|
||
struct MHD_Response *HTTP_build_response_JSON(const char *message) { | ||
struct MHD_Response *response; | ||
#ifndef RESPONSE_BUILDER_H | ||
#define RESPONSE_BUILDER_H | ||
|
||
response = MHD_create_response_from_buffer(strlen(message), (void *)message, MHD_RESPMEM_PERSISTENT); | ||
|
||
if (!response) | ||
return NULL; | ||
#include <microhttpd.h> | ||
#include "utils.h" | ||
|
||
MHD_add_response_header(response, "Content-Type", "application/json"); | ||
MHD_add_response_header(response, "Access-Control-Allow-Origin", "*"); | ||
struct MHD_Response *HTTP_build_response_JSON(const char *message); | ||
|
||
return response; | ||
} | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,107 +1,18 @@ | ||
#include "headers/pg.h" | ||
#include <string.h> | ||
#include <stdio.h> | ||
#ifndef USER_HANDLER_H | ||
#define USER_HANDLER_H | ||
|
||
HTTP_response get_all(const char *url) { | ||
const char *query = "SELECT * FROM users"; | ||
#include "utils.h" | ||
|
||
char *result = executeQueryToJson(query); | ||
HTTP_response get_all(const char *url); | ||
|
||
return validate_result(result); | ||
} | ||
HTTP_response get_one(const char *user_id); | ||
|
||
HTTP_response get_one(const char *user_id) { | ||
if (user_id == NULL) { | ||
return (HTTP_response){ | ||
.body = simple_message("No user id provided"), | ||
.status = BAD_REQUEST | ||
}; | ||
} | ||
HTTP_response create(const char *body); | ||
|
||
char query[64]; | ||
snprintf(query, sizeof(query), "SELECT * FROM users WHERE id = %s", user_id); | ||
HTTP_response update(const char *user_id, const char *body); | ||
|
||
char *result = executeQueryToJson(query); | ||
HTTP_response drop(const char *user_id); | ||
|
||
return validate_result(result); | ||
} | ||
HTTP_response user_router(const char *url, const char *method, const char *body); | ||
|
||
HTTP_response create(const char *body) { | ||
if (body == NULL) { | ||
return (HTTP_response){ | ||
.body = simple_message("No body provided"), | ||
.status = BAD_REQUEST | ||
}; | ||
} | ||
|
||
char query[256]; | ||
snprintf(query, sizeof(query), "INSERT INTO users (name, email) VALUES ('%s', '%s')", "name", "email"); | ||
|
||
char *result = executeQueryToJson(query); | ||
|
||
return validate_result(result); | ||
} | ||
|
||
HTTP_response update(const char *user_id, const char *body) { | ||
if (user_id == NULL) { | ||
return (HTTP_response){ | ||
.body = simple_message("No user id provided"), | ||
.status = BAD_REQUEST | ||
}; | ||
} | ||
|
||
if (body == NULL) { | ||
return (HTTP_response){ | ||
.body = simple_message("No body provided"), | ||
.status = BAD_REQUEST | ||
}; | ||
} | ||
|
||
char query[256]; | ||
snprintf(query, sizeof(query), "UPDATE users SET name = '%s', email = '%s' WHERE id = %s", "name", "email", user_id); | ||
|
||
char *result = executeQueryToJson(query); | ||
|
||
return validate_result(result); | ||
} | ||
|
||
HTTP_response drop(const char *user_id) { | ||
char query[64]; | ||
snprintf(query, sizeof(query), "DELETE FROM users WHERE id = %s", user_id); | ||
|
||
char *result = executeQueryToJson(query); | ||
|
||
return validate_result(result); | ||
} | ||
|
||
HTTP_response user_router(const char *url, const char *method, const char *body){ | ||
char *user_id = strstr(url, "/users/"); | ||
if (user_id != NULL) { | ||
user_id += strlen("/users/"); | ||
} | ||
|
||
if (validate_method(method, "GET")) { | ||
if(user_id == NULL){ | ||
return get_all(url); | ||
} else { | ||
return get_one(user_id); | ||
} | ||
} | ||
|
||
if (validate_method(method, "POST")) { | ||
return create(body); | ||
} | ||
|
||
if (validate_method(method, "PUT")) { | ||
return update(user_id, body); | ||
} | ||
|
||
if (validate_method(method, "DELETE")) { | ||
return drop(user_id); | ||
} | ||
|
||
return (HTTP_response){ | ||
.body = simple_message("Not implemented"), | ||
.status = NOT_IMPLEMENTED | ||
}; | ||
} | ||
#endif |
Oops, something went wrong.