ascii-chat
Binary v0.15.83-53-gb711d8720
Library lib/v0.3.2-701-gb711d8720
Video chat in your terminal
Loading...
Searching...
No Matches
main.c File Reference

Go to the source code of this file.

Data Structures

struct  api_server_t
struct  api_render_job

Typedefs

typedef struct api_render_job api_render_job_t

Functions

int api_server_main (void)

Typedef Documentation

◆ api_render_job_t

Definition at line 20 of file api-server/main.c.

Function Documentation

◆ api_server_main()

int api_server_main ( void )

Definition at line 492 of file api-server/main.c.

492 {
493 http_server_t *server = NULL;
494 api_server_t api = {0};
495 quic_endpoint_t *http3_endpoint = NULL;
496 quic_endpoint_t *http3_endpoint6 = NULL;
497 const char *tls_cert = GET_OPTION(tls_cert);
498 const char *tls_key = GET_OPTION(tls_key);
499 const char *tls_password = GET_OPTION(tls_key_password);
500 if (!tls_cert || !*tls_cert)
501 tls_cert = NULL;
502 if (!tls_key || !*tls_key)
503 tls_key = NULL;
504 if (!tls_password || !*tls_password)
505 tls_password = NULL;
506 http_server_config_t config = {.bind_address = GET_OPTION(address),
507 .bind_address6 = GET_OPTION(address6),
508 .port = (uint16_t)GET_OPTION(port),
509 .enable_http1 = true,
510 .enable_http2 = true,
511 .enable_http3 = false,
512 .tls_cert_path = tls_cert,
513 .tls_key_path = tls_key,
514 .tls_key_password = tls_password,
515 .max_connections = (size_t)GET_OPTION(max_clients),
516 .max_request_body_bytes = 10u * 1024u * 1024u,
517 .max_response_body_bytes = 8u * 1024u * 1024u,
518 .on_request = api_request,
519 .on_wakeup = api_wakeup};
520 if (!config.max_connections)
521 config.max_connections = 1;
523 http_renderer_config_t renderer_config = {.max_jobs = (unsigned)config.max_connections,
524 .max_input_bytes = config.max_request_body_bytes,
525 .max_output_bytes = config.max_response_body_bytes,
526 .timeout_ms = 10000,
527 .restricted = true,
528 .on_complete = api_render_complete,
529 .on_error = api_render_error,
530 .user_data = &api};
531 err = http_renderer_init(&api.renderer, &renderer_config);
532 if (err != ASCIICHAT_OK)
533 return err;
534 config.on_request = api_request;
535 config.user_data = &api;
536 err = http_server_init(&server, &config);
537 if (err != ASCIICHAT_OK) {
538 LOG_ERRNO_IF_SET("API HTTP server initialization failed");
540 return err;
541 }
542 api.http = server;
543 if (tls_cert && tls_key) {
544 struct sockaddr_storage bind_address;
545 socklen_t bind_length = 0;
546 err = parse_numeric_socket_address(GET_OPTION(address), (uint16_t)GET_OPTION(port), &bind_address, &bind_length);
547 if (err == ASCIICHAT_OK) {
548 quic_config_t quic_config = {.alpn = "h3",
549 .certificate_file = tls_cert,
550 .private_key_file = tls_key,
551 .enable_http3 = true,
552 .http_server = server};
553 err =
554 quic_endpoint_open((const struct sockaddr *)&bind_address, bind_length, true, &quic_config, &http3_endpoint);
555 if (err == ASCIICHAT_OK && GET_OPTION(address6) && *GET_OPTION(address6)) {
556 struct sockaddr_storage bind_address6;
557 socklen_t bind_length6 = 0;
558 err = parse_numeric_socket_address(GET_OPTION(address6), (uint16_t)GET_OPTION(port), &bind_address6,
559 &bind_length6);
560 if (err == ASCIICHAT_OK)
561 err = quic_endpoint_open((const struct sockaddr *)&bind_address6, bind_length6, true, &quic_config,
562 &http3_endpoint6);
563 }
564 }
565 if (err != ASCIICHAT_OK) {
566 LOG_ERRNO_IF_SET("API HTTP/3 listener initialization failed");
567 http_server_destroy(&server);
569 return err;
570 }
571 }
572 err = http_server_start(server);
573 if (err != ASCIICHAT_OK) {
574 LOG_ERRNO_IF_SET("API HTTP server start failed");
575 if (http3_endpoint) {
576 (void)quic_endpoint_stop(http3_endpoint);
577 (void)quic_endpoint_destroy(&http3_endpoint);
578 }
579 if (http3_endpoint6) {
580 (void)quic_endpoint_stop(http3_endpoint6);
581 (void)quic_endpoint_destroy(&http3_endpoint6);
582 }
583 http_server_destroy(&server);
585 return err;
586 }
587 platform_systemd_notify_status("API HTTP listener started");
588 platform_systemd_notify_ready();
590 uv_sleep(16);
591 http_server_stop(server);
592 if (http3_endpoint) {
593 (void)quic_endpoint_stop(http3_endpoint);
594 (void)quic_endpoint_destroy(&http3_endpoint);
595 }
596 if (http3_endpoint6) {
597 (void)quic_endpoint_stop(http3_endpoint6);
598 (void)quic_endpoint_destroy(&http3_endpoint6);
599 }
601 api_discard_jobs(&api);
602 http_server_destroy(&server);
603 return ASCIICHAT_OK;
604}
#define APP_CALLBACK_BOOL(callback_name)
#define LOG_ERRNO_IF_SET(message)
asciichat_error_t quic_endpoint_open(const struct sockaddr *address, socklen_t length, bool listener, const quic_config_t *config, quic_endpoint_t **out)
Definition endpoint.c:131
asciichat_error_t quic_endpoint_stop(quic_endpoint_t *endpoint)
Definition endpoint.c:308
asciichat_error_t quic_endpoint_destroy(quic_endpoint_t **endpoint)
Definition endpoint.c:353
unsigned short uint16_t
Definition common.h:61
asciichat_error_t
Error and exit codes - unified status values (0-255).
Definition error_codes.h:44
@ ASCIICHAT_OK
Definition error_codes.h:46
#define GET_OPTION(field)
Safely get a specific option field (lock-free read).
Definition options.h:1336
asciichat_error_t http_renderer_init(http_renderer_t **out, const http_renderer_config_t *config)
void http_renderer_destroy(http_renderer_t **renderer)
struct quic_endpoint quic_endpoint_t
asciichat_error_t parse_numeric_socket_address(const char *host, uint16_t port, struct sockaddr_storage *address, socklen_t *length)
Definition ip.c:154
asciichat_error_t http_server_init(http_server_t **out, const http_server_config_t *cfg)
void http_server_stop(http_server_t *s)
void http_server_destroy(http_server_t **p)
asciichat_error_t http_server_start(http_server_t *s)
bool should_exit(void)
Definition main.c:249
http_server_t * http
http_renderer_t * renderer
struct http_server http_server_t
Definition tls.h:8

References APP_CALLBACK_BOOL, ASCIICHAT_OK, GET_OPTION, api_server_t::http, http_renderer_destroy(), http_renderer_init(), http_server_destroy(), http_server_init(), http_server_start(), http_server_stop(), LOG_ERRNO_IF_SET, http_server_config_t::max_connections, http_server_config_t::max_request_body_bytes, http_server_config_t::max_response_body_bytes, http_server_config_t::on_request, parse_numeric_socket_address(), quic_endpoint_destroy(), quic_endpoint_open(), quic_endpoint_stop(), api_server_t::renderer, should_exit(), and http_server_config_t::user_data.