1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
//! # sod-actix-web
//!
//! This crate provides [`sod::Service`] abstractions around [`actix_web`] via [`Handler`] implementations.
//!
//! # Handlers
//!
//! The [`ServiceHandler`] acts as an [`actix_web`] [`Handler`], dispatching requests to an underlying
//! [`sod::AsyncService`] or [`sod::Service`] implementation.
//!
//! ## Service I/O
//!
//! The input to the underlying [`AsyncService`] is directly compatible with the native [`FromRequest`] trait
//! in [`actix_web`]. As such, a tuple of [`FromRequest`] impls can be handled as input for an [`AsyncService`].
//!
//! The output from the underlying [`AsyncService`] must implement the native [`Responder`] trait from [`actix_web`].
//! This means that all output type from the service should be compatible with all output types from [`actix_web`].
//! This should include a simple [`String`] or full [`actix_web::HttpResponse`].
//!
//! ## Greet Server Example
//!
//! The following example mirrors the default [`actix_web`] greeter example, except it uses the service abstraction
//! provided by this library:
//!
//! ```rust,no_run
//! use actix_web::{web, App, HttpServer};
//! use sod::Service;
//! use sod_actix_web::ServiceHandler;
//!
//! #[actix_web::main]
//! async fn main() -> std::io::Result<()> {
//! struct GreetService;
//! impl Service for GreetService {
//! type Input = web::Path<String>;
//! type Output = String;
//! type Error = std::convert::Infallible;
//! fn process(&self, name: web::Path<String>) -> Result<Self::Output, Self::Error> {
//! Ok(format!("Hello {name}!"))
//! }
//! }
//!
//! HttpServer::new(|| {
//! App::new().service(
//! web::resource("/greet/{name}").route(web::get().to(ServiceHandler::new(GreetService.into_async()))),
//! )
//! })
//! .bind(("127.0.0.1", 8080))?
//! .run()
//! .await
//! }
//! ```
//!
//! ## Math Server Example
//!
//! The following example is slightly more advanced, demonstrating how [`AsyncService`] and a tuple of inputs may be used:
//!
//! ```rust,no_run
//! use std::{io::Error, io::ErrorKind};
//! use actix_web::{web, App, HttpServer};
//! use serde_derive::Deserialize;
//! use sod::{async_trait, AsyncService};
//! use sod_actix_web::ServiceHandler;
//!
//! #[actix_web::main]
//! async fn main() -> std::io::Result<()> {
//! #[derive(Debug, Deserialize)]
//! pub struct MathParams {
//! a: i64,
//! b: i64,
//! }
//!
//! struct MathService;
//! #[async_trait]
//! impl AsyncService for MathService {
//! type Input = (web::Path<String>, web::Query<MathParams>);
//! type Output = String;
//! type Error = Error;
//! async fn process(
//! &self,
//! (func, params): (web::Path<String>, web::Query<MathParams>),
//! ) -> Result<Self::Output, Self::Error> {
//! let value = match func.as_str() {
//! "add" => params.a + params.b,
//! "sub" => params.a - params.b,
//! "mul" => params.a * params.b,
//! "div" => params.a / params.b,
//! _ => return Err(Error::new(ErrorKind::Other, "invalid func")),
//! };
//! Ok(format!("{value}"))
//! }
//! }
//!
//! HttpServer::new(|| {
//! App::new().service(
//! web::resource("/math/{func}").route(web::get().to(ServiceHandler::new(MathService))),
//! )
//! })
//! .bind(("127.0.0.1", 8080))?
//! .run()
//! .await
//! }
//! ```
//!
//! # WebSockets
//!
//! WebSocket [`sod::Service`] abstractions are provided in the [`ws`] module.
use ;
use ;
use AsyncService;
/// The highest level abstraction provided by this library. It is used to encapsulate underlying [`sod::Service`]
/// impls with an [`actix_web`] [`Handler`] that can be natively wired into an Actix [`actix_web::App`].
///
/// Input tuples of [`FromRequest`] and outputs of [`Responder`] the responder trait make this directly compatible
/// with the native Actix request and response types.
///
/// See the this module's documentation for details and examples.