Skip to content

Commit aab9d99

Browse files
committed
refactor(new): improve backend and web templates
2 parents 93312c1 + 0d48de2 commit aab9d99

15 files changed

Lines changed: 707 additions & 72 deletions
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* @file BackendTemplateUtils.hpp
3+
* @author Gaspard Kirira
4+
*
5+
* Copyright 2025, Gaspard Kirira. All rights reserved.
6+
* https://github.com/vixcpp/vix
7+
* Use of this source code is governed by a MIT license
8+
* that can be found in the License file.
9+
*/
10+
11+
#ifndef VIX_CLI_COMMANDS_NEW_TEMPLATES_BACKEND_TEMPLATE_UTILS_HPP
12+
#define VIX_CLI_COMMANDS_NEW_TEMPLATES_BACKEND_TEMPLATE_UTILS_HPP
13+
14+
#include <string>
15+
16+
namespace vix::commands::new_cmd::templates
17+
{
18+
/**
19+
* @brief Build a stable include guard for a generated backend header.
20+
*
21+
* The generated guard includes the project name and a file-specific suffix
22+
* to avoid collisions between generated headers and installed Vix headers.
23+
*
24+
* @param projectName Name of the generated backend project.
25+
* @param suffix File-specific suffix, for example APP_BOOTSTRAP or HOME_CONTROLLER.
26+
* @return Include guard name.
27+
*/
28+
std::string make_backend_header_guard(
29+
const std::string &projectName,
30+
const std::string &suffix);
31+
32+
} // namespace vix::commands::new_cmd::templates
33+
34+
#endif // VIX_CLI_COMMANDS_NEW_TEMPLATES_BACKEND_TEMPLATE_UTILS_HPP
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* @file WebTemplateUtils.hpp
3+
* @author Gaspard Kirira
4+
*
5+
* Copyright 2025, Gaspard Kirira. All rights reserved.
6+
* https://github.com/vixcpp/vix
7+
* Use of this source code is governed by a MIT license
8+
* that can be found in the License file.
9+
*/
10+
11+
#ifndef VIX_CLI_COMMANDS_NEW_TEMPLATES_WEB_TEMPLATE_UTILS_HPP
12+
#define VIX_CLI_COMMANDS_NEW_TEMPLATES_WEB_TEMPLATE_UTILS_HPP
13+
14+
#include <string>
15+
16+
namespace vix::commands::new_cmd::templates
17+
{
18+
/**
19+
* @brief Build a stable include guard for a generated web header.
20+
*
21+
* @param projectName Name of the generated web project.
22+
* @param suffix File-specific suffix, for example APP_BOOTSTRAP.
23+
* @return Include guard name.
24+
*/
25+
std::string make_web_header_guard(
26+
const std::string &projectName,
27+
const std::string &suffix);
28+
29+
} // namespace vix::commands::new_cmd::templates
30+
31+
#endif // VIX_CLI_COMMANDS_NEW_TEMPLATES_WEB_TEMPLATE_UTILS_HPP

src/commands/new/templates/backend/BackendBootstrapTemplates.cpp

Lines changed: 68 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
*/
1010

1111
#include <vix/cli/commands/new/templates/backend/BackendBootstrapTemplates.hpp>
12-
12+
#include <vix/cli/commands/new/templates/backend/BackendTemplateUtils.hpp>
13+
#include <algorithm>
14+
#include <cctype>
1315
#include <string>
1416

