-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
95 lines (80 loc) · 2.8 KB
/
main.c
File metadata and controls
95 lines (80 loc) · 2.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <standardloop/logger.h>
#include "./cerver.h"
#include "./http/handler.h"
#include <standardloop/util.h>
void foo(const HttpRequest *, HttpResponse *);
void bar(const HttpRequest *, HttpResponse *);
void fooID(const HttpRequest *, HttpResponse *);
void fooStatic(const HttpRequest *, HttpResponse *);
void foo(const HttpRequest *request, HttpResponse *response)
{
// Log(TRACE, "[JOSH]: entering special test function\n");
// char *accepted_types = MapGet(request->headers, "Accept");
// if (accepted_types == NULL)
// {
// }
if (request == NULL || response == NULL)
{
return;
}
response->response_code = HttpOK;
response->body = QuickAllocatedString("Hello!");
SendResponse(response);
}
void fooID(const HttpRequest *request, HttpResponse *response)
{
// char *path_param_id = GetPathParam(request->path_params, "id");
JSONValue *path_param_id_obj = HashMapGet(request->path_params, "id");
if (path_param_id_obj == NULL)
{
const char *hello = "HTTP/1.1 200 OK\nContent-Type: text/plain\nContent-Length: 6\n\nHello!";
(void)write(request->client_socket, hello, strlen(hello));
return;
}
char *path_param_id = path_param_id_obj->value;
if (path_param_id != NULL)
{
printf("\npath_param_id: %s\n", path_param_id);
}
Log(TRACE, "[JOSH]: entering special test ID function\n");
if (request == NULL || response == NULL)
{
return;
}
const char *hello = "HTTP/1.1 200 OK\nContent-Type: text/plain\nContent-Length: 6\n\nHello!";
(void)write(request->client_socket, hello, strlen(hello));
}
void fooStatic(const HttpRequest *request, HttpResponse *response)
{
// Log(TRACE, "[JOSH]: entering special test function\n");
JSONValue *accepted_types_obj = HashMapGet(request->path_params, "Accept");
char *accepted_types = accepted_types_obj->value;
if (accepted_types == NULL)
{
}
if (request == NULL || response == NULL)
{
return;
}
HandleStaticPath(request->client_socket, "/static/foo.html");
return;
}
int main(void)
{
int port = atoi(GetEnv("PORT", "8080"));
int num_threads = atoi(GetEnv("NUM_THREADS", "4"));
int buffer_size = atoi(GetEnv("BUFFER_SIZE", "100"));
SetLogLevel(StringToLogLevel(GetEnv("LOG_LEVEL", "TRACE")));
// SetLogLevel(StringToLogLevel(GetEnv("LOG_LEVEL", "ERROR")));
Cerver *server = InitCerver(port, num_threads, buffer_size);
(void)AddRouteToTable(server->router->get, "/foo", foo);
// (void)AddRouteToTable(server->router->get, "/foo/{id=int}/bar/{name=string}", fooID);
// WIP support *
//(void)AddRouteToTable(server->router->get, "/bar/.*", bar);
StartCerver(server);
return EXIT_SUCCESS;
}