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

Implements a simple HTTP server using Civetweb to serve GitHub user information. More...

#include "server.h"
#include "../vendor/civetweb/civetweb.h"
#include "user.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

Go to the source code of this file.

Functions

static int user_handler (struct mg_connection *conn, void *cbdata)
 HTTP request handler for user lookup.
 
void start_http_server (int port)
 Starts an HTTP server on the specified port.
 

Detailed Description

Implements a simple HTTP server using Civetweb to serve GitHub user information.

Definition in file server.c.

Function Documentation

◆ start_http_server()

void start_http_server ( int port)

Starts an HTTP server on the specified port.

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.

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().

◆ user_handler()

static int user_handler ( struct mg_connection * conn,
void * cbdata )
static

HTTP request handler for user lookup.

Parses the request URI to extract the username, fetches the corresponding GitHub user, and responds with a JSON object.

Parameters
connThe connection to the client.
cbdataCallback data (unused).
Returns
HTTP status code (e.g., 200 for success, 400 for bad request).

Definition at line 26 of file server.c.

26 {
27 (void)cbdata; // Mark parameter as deliberately unused
28 const struct mg_request_info *req_info = mg_get_request_info(conn);
29 const char *uri = req_info->local_uri;
30
31 // Validate that the URI is valid and null-terminated
32 size_t uri_len = strnlen(uri, 1024); // Limit max length to avoid over-read
33 if (uri_len < 2) {
34 mg_printf(conn, "HTTP/1.1 400 Bad Request\r\nContent-Type: "
35 "text/plain\r\n\r\nMissing username\n");
36 return 400;
37 }
38
39 const char *username = uri + 1; // skip the initial '/'
40 User user = fetch_github_user(username);
41
42 mg_printf(conn, "HTTP/1.1 200 OK\r\nContent-Type: application/json\r\n\r\n");
43 mg_printf(conn,
44 "{\n \"login\": \"%s\",\n \"name\": \"%s\",\n \"company\": "
45 "\"%s\",\n \"location\": \"%s\"\n}\n",
46 user.login, user.name, user.company, user.location);
47
48 return 200;
49}
CIVETWEB_API const struct mg_request_info * mg_get_request_info(const struct mg_connection *conn)
Definition civetweb.c:3521
CIVETWEB_API int mg_printf(struct mg_connection *conn, const char *fmt,...)
Definition civetweb.c:7034
Structure representing a GitHub user.
Definition user.h:9
const char * name
Full name.
Definition user.h:11
const char * login
GitHub username.
Definition user.h:10
const char * location
User's location.
Definition user.h:13
const char * company
Company name.
Definition user.h:12
const char * local_uri
Definition civetweb.h:157
User fetch_github_user(const char *username)
Fetch a GitHub user's information via the GitHub API.
Definition user.c:130

References User::company, fetch_github_user(), mg_request_info::local_uri, User::location, User::login, mg_get_request_info(), mg_printf(), and User::name.

Referenced by start_http_server().