1517
namespace vix::commands::new_cmd::templates
@@ -18,9 +20,24 @@ namespace vix::commands::new_cmd::templates
1820
std::string make_backend_main_cpp(const std::string &projectName)
1921
{
2022
std::string s;
21-
s.reserve(400);
23+
s.reserve(900);
24+
25+
s += "/**\n";
26+
s += " * @file main.cpp\n";
27+
s += " * @brief Entry point for the " + projectName + " backend application.\n";
28+
s += " */\n\n";
2229

2330
s += "#include <" + projectName + "/app/AppBootstrap.hpp>\n\n";
31+
32+
s += "/**\n";
33+
s += " * @brief Start the backend application.\n";
34+
s += " *\n";
35+
s += " * The main function stays intentionally small. Application setup,\n";
36+
s += " * middleware registration, route registration, and server startup are\n";
37+
s += " * delegated to " + projectName + "::app::AppBootstrap.\n";
38+
s += " *\n";
39+
s += " * @return Process exit code.\n";
40+
s += " */\n";
2441
s += "int main()\n";
2542
s += "{\n";
2643
s += " " + projectName + "::app::AppBootstrap bootstrap;\n";
@@ -32,49 +49,93 @@ namespace vix::commands::new_cmd::templates
3249

3350
std::string make_backend_app_bootstrap_hpp(const std::string &projectName)
3451
{
52+
const std::string guard = make_backend_header_guard(projectName, "APP_BOOTSTRAP");
53+
3554
std::string s;
36-
s.reserve(900);
55+
s.reserve(1600);
56+
57+
s += "/**\n";
58+
s += " * @file AppBootstrap.hpp\n";
59+
s += " * @brief Application bootstrap for the " + projectName + " backend.\n";
60+
s += " */\n\n";
61+
62+
s += "#ifndef " + guard + "\n";
63+
s += "#define " + guard + "\n\n";
3764

38-
s += "#pragma once\n\n";
3965
s += "namespace " + projectName + "::app\n";
4066
s += "{\n";
67+
s += " /**\n";
68+
s += " * @brief Owns the startup sequence of the backend application.\n";
69+
s += " *\n";
70+
s += " * AppBootstrap keeps main.cpp minimal and centralizes the application\n";
71+
s += " * initialization flow: configuration loading, Vix app creation,\n";
72+
s += " * template/static directory setup, middleware registration, route\n";
73+
s += " * registration, and server startup.\n";
74+
s += " */\n";
4175
s += " class AppBootstrap\n";
4276
s += " {\n";
4377
s += " public:\n";
44-
s += " AppBootstrap() = default;\n";
78+
s += " /**\n";
79+
s += " * @brief Create a default application bootstrap instance.\n";
80+
s += " */\n";
81+
s += " AppBootstrap() = default;\n\n";
82+
83+
s += " /**\n";
84+
s += " * @brief Destroy the application bootstrap instance.\n";
85+
s += " */\n";
4586
s += " ~AppBootstrap() = default;\n\n";
87+
4688
s += " AppBootstrap(const AppBootstrap &) = delete;\n";
4789
s += " AppBootstrap &operator=(const AppBootstrap &) = delete;\n";
4890
s += " AppBootstrap(AppBootstrap &&) = delete;\n";
4991
s += " AppBootstrap &operator=(AppBootstrap &&) = delete;\n\n";
92+
93+
s += " /**\n";
94+
s += " * @brief Run the backend application.\n";
95+
s += " *\n";
96+
s += " * @return Process exit code. Returns 0 when the application exits normally.\n";
97+
s += " */\n";
5098
s += " int run();\n";
5199
s += " };\n";
52-
s += "} // namespace " + projectName + "::app\n";
100+
s += "} // namespace " + projectName + "::app\n\n";
101+
102+
s += "#endif // " + guard + "\n";
53103

54104
return s;
55105
}
56106

57107
std::string make_backend_app_bootstrap_cpp(const std::string &projectName)
58108
{
59109
std::string s;
60-
s.reserve(2400);
110+
s.reserve(3200);
111+
112+
s += "/**\n";
113+
s += " * @file AppBootstrap.cpp\n";
114+
s += " * @brief Startup implementation for the " + projectName + " backend.\n";
115+
s += " */\n\n";
61116

62117
s += "#include <" + projectName + "/app/AppBootstrap.hpp>\n";
63118
s += "#include <" + projectName + "/presentation/middleware/MiddlewareRegistry.hpp>\n";
64119
s += "#include <" + projectName + "/presentation/routes/RouteRegistry.hpp>\n\n";
120+
65121
s += "#include <vix.hpp>\n";
66122
s += "#include <vix/log.hpp>\n\n";
123+
67124
s += "namespace " + projectName + "::app\n";
68125
s += "{\n";
69126
s += " int AppBootstrap::run()\n";
70127
s += " {\n";
71128
s += " vix::config::Config cfg{\".env\"};\n";
72129
s += " vix::App app;\n\n";
130+
73131
s += " app.templates(\"views\");\n";
74132
s += " app.static_dir(\"public\", \"/\");\n\n";
133+
75134
s += " presentation::middleware::MiddlewareRegistry::register_all(app);\n";
76135
s += " presentation::routes::RouteRegistry::register_all(app);\n\n";
136+
77137
s += " vix::log::info(\"Starting " + projectName + " on port {}\", cfg.getServerPort());\n\n";
138+
78139
s += " app.run(cfg);\n";
79140
s += " return 0;\n";
80141
s += " }\n";

src/commands/new/templates/backend/BackendControllerTemplates.cpp

Lines changed: 69 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,41 +9,71 @@
99
*/
1010

1111
#include <vix/cli/commands/new/templates/backend/BackendControllerTemplates.hpp>
12+
#include <vix/cli/commands/new/templates/backend/BackendTemplateUtils.hpp>
1213

14+
#include <cctype>
1315
#include <string>
1416

1517
namespace vix::commands::new_cmd::templates
1618
{
17-
1819
std::string make_backend_home_controller_hpp(const std::string &projectName)
1920
{
21+
const std::string guard = make_backend_header_guard(projectName, "HOME_CONTROLLER");
22+
2023
std::string s;
21-
s.reserve(800);
24+
s.reserve(1700);
25+
26+
s += "/**\n";
27+
s += " * @file HomeController.hpp\n";
28+
s += " * @brief Home API routes for the " + projectName + " backend.\n";
29+
s += " */\n\n";
30+
31+
s += "#ifndef " + guard + "\n";
32+
s += "#define " + guard + "\n\n";
2233

23-
s += "#pragma once\n\n";
2434
s += "namespace vix\n";
2535
s += "{\n";
2636
s += " class App;\n";
2737
s += "}\n\n";
38+
2839
s += "namespace " + projectName + "::presentation::controllers\n";
2940
s += "{\n";
41+
s += " /**\n";
42+
s += " * @brief Registers public home routes for the backend API.\n";
43+
s += " *\n";
44+
s += " * HomeController owns lightweight routes that are useful for verifying\n";
45+
s += " * that the generated backend is reachable and correctly wired.\n";
46+
s += " */\n";
3047
s += " class HomeController\n";
3148
s += " {\n";
3249
s += " public:\n";
50+
s += " /**\n";
51+
s += " * @brief Register the home routes on the given Vix application.\n";
52+
s += " *\n";
53+
s += " * @param app Target application receiving the routes.\n";
54+
s += " */\n";
3355
s += " static void register_routes(vix::App &app);\n";
3456
s += " };\n";
35-
s += "} // namespace " + projectName + "::presentation::controllers\n";
57+
s += "} // namespace " + projectName + "::presentation::controllers\n\n";
58+
59+
s += "#endif // " + guard + "\n";
3660

3761
return s;
3862
}
3963

4064
std::string make_backend_home_controller_cpp(const std::string &projectName)
4165
{
4266
std::string s;
43-
s.reserve(1500);
67+
s.reserve(2200);
68+
69+
s += "/**\n";
70+
s += " * @file HomeController.cpp\n";
71+
s += " * @brief Home route implementation for the " + projectName + " backend.\n";
72+
s += " */\n\n";
4473

4574
s += "#include <" + projectName + "/presentation/controllers/HomeController.hpp>\n\n";
4675
s += "#include <vix.hpp>\n\n";
76+
4777
s += "namespace " + projectName + "::presentation::controllers\n";
4878
s += "{\n";
4979
s += " void HomeController::register_routes(vix::App &app)\n";
@@ -65,33 +95,62 @@ namespace vix::commands::new_cmd::templates
6595

6696
std::string make_backend_health_controller_hpp(const std::string &projectName)
6797
{
98+
const std::string guard = make_backend_header_guard(projectName, "HEALTH_CONTROLLER");
99+
68100
std::string s;
69-
s.reserve(800);
101+
s.reserve(1700);
102+
103+
s += "/**\n";
104+
s += " * @file HealthController.hpp\n";
105+
s += " * @brief Health check routes for the " + projectName + " backend.\n";
106+
s += " */\n\n";
107+
108+
s += "#ifndef " + guard + "\n";
109+
s += "#define " + guard + "\n\n";
70110

71-
s += "#pragma once\n\n";
72111
s += "namespace vix\n";
73112
s += "{\n";
74113
s += " class App;\n";
75114
s += "}\n\n";
115+
76116
s += "namespace " + projectName + "::presentation::controllers\n";
77117
s += "{\n";
118+
s += " /**\n";
119+
s += " * @brief Registers health check routes for the backend application.\n";
120+
s += " *\n";
121+
s += " * HealthController exposes routes intended for local checks, reverse\n";
122+
s += " * proxies, deployment scripts, and production monitoring.\n";
123+
s += " */\n";
78124
s += " class HealthController\n";
79125
s += " {\n";
80126
s += " public:\n";
127+
s += " /**\n";
128+
s += " * @brief Register health check routes on the given Vix application.\n";
129+
s += " *\n";
130+
s += " * @param app Target application receiving the routes.\n";
131+
s += " */\n";
81132
s += " static void register_routes(vix::App &app);\n";
82133
s += " };\n";
83-
s += "} // namespace " + projectName + "::presentation::controllers\n";
134+
s += "} // namespace " + projectName + "::presentation::controllers\n\n";
135+
136+
s += "#endif // " + guard + "\n";
84137

85138
return s;
86139
}
87140

88141
std::string make_backend_health_controller_cpp(const std::string &projectName)
89142
{
90143
std::string s;
91-
s.reserve(1700);
144+
s.reserve(2600);
145+
146+
s += "/**\n";
147+
s += " * @file HealthController.cpp\n";
148+
s += " * @brief Health route implementation for the " + projectName + " backend.\n";
149+
s += " */\n\n";
92150

93151
s += "#include <" + projectName + "/presentation/controllers/HealthController.hpp>\n\n";
94152
s += "#include <vix.hpp>\n\n";
153+
95154
s += "namespace " + projectName + "::presentation::controllers\n";
96155
s += "{\n";
97156
s += " void HealthController::register_routes(vix::App &app)\n";
@@ -105,6 +164,7 @@ namespace vix::commands::new_cmd::templates
105164
s += " \"service\", \"" + projectName + "\"\n";
106165
s += " });\n";
107166
s += " });\n\n";
167+
108168
s += " app.get(\"/api/health\", [](vix::Request &req, vix::Response &res)\n";
109169
s += " {\n";
110170
s += " (void)req;\n\n";

0 commit comments

Comments
 (0)