Skip to main content

vetis_macros/
lib.rs

1#[macro_export]
2macro_rules! http {
3    (hostname => $hostname:expr, port => $port:expr, interface => $interface:expr, handler => $handler:ident) => {
4        async move {
5            use vetis::{
6                config::{ListenerConfig, ServerConfig, VirtualHostConfig},
7                errors::VetisError,
8                server::path::HandlerPath,
9                server::virtual_host::VirtualHost,
10                Vetis,
11            };
12
13            let listener = ListenerConfig::builder()
14                .port($port)
15                .interface($interface)
16                .build();
17
18            let config = ServerConfig::builder()
19                .add_listener(listener)
20                .build();
21
22            let virtual_host_config = VirtualHostConfig::builder()
23                .hostname($hostname)
24                .port($port)
25                .build()?;
26
27            let mut virtual_host = VirtualHost::new(virtual_host_config);
28
29            let root_path = HandlerPath::new_host_path("/", Box::new($handler));
30
31            virtual_host.add_path(root_path);
32
33            let mut vetis = vetis::Vetis::new(config);
34
35            vetis
36                .add_virtual_host(virtual_host)
37                .await;
38
39            Ok::<Vetis, Box<VetisError>>(vetis)
40        }
41    };
42
43    (hostname => $hostname:literal, port => $port:literal, interface => $interface:literal, handler => $handler:ident) => {
44        async move {
45            use vetis::{
46                config::{ListenerConfig, ServerConfig, VirtualHostConfig},
47                errors::VetisError,
48                server::{path::HandlerPath, virtual_host::VirtualHost},
49                Vetis,
50            };
51
52            let listener = ListenerConfig::builder()
53                .port($port)
54                .interface($interface)
55                .build();
56
57            let config = ServerConfig::builder()
58                .add_listener(listener)
59                .build();
60
61            let virtual_host_config = VirtualHostConfig::builder()
62                .hostname($hostname)
63                .port($port)
64                .build()?;
65
66            let mut virtual_host = VirtualHost::new(virtual_host_config);
67
68            let root_path = HandlerPath::new_host_path("/", Box::new($handler));
69
70            virtual_host.add_path(root_path);
71
72            let mut vetis = Vetis::new(config);
73
74            vetis
75                .add_virtual_host(virtual_host)
76                .await;
77
78            Ok::<Vetis, Box<VetisError>>(vetis)
79        }
80    };
81}
82
83#[macro_export]
84macro_rules! https {
85    (hostname => &$hostname:ident, port => &$port:ident, interface => &$interface:ident, &cert => &$cert:ident, &key => &$key:ident) => {
86        use vetis::{
87            config::{ListenerConfig, ServerConfig, VirtualHostConfig},
88            errors::VetisError,
89            server::{path::HandlerPath, virtual_host::VirtualHost},
90            Vetis,
91        };
92
93        let listener = ListenerConfig::builder()
94            .port($port)
95            .interface($interface)
96            .build();
97
98        let config = ServerConfig::builder()
99            .add_listener(listener)
100            .build();
101
102        let security_config = SecurityConfig::builder()
103            .cert_from_file($cert.to_string())
104            .key_from_file($key.to_string())
105            .build();
106
107        let virtual_host_config = VirtualHostConfig::builder()
108            .hostname($hostname)
109            .port($port)
110            .security(security_config)
111            .build()?;
112
113        let mut virtual_host = VirtualHost::new(virtual_host_config);
114
115        let root_path = HandlerPath::new_host_path("/", Box::new($handler));
116
117        virtual_host.add_path(root_path);
118
119        let mut vetis = Vetis::new(config);
120
121        vetis
122            .add_virtual_host(virtual_host)
123            .await;
124
125        Ok::<Vetis, Box<VetisError>>(vetis)
126    };
127}