Github User Fetcher 1.0.0
C Application with Server and GUI
Loading...
Searching...
No Matches
server.h File Reference

Declaration of the HTTP server functions. More...

Go to the source code of this file.

Functions

void start_http_server (int port)
 Starts the HTTP server.
 

Detailed Description

Declaration of the HTTP server functions.

Definition in file server.h.

Function Documentation

◆ start_http_server()

void start_http_server ( int port)

Starts the HTTP server.

Initializes and runs the Civetweb server on the specified port.

Parameters
portTCP port number to listen on.

Starts the HTTP server.

Configures Civetweb with basic options and handles incoming user requests. The server runs indefinitely until manually interrupted.

Parameters
portThe TCP port to listen on.

Starts the HTTP server.

Sets up an io_context, signal handlers for graceful shutdown, and an asynchronous TCP acceptor to listen for and handle incoming HTTP connections using HttpSession.

The server runs in a multi-threaded fashion, utilizing all available hardware threads.

Parameters
portThe port number on which the HTTP server should listen.

Lambda for accepting incoming connections recursively.

This lambda captures itself and re-issues accept calls until the server is stopped.

Definition at line 59 of file server.c.

59 {
60 char *port_str = NULL;
61 if (asprintf(&port_str, "%d", port) == -1) {
62 (void)fprintf(stderr, "Failed to allocate memory for port\n");
63 return;
64 }
65
66 const char *options[] = {"listening_ports",
67 port_str,
68 "enable_keep_alive",
69 "yes",
70 "access_log_file",
71 "access.log",
72 NULL};
73
74 struct mg_callbacks callbacks = {0};
75 struct mg_context *ctx = mg_start(&callbacks, NULL, options);
76
77 if (!ctx) {
78 (void)fprintf(stderr, "Failed to start Civetweb server\n");
79 return;
80 }
81
83
84 printf("Server started on port %s. Visit http://localhost:%s/USERNAME\n",
85 port_str, port_str);
86 puts("Press Ctrl+C to quit.");
87 free(port_str);
88
89 // Keep running (or use signal handler in future)
90 while (1) {
91 sleep(1);
92 }
93
94 mg_stop(ctx);
95}
#define free
Definition civetweb.c:1542
CIVETWEB_API void mg_stop(struct mg_context *ctx)
Definition civetweb.c:20346
CIVETWEB_API struct mg_context * mg_start(const struct mg_callbacks *callbacks, void *user_data, const char **options)
Definition civetweb.c:21205
CIVETWEB_API void mg_set_request_handler(struct mg_context *ctx, const char *uri, mg_request_handler handler, void *cbdata)
Definition civetweb.c:14466
#define NULL
Definition gmacros.h:924
static int user_handler(struct mg_connection *conn, void *cbdata)
HTTP request handler for user lookup.
Definition server.c:26
struct mg_callbacks callbacks
Definition civetweb.c:2432
#define printf

References mg_context::callbacks, free, mg_set_request_handler(), mg_start(), mg_stop(), NULL, printf, and user_handler().

Referenced by main(), and TEST_CASE().