emme coverage


Directory: src/
File: src/main.c
Date: 2026-05-14 14:35:13
Exec Total Coverage
Lines: 23 54 42.6%
Functions: 1 1 100.0%
Branches: 13 42 31.0%

Line Branch Exec Source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include "server.h"
5 #include "config.h"
6 #include "log.h"
7 #include "metrics.h"
8 #include "backend_pool.h"
9
10 #define DEFAULT_METRICS_PORT 9090
11 #define MAX_PORT_NUMBER 65535
12
13 10 int main(int argc, char **argv) {
14 ServerConfig config;
15 10 char *config_path = "config.yaml";
16
17
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 10 times.
20 for (int i = 1; i < argc; i++) {
18
2/4
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 10 times.
✗ Branch 3 not taken.
10 if (strcmp(argv[i], "--config") == 0 && i + 1 < argc) {
19 10 config_path = argv[++i];
20 }
21 }
22
23 10 const char *env_config = getenv("EMME_CONFIG_PATH");
24
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
10 if (env_config) {
25 config_path = (char *)env_config;
26 }
27
28
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 10 times.
10 if (load_config(&config, config_path) != 0) {
29 fprintf(stderr, "Error loading configuration from %s\n", config_path);
30 return 1;
31 }
32
33 10 apply_env_overrides(&config);
34
35
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 10 times.
10 if (log_init(&config.logging) != 0) {
36 fprintf(stderr, "Error initializing logging\n");
37 exit(EXIT_FAILURE);
38 }
39
40
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 10 times.
28 for (int i = 0; i < config.route_count; i++) {
41 18 Route *route = &config.routes[i];
42
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 18 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
18 if (route->http2_enabled && route->backend[0] != '\0') {
43 char host[256];
44 int port;
45 if (parse_backend_url(route->backend, host, sizeof(host), &port) == 0) {
46 route->pool = backend_pool_create(host, port, route->tls_enabled,
47 route->tls_verify,
48 route->connection_pool.size);
49 if (route->pool) {
50 log_message(LOG_LEVEL_INFO, "Created connection pool for %s (size=%d)",
51 route->backend, route->connection_pool.size);
52
53 if (route->health_check.enabled) {
54 if (backend_pool_start_health_checker(route->pool,
55 &route->health_check) == 0) {
56 log_message(LOG_LEVEL_INFO, "Health checker started for %s",
57 route->backend);
58 }
59 }
60
61 if (route->circuit_breaker.enabled) {
62 if (backend_pool_init_circuit_breaker(route->pool,
63 &route->circuit_breaker) == 0) {
64 log_message(LOG_LEVEL_INFO, "Circuit breaker initialized for %s",
65 route->backend);
66 }
67 }
68 } else {
69 log_message(LOG_LEVEL_ERROR, "Failed to create pool for %s", route->backend);
70 }
71 }
72 }
73 }
74
75 10 metrics_init();
76
77 10 const char *env_metrics_port = getenv("EMME_METRICS_PORT");
78 10 int metrics_port = DEFAULT_METRICS_PORT;
79
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
10 if (env_metrics_port) {
80 char *endptr;
81 long port = strtol(env_metrics_port, &endptr, 10);
82 if (*endptr == '\0' && port > 0 && port <= MAX_PORT_NUMBER) {
83 metrics_port = (int)port;
84 } else {
85 log_message(LOG_LEVEL_WARN, "Invalid EMME_METRICS_PORT value '%s', using default %d",
86 env_metrics_port, metrics_port);
87 }
88 }
89
90
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 10 times.
10 if (metrics_start_server(metrics_port) != 0) {
91 log_message(LOG_LEVEL_ERROR, "Failed to start metrics server on port %d", metrics_port);
92 }
93
94 10 log_message(LOG_LEVEL_INFO, "Starting server on port %d (max_connections=%d)",
95 config.port, config.max_connections);
96
97
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 10 times.
10 if (start_server(&config) != 0) {
98 log_message(LOG_LEVEL_ERROR, "Error starting server");
99 metrics_shutdown();
100 log_shutdown();
101 exit(EXIT_FAILURE);
102 }
103
104 10 metrics_shutdown();
105 10 log_shutdown();
106 10 return EXIT_SUCCESS;
107 }
108