example_server/
example_server.rs1use std::sync::Arc;
2
3use async_trait::async_trait;
4use http::Response;
5use hyper::Body;
6
7use hyper_fast::server::{ApiError, HttpResponse, HttpRoute, Service};
8use hyper_fast::server::{ServiceBuilder, ServiceDaemon, start_http_server};
9#[cfg(feature = "settings")]
10use hyper_fast::server::utils::load_config;
11#[cfg(any(feature = "access_log", feature = "metrics"))]
12use hyper_fast::server::utils::setup_logging;
13
14#[tokio::main(flavor = "multi_thread")]
15async fn main() -> Result<(), anyhow::Error> {
16 #[cfg(feature = "settings")]
17 load_config("examples/config", "dev")?;
18
19 #[cfg(any(feature = "access_log"))]
20 setup_logging("examples/config/log4rs.yml")?;
21
22 start_http_server("127.0.0.1:6464", ExampleServiceBuilder {}).await
23}
24
25pub struct ExampleService {
26 }
28
29pub struct ExampleServiceDaemon {}
30
31pub struct ExampleServiceBuilder {
32 }
34
35#[async_trait]
36impl ServiceDaemon<ExampleService> for ExampleServiceDaemon {
37 async fn start(&self, _service: Arc<ExampleService>) {
38 }
40}
41
42#[async_trait]
43impl ServiceBuilder<ExampleService, ExampleServiceDaemon> for ExampleServiceBuilder {
44 async fn build(self) -> anyhow::Result<(ExampleService, Option<ExampleServiceDaemon>)> {
45 let service = ExampleService {};
46
47 Ok((service, None))
48 }
49}
50
51#[async_trait]
52impl Service for ExampleService {
53 async fn api_handler<'a>(
54 &'a self,
55 _: Body,
56 route: &HttpRoute<'a>,
57 path: &[&str],
58 ) -> Result<Response<Body>, ApiError> {
59 match path {
60 ["test"] if matches!(route.method, &http::Method::GET) => {
61 self.get_test(route).await
62 }
63 _ => HttpResponse::not_found(route.path),
64 }
65 }
66}
67
68impl ExampleService {
69 pub async fn get_test(&self, route: &HttpRoute<'_>) -> Result<Response<Body>, ApiError> {
70 HttpResponse::string(route, "GET::/api/test - test passed".to_string())
71 }
72}
73