Skip to content

Commit 7eda882

Browse files
committed
feat(middleware): add static_dir bridge and module init hook
1 parent c58066f commit 7eda882

File tree

4 files changed

+100
-0
lines changed

4 files changed

+100
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
*
3+
* @file module_init.hpp
4+
* @author Gaspard Kirira
5+
*
6+
* Copyright 2025, 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_MIDDLEWARE_MODULE_INIT_HPP
14+
#define VIX_MIDDLEWARE_MODULE_INIT_HPP
15+
16+
extern "C" void vix_middleware_module_init();
17+
18+
#endif
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/**
2+
*
3+
* @file static_dir_bridge.hpp
4+
* @author Gaspard Kirira
5+
*
6+
* Copyright 2025, 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_MIDDLEWARE_STATIC_DIR_BRIDGE_HPP
14+
#define VIX_MIDDLEWARE_STATIC_DIR_BRIDGE_HPP
15+
16+
#include <vix/app/App.hpp>
17+
#include <vix/middleware/middleware.hpp>
18+
#include <vix/middleware/performance/static_files.hpp>
19+
20+
namespace vix::middleware
21+
{
22+
inline void register_static_dir()
23+
{
24+
static vix::mw::Services g_services{};
25+
26+
vix::App::set_static_handler(
27+
[](vix::App &app,
28+
const std::filesystem::path &root,
29+
const std::string &mount,
30+
const std::string &index_file,
31+
bool add_cache_control,
32+
const std::string &cache_control,
33+
bool fallthrough) -> bool
34+
{
35+
vix::middleware::performance::StaticFilesOptions opt;
36+
opt.mount = mount;
37+
opt.index_file = index_file;
38+
opt.add_cache_control = add_cache_control;
39+
opt.cache_control = cache_control;
40+
opt.fallthrough = fallthrough;
41+
42+
auto mw = vix::middleware::performance::static_files(root, std::move(opt));
43+
44+
auto httpmw = vix::middleware::to_http_middleware(std::move(mw), g_services);
45+
46+
vix::App::Middleware appmw =
47+
[httpmw = std::move(httpmw)](
48+
vix::vhttp::Request &req,
49+
vix::vhttp::ResponseWrapper &res,
50+
vix::App::Next next) mutable
51+
{
52+
httpmw(req, res, vix::mw::Next([n = std::move(next)]() mutable
53+
{ n(); }));
54+
};
55+
56+
app.use(std::string(mount), std::move(appmw));
57+
return true;
58+
});
59+
}
60+
}
61+
62+
#endif

src/module_init.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#include <vix/middleware/static_dir_bridge.hpp>
2+
3+
extern "C" void vix_middleware_module_init()
4+
{
5+
vix::middleware::register_static_dir();
6+
}

src/static_dir_bridge.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#include <vix/middleware/static_dir_bridge.hpp>
2+
3+
namespace
4+
{
5+
struct AutoRegisterStaticDir
6+
{
7+
AutoRegisterStaticDir()
8+
{
9+
vix::middleware::register_static_dir();
10+
}
11+
};
12+
13+
static AutoRegisterStaticDir g_auto_register{};
14+
}

0 commit comments

Comments
 (0)