snipplets.dev/code/C/Web/ulfius-server/server.h

69 lines
1.9 KiB
C
Raw Normal View History

2023-08-01 03:25:16 +03:00
atomic_bool running = true;
#define PORT 8000
#define PREFIX "/api"
2024-05-16 20:40:00 +03:00
int callback_default(const struct _u_request *request, struct _u_response *response, void *user_data);
int callback_all_test_foo(const struct _u_request *request, struct _u_response *response, void *user_data);
2023-08-01 03:25:16 +03:00
void *thread_server(void *vargp) {
int ret;
struct _u_instance instance;
if (ulfius_init_instance(&instance, PORT, NULL, NULL) != U_OK) {
exit(1);
}
instance.max_post_body_size = 1024;
2024-05-16 20:40:00 +03:00
ulfius_add_endpoint_by_val(&instance, "GET", PREFIX, "/param/:name", 0, &callback_all_test_foo, "user data 1");
ulfius_add_endpoint_by_val(&instance, "POST", PREFIX, "/param/:name", 0, &callback_all_test_foo, "user data 2");
2023-08-01 03:25:16 +03:00
ulfius_set_default_endpoint(&instance, &callback_default, NULL);
ret = ulfius_start_framework(&instance);
if (ret == U_OK) {
printf("Server started at %d.\n", PORT);
} else {
printf("Error starting server as %d port.\n", PORT);
}
while (running) {
}
printf("Server halt.\n");
ulfius_stop_framework(&instance);
ulfius_clean_instance(&instance);
}
2024-05-16 20:40:00 +03:00
int callback_default(const struct _u_request *request, struct _u_response *response, void *user_data) {
2023-08-01 03:25:16 +03:00
(void)(request);
(void)(user_data);
2024-05-16 20:40:00 +03:00
ulfius_set_string_body_response(response, 404, "Page not found, do what you want");
2023-08-01 03:25:16 +03:00
return U_CALLBACK_CONTINUE;
}
2024-05-16 20:40:00 +03:00
int callback_all_test_foo(const struct _u_request *request, struct _u_response *response, void *user_data) {
2023-08-01 03:25:16 +03:00
// url_params = request->map_url
// post_params = request->map_post_body
const char **keys, *value;
keys = u_map_enum_keys(request->map_url);
value = u_map_get(request->map_url, keys[0]);
if (strcmp("quit", value) == 0) {
running = false;
}
char *response_body = msprintf("Your %s is %s\n", keys[0], value);
print_to_terminal(response_body);
ulfius_set_string_body_response(response, 200, response_body);
o_free(response_body);
return U_CALLBACK_CONTINUE;
}