Skip to content

Commit bb4635d

Browse files
committed
feat(cli): add production health checks
2 parents e2583e5 + 5140fd1 commit bb4635d

11 files changed

Lines changed: 1092 additions & 0 deletions

File tree

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/**
2+
*
3+
* @file HealthCommand.hpp
4+
* @author Gaspard Kirira
5+
*
6+
* Copyright 2026, Gaspard Kirira. All rights reserved.
7+
* https://github.com/vixcpp/vix
8+
* Use of this source code is governed by a MIT license
9+
* that can be found in the License file.
10+
*
11+
* Vix.cpp
12+
*/
13+
#ifndef VIX_HEALTH_COMMAND_HPP
14+
#define VIX_HEALTH_COMMAND_HPP
15+
16+
#include <string>
17+
#include <vector>
18+
19+
namespace vix::commands
20+
{
21+
/**
22+
* @brief Command entry point for production health checks.
23+
*/
24+
struct HealthCommand
25+
{
26+
/**
27+
* @brief Run the health command.
28+
*
29+
* Supported forms:
30+
*
31+
* `vix health`
32+
* `vix health local`
33+
* `vix health public`
34+
* `vix health websocket`
35+
*
36+
* @param args Command arguments after `health`.
37+
* @return Process exit code.
38+
*/
39+
static int run(const std::vector<std::string> &args);
40+
41+
/**
42+
* @brief Print health command help.
43+
*
44+
* @return Process exit code.
45+
*/
46+
static int help();
47+
};
48+
}
49+
50+
#endif
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/**
2+
*
3+
* @file HealthChecker.hpp
4+
* @author Gaspard Kirira
5+
*
6+
* Copyright 2026, Gaspard Kirira. All rights reserved.
7+
* https://github.com/vixcpp/vix
8+
* Use of this source code is governed by a MIT license
9+
* that can be found in the License file.
10+
*
11+
* Vix.cpp
12+
*/
13+
#ifndef VIX_HEALTH_CHECKER_HPP
14+
#define VIX_HEALTH_CHECKER_HPP
15+
16+
#include <vix/cli/commands/health/HealthTypes.hpp>
17+
18+
namespace vix::commands::health::checker
19+
{
20+
/**
21+
* @brief Run all configured health checks.
22+
*
23+
* @param cfg Loaded health configuration.
24+
* @return Process exit code.
25+
*/
26+
int check_all(const HealthConfig &cfg);
27+
28+
/**
29+
* @brief Run the local application health check.
30+
*
31+
* @param cfg Loaded health configuration.
32+
* @return Process exit code.
33+
*/
34+
int check_local(const HealthConfig &cfg);
35+
36+
/**
37+
* @brief Run the public HTTPS health check.
38+
*
39+
* @param cfg Loaded health configuration.
40+
* @return Process exit code.
41+
*/
42+
int check_public(const HealthConfig &cfg);
43+
44+
/**
45+
* @brief Run the WebSocket health check.
46+
*
47+
* @param cfg Loaded health configuration.
48+
* @return Process exit code.
49+
*/
50+
int check_websocket(const HealthConfig &cfg);
51+
}
52+
53+
#endif
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
*
3+
* @file HealthConfig.hpp
4+
* @author Gaspard Kirira
5+
*
6+
* Copyright 2026, Gaspard Kirira. All rights reserved.
7+
* https://github.com/vixcpp/vix
8+
* Use of this source code is governed by a MIT license
9+
* that can be found in the License file.
10+
*
11+
* Vix.cpp
12+
*/
13+
#ifndef VIX_HEALTH_CONFIG_HPP
14+
#define VIX_HEALTH_CONFIG_HPP
15+
16+
#include <vix/cli/commands/health/HealthTypes.hpp>
17+
18+
#include <optional>
19+
#include <string>
20+
21+
namespace vix::commands::health
22+
{
23+
/**
24+
* @brief Read the current Vix project name.
25+
*
26+
* @return The detected project name, or std::nullopt if no name can be resolved.
27+
*/
28+
std::optional<std::string> read_project_name();
29+
30+
/**
31+
* @brief Load production health configuration from vix.json.
32+
*
33+
* The configuration is read from `production.health`.
34+
*
35+
* @return Loaded health check configuration.
36+
*/
37+
HealthConfig load_health_config();
38+
39+
/**
40+
* @brief Return a readable name for a health target.
41+
*
42+
* @param target Health target.
43+
* @return Target name.
44+
*/
45+
const char *target_name(HealthTarget target) noexcept;
46+
}
47+
48+
#endif
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/**
2+
*
3+
* @file HealthOutput.hpp
4+
* @author Gaspard Kirira
5+
*
6+
* Copyright 2026, Gaspard Kirira. All rights reserved.
7+
* https://github.com/vixcpp/vix
8+
* Use of this source code is governed by a MIT license
9+
* that can be found in the License file.
10+
*
11+
* Vix.cpp
12+
*/
13+
#ifndef VIX_HEALTH_OUTPUT_HPP
14+
#define VIX_HEALTH_OUTPUT_HPP
15+
16+
#include <vix/cli/commands/health/HealthTypes.hpp>
17+
18+
#include <iosfwd>
19+
#include <string>
20+
21+
namespace vix::commands::health::output
22+
{
23+
/**
24+
* @brief Print the health command summary.
25+
*
26+
* @param out Output stream.
27+
* @param cfg Loaded health configuration.
28+
*/
29+
void print_summary(
30+
std::ostream &out,
31+
const HealthConfig &cfg);
32+
33+
/**
34+
* @brief Print one health check result.
35+
*
36+
* @param out Output stream.
37+
* @param result Health check result.
38+
*/
39+
void print_result(
40+
std::ostream &out,
41+
const HealthResult &result);
42+
43+
/**
44+
* @brief Print a successful health message.
45+
*
46+
* @param out Output stream.
47+
* @param message Message to print.
48+
*/
49+
void ok(
50+
std::ostream &out,
51+
const std::string &message);
52+
53+
/**
54+
* @brief Print a health warning message.
55+
*
56+
* @param out Output stream.
57+
* @param message Message to print.
58+
*/
59+
void warn(
60+
std::ostream &out,
61+
const std::string &message);
62+
63+
/**
64+
* @brief Print a health error message.
65+
*
66+
* @param out Output stream.
67+
* @param message Message to print.
68+
*/
69+
void error(
70+
std::ostream &out,
71+
const std::string &message);
72+
73+
/**
74+
* @brief Print a fix suggestion.
75+
*
76+
* @param out Output stream.
77+
* @param message Fix suggestion.
78+
*/
79+
void fix(
80+
std::ostream &out,
81+
const std::string &message);
82+
}
83+
84+
#endif
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
/**
2+
*
3+
* @file HealthTypes.hpp
4+
* @author Gaspard Kirira
5+
*
6+
* Copyright 2026, Gaspard Kirira. All rights reserved.
7+
* https://github.com/vixcpp/vix
8+
* Use of this source code is governed by a MIT license
9+
* that can be found in the License file.
10+
*
11+
* Vix.cpp
12+
*/
13+
#ifndef VIX_HEALTH_TYPES_HPP
14+
#define VIX_HEALTH_TYPES_HPP
15+
16+
#include <cstdint>
17+
#include <optional>
18+
#include <string>
19+
20+
namespace vix::commands::health
21+
{
22+
/**
23+
* @enum HealthTarget
24+
* @brief Health check target type.
25+
*/
26+
enum class HealthTarget
27+
{
28+
Local,
29+
Public,
30+
WebSocket
31+
};
32+
33+
/**
34+
* @struct HealthEndpointConfig
35+
* @brief Configuration for one health endpoint.
36+
*/
37+
struct HealthEndpointConfig
38+
{
39+
/**
40+
* @brief Whether this endpoint is configured and can be checked.
41+
*/
42+
bool enabled{false};
43+
44+
/**
45+
* @brief Endpoint URL.
46+
*/
47+
std::string url{};
48+
49+
/**
50+
* @brief Expected HTTP status code.
51+
*/
52+
int expectedStatus{200};
53+
54+
/**
55+
* @brief Request timeout in milliseconds.
56+
*/
57+
std::uint64_t timeoutMs{5000};
58+
59+
/**
60+
* @brief Maximum accepted response time in milliseconds.
61+
*/
62+
std::uint64_t maxResponseMs{1000};
63+
};
64+
65+
/**
66+
* @struct HealthConfig
67+
* @brief Production health check configuration.
68+
*/
69+
struct HealthConfig
70+
{
71+
/**
72+
* @brief Application name.
73+
*/
74+
std::string appName{"vix-app"};
75+
76+
/**
77+
* @brief Optional systemd service name checked before health requests.
78+
*/
79+
std::optional<std::string> serviceName{};
80+
81+
/**
82+
* @brief Local internal application endpoint.
83+
*/
84+
HealthEndpointConfig local{};
85+
86+
/**
87+
* @brief Public HTTPS endpoint.
88+
*/
89+
HealthEndpointConfig publicEndpoint{};
90+
91+
/**
92+
* @brief Public WebSocket endpoint.
93+
*/
94+
HealthEndpointConfig websocket{};
95+
};
96+
97+
/**
98+
* @struct HealthResult
99+
* @brief Result of one health check.
100+
*/
101+
struct HealthResult
102+
{
103+
/**
104+
* @brief Checked target.
105+
*/
106+
HealthTarget target{HealthTarget::Local};
107+
108+
/**
109+
* @brief Checked URL.
110+
*/
111+
std::string url{};
112+
113+
/**
114+
* @brief Expected HTTP status code.
115+
*/
116+
int expectedStatus{200};
117+
118+
/**
119+
* @brief Actual HTTP status code.
120+
*/
121+
int actualStatus{0};
122+
123+
/**
124+
* @brief Measured response time in milliseconds.
125+
*/
126+
std::uint64_t responseMs{0};
127+
128+
/**
129+
* @brief Maximum accepted response time in milliseconds.
130+
*/
131+
std::uint64_t maxResponseMs{1000};
132+
133+
/**
134+
* @brief Whether the endpoint is healthy.
135+
*/
136+
bool healthy{false};
137+
138+
/**
139+
* @brief Human-readable error message when unhealthy.
140+
*/
141+
std::string error{};
142+
};
143+
}
144+
145+
#endif

0 commit comments

Comments
 (0)