ascii-chat
Binary v0.16.114-28-gf5913657f
Library lib/v0.3.2-3302-gf5913657f
Video chat in your terminal
Loading...
Searching...
No Matches
main.h File Reference

Go to the source code of this file.

Functions

int api_server_main (void)

Function Documentation

◆ api_server_main()

int api_server_main ( void )

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

535 {
536 http_server_t *server = NULL;
537 api_server_t api = {0};
538 quic_endpoint_t *http3_endpoint = NULL;
539 quic_endpoint_t *http3_endpoint6 = NULL;
540 const char *tls_cert = GET_OPTION(tls_cert);
541 const char *tls_key = GET_OPTION(tls_key);
542 const char *tls_password = GET_OPTION(tls_key_password);
543 if (!tls_cert || !*tls_cert)
544 tls_cert = NULL;
545 if (!tls_key || !*tls_key)
546 tls_key = NULL;
547 if (!tls_password || !*tls_password)
548 tls_password = NULL;
549 http_server_config_t config = {.bind_address = GET_OPTION(address),
550 .bind_address6 = GET_OPTION(address6),
551 .port = (uint16_t)GET_OPTION(port),
552 .enable_http1 = true,
553 .enable_http2 = true,
554 .enable_http3 = false,
555 .tls_cert_path = tls_cert,
556 .tls_key_path = tls_key,
557 .tls_key_password = tls_password,
558 .max_connections =
559 (size_t)GET_OPTION(max_clients),
560 .max_request_body_bytes = 10u * 1024u * 1024u,
561 .max_response_body_bytes = 8u * 1024u * 1024u,
562 .on_request = api_request,
563 .on_wakeup = api_wakeup};
564 if (!config.max_connections)
565 config.max_connections = 1;
567 http_renderer_config_t renderer_config = {
568 .max_jobs = (unsigned)config.max_connections,
569 .max_input_bytes = config.max_request_body_bytes,
570 .max_output_bytes = config.max_response_body_bytes,
571 .timeout_ms = 10000,
572 .restricted = true,
573 .on_complete = api_render_complete,
574 .on_error = api_render_error,
575 .user_data = &api};
576 err = http_renderer_init(&api.renderer, &renderer_config);
577 if (err != ASCIICHAT_OK)
578 return err;
579 config.on_request = api_request;
580 config.user_data = &api;
581 err = http_server_init(&server, &config);
582 if (err != ASCIICHAT_OK) {
583 LOG_ERRNO_IF_SET("API HTTP server initialization failed");
585 return err;
586 }
587 api.http = server;
588 if (tls_cert && tls_key) {
589 struct sockaddr_storage bind_address;
590 socklen_t bind_length = 0;
592 (uint16_t)GET_OPTION(port),
593 &bind_address, &bind_length);
594 if (err == ASCIICHAT_OK) {
595 quic_config_t quic_config = {.alpn = "h3",
596 .certificate_file = tls_cert,
597 .private_key_file = tls_key,
598 .enable_http3 = true,
599 .http_server = server};
600 err =
601 quic_endpoint_open((const struct sockaddr *)&bind_address,
602 bind_length, true, &quic_config, &http3_endpoint);
603 if (err == ASCIICHAT_OK && *GET_OPTION(address6)) {
604 struct sockaddr_storage bind_address6;
605 socklen_t bind_length6 = 0;
607 (uint16_t)GET_OPTION(port),
608 &bind_address6, &bind_length6);
609 if (err == ASCIICHAT_OK)
610 err = quic_endpoint_open((const struct sockaddr *)&bind_address6,
611 bind_length6, true, &quic_config,
612 &http3_endpoint6);
613 }
614 }
615 if (err != ASCIICHAT_OK) {
616 LOG_ERRNO_IF_SET("API HTTP/3 listener initialization failed");
617 http_server_destroy(&server);
619 return err;
620 }
621 }
622 err = http_server_start(server);
623 if (err != ASCIICHAT_OK) {
624 LOG_ERRNO_IF_SET("API HTTP server start failed");
625 if (http3_endpoint) {
626 (void)quic_endpoint_stop(http3_endpoint);
627 (void)quic_endpoint_destroy(&http3_endpoint);
628 }
629 if (http3_endpoint6) {
630 (void)quic_endpoint_stop(http3_endpoint6);
631 (void)quic_endpoint_destroy(&http3_endpoint6);
632 }
633 http_server_destroy(&server);
635 return err;
636 }
637 platform_systemd_notify_status("API HTTP listener started");
638 platform_systemd_notify_ready();
641 uv_sleep(16);
642 http_server_stop(server);
643 if (http3_endpoint) {
644 (void)quic_endpoint_stop(http3_endpoint);
645 (void)quic_endpoint_destroy(&http3_endpoint);
646 }
647 if (http3_endpoint6) {
648 (void)quic_endpoint_stop(http3_endpoint6);
649 (void)quic_endpoint_destroy(&http3_endpoint6);
650 }
652 api_discard_jobs(&api);
653 http_server_destroy(&server);
654 return ASCIICHAT_OK;
655}
#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:128
asciichat_error_t quic_endpoint_stop(quic_endpoint_t *endpoint)
Definition endpoint.c:289
asciichat_error_t quic_endpoint_destroy(quic_endpoint_t **endpoint)
Definition endpoint.c:334
unsigned short uint16_t
asciichat_error_t
Error and exit codes - unified status values (0-255).
Definition error_codes.h:48
@ ASCIICHAT_OK
Definition error_codes.h:50
#define GET_OPTION(field)
Definition options.h:1339
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:155
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:260
http_server_t * http
http_renderer_t * renderer
struct http_server http_server_t
Definition tls.h:8
void windows_service_ready(void)

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(), http_server_config_t::user_data, and windows_service_ready().