diff --git a/bin/handler.o b/bin/handler.o new file mode 100644 index 0000000..e776d6e Binary files /dev/null and b/bin/handler.o differ diff --git a/bin/main b/bin/main index 82fed1b..d79340a 100755 Binary files a/bin/main and b/bin/main differ diff --git a/bin/main.o b/bin/main.o index cf7be2b..ce29ffe 100644 Binary files a/bin/main.o and b/bin/main.o differ diff --git a/src/handler.h b/src/handler.h new file mode 100644 index 0000000..582eaa4 --- /dev/null +++ b/src/handler.h @@ -0,0 +1,25 @@ +#include +#include +#include +#include "logger.h" + +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; + + log_api(url_str, method_str); + + response = MHD_create_response_from_buffer(strlen(url_str), (void *)url_str, MHD_RESPMEM_PERSISTENT); + + if (!response) + return MHD_NO; + + ret = MHD_queue_response(connection, MHD_HTTP_OK, response); + MHD_destroy_response(response); + + return ret; +} \ No newline at end of file diff --git a/src/logger.h b/src/logger.h new file mode 100644 index 0000000..7e78a1e --- /dev/null +++ b/src/logger.h @@ -0,0 +1,20 @@ +#include +#include + +void log_api(const char *url, const char *method, const char *status_code) { + char *color; + + if (strcmp(method, "GET") == 0) { + color = "\033[0;32m"; + } else if (strcmp(method, "POST") == 0) { + color = "\033[0;33m"; + } else if (strcmp(method, "PUT") == 0) { + color = "\033[0;34m"; + } else if (strcmp(method, "DELETE") == 0) { + color = "\033[0;35m"; + } else { + color = "\033[0;36m"; + } + + printf("%s[%s] %s -> %s\n", color, method, url); +} \ No newline at end of file diff --git a/src/main.c b/src/main.c index ffd55e6..4d61484 100644 --- a/src/main.c +++ b/src/main.c @@ -1,7 +1,24 @@ #include +#include #include +#include "handler.h" -int main(int argc, char *argv[]) { - printf("Hello, Wolrd!\n"); - return 0; +#define PORT 8080 +#define color_green "\033[0;32m" + +int main() { + printf("%sStart server on port %d\n", color_green, PORT); + + struct MHD_Daemon *daemon; + + daemon = MHD_start_daemon(MHD_USE_THREAD_PER_CONNECTION, PORT, NULL, NULL, + &default_handler, NULL, MHD_OPTION_END); + if (!daemon) + return 1; + + getchar(); + + MHD_stop_daemon(daemon); + + return 0; }