This commit is contained in:
Alexander Popov 2023-08-01 03:25:16 +03:00
parent 4026c398e3
commit bab9d2d315
Signed by: iiiypuk
GPG Key ID: E47FE0AB36CD5ED6
6 changed files with 131 additions and 0 deletions

6
~/C/rest_server/.gitignore vendored Normal file
View File

@ -0,0 +1,6 @@
orcania/
ulfius/
*.a
rest

7
~/C/rest_server/Makefile Normal file
View File

@ -0,0 +1,7 @@
CC = clang
all:
$(CC) -std=c99 -Wall -O3 -o rest main.c -lulfius -lorcania
tcc:
tcc -O3 -I./ulfius/include -I./orcania/include -o rest main.c libulfius.2.7.13.a liborcania.2.3.2.a -lyder -lgnutls -lz -ljansson -lmicrohttpd -lcurl

16
~/C/rest_server/README.md Normal file
View File

@ -0,0 +1,16 @@
REST API
--------
* `http://localhost:8000/api/param/:name` - возвращает `:name`
* `http://localhost:8000/api/param/quit` - завершит выполнение программы
Зависимости
-----------
* [ulfius](https://github.com/babelouest/ulfius)
* [orcania](https://github.com/babelouest/orcania)
```sh
ar rcs libulfius.2.7.13.a ulfius.o u_map.o u_request.o u_response.o u_send_request.o u_websocket.o yuarel.o
ar rcs liborcania.2.3.2.a orcania.o memory.o base64.o
```

26
~/C/rest_server/main.c Normal file
View File

@ -0,0 +1,26 @@
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <pthread.h>
#include <ulfius.h>
#include <stdatomic.h>
#include "utils.h"
#include "server.h"
int main(int argc, char **argv) {
// создаём отдельный поток, в котором запускаем сервер
pthread_t tid;
pthread_create(&tid, NULL, thread_server, (void *)&tid);
// while (true) {
// }
pthread_exit(NULL);
return 0;
}

75
~/C/rest_server/server.h Normal file
View File

@ -0,0 +1,75 @@
atomic_bool running = true;
#define PORT 8000
#define PREFIX "/api"
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);
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;
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");
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);
}
int callback_default(const struct _u_request *request,
struct _u_response *response, void *user_data) {
(void)(request);
(void)(user_data);
ulfius_set_string_body_response(response, 404,
"Page not found, do what you want");
return U_CALLBACK_CONTINUE;
}
int callback_all_test_foo(const struct _u_request *request,
struct _u_response *response, void *user_data) {
// 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;
}

1
~/C/rest_server/utils.h Normal file
View File

@ -0,0 +1 @@
void print_to_terminal(char *text) { printf("%s", text); }