Github User Fetcher 1.0.0
C Application with Server and GUI
Loading...
Searching...
No Matches
main.cpp
Go to the documentation of this file.
1/**
2 * @file main.cpp
3 * @brief Entry point for the application. Parses command-line arguments
4 * and either starts the HTTP server or fetches and displays a GitHub
5 * user.
6 *
7 * This file contains the main function which acts as the orchestrator of the
8 * program. It interprets the configuration from the user, and based on that
9 * configuration, it either launches a local HTTP server or performs a GitHub
10 * API request to fetch user data, then displays it to stdout.
11 */
12
13#include "config.hpp"
14#include "server.hpp"
15#include "user.hpp"
16
17#include <iostream>
18
19/**
20 * @brief Main function that initializes and runs the application.
21 *
22 * This function performs the following steps:
23 * - Parses command-line arguments into an AppConfig object.
24 * - Based on the parsed configuration:
25 * - If `run_server` is true, launches the HTTP server on the specified port.
26 * - Otherwise, fetches a GitHub user and prints the result.
27 *
28 * Any exceptions thrown during the execution are caught and logged to `stderr`,
29 * with a non-zero return code indicating failure.
30 *
31 * @param argc Argument count.
32 * @param argv Argument vector.
33 * @return int Exit status code (0 for success, non-zero for failure).
34 */
35int main(int argc, char **argv) {
36 try {
37 // Convert argv to vector<string> for easier handling
38 std::vector<std::string> args(argv + 1, argv + argc);
39
40 // Parse arguments into configuration
41 AppConfig config = parse_arguments(args);
42
43 if (config.run_server) {
44 // Start HTTP server if --server option is set
45 start_http_server(config.port);
46 } else {
47 // Otherwise fetch and display GitHub user info
48 User user = User::fetch_github_user(config.username);
49 user.print();
50 }
51
52 return 0;
53 } catch (const std::exception &ex) {
54 std::cerr << "Error: " << ex.what() << "\n";
55 return 1;
56 }
57}
AppConfig parse_arguments(const std::vector< std::string > &args)
Parses command-line arguments into an AppConfig structure.
Definition config.cpp:31
int main(void)
Definition sanitycheckc.c:1
void start_http_server(int port)
Starts an HTTP server on the specified port.
Definition server.c:59
Structure representing a GitHub user.
Definition user.h:9