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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
#![cfg_attr(docsrs, feature(doc_cfg))]
#![doc = include_str!("../README.md")]

#[macro_use]
mod macros;

pub mod resources;
use resources::Resources;

pub mod state;

pub mod routes;
use routes::{Catcher, ParamsNames, RawRoute, Route, Routes};

#[macro_use]
pub mod util;

pub mod into;
use into::IntoRoute;

pub mod error;
pub use error::{Error, Result};

pub mod extractor;
pub use extractor::Res;

mod server;
use server::Server;

mod routing;
use routing::{RequestConfigs, ServerShared};
use tracing::info;

#[cfg(feature = "fs")]
#[cfg_attr(docsrs, doc(cfg(feature = "fs")))]
pub mod fs;

#[cfg(feature = "json")]
#[cfg_attr(docsrs, doc(cfg(feature = "json")))]
pub mod json;

#[cfg(feature = "ws")]
#[cfg_attr(docsrs, doc(cfg(feature = "ws")))]
pub mod ws;

#[cfg(feature = "graphql")]
#[cfg_attr(docsrs, doc(cfg(feature = "graphql")))]
pub mod graphql;

#[cfg(feature = "api")]
#[cfg_attr(docsrs, doc(cfg(feature = "api")))]
pub mod api;

#[cfg(feature = "json")]
#[doc(hidden)]
pub use serde_json;

pub mod service {
	pub use crate::server::ChuchiService;
}

use std::any::Any;
use std::net::SocketAddr;
use std::sync::Arc;
use std::time::Duration;

use tokio::net::ToSocketAddrs;
use tokio::task::JoinHandle;

pub use chuchi_core::{
	body, header, request, response, Body, Request, Response,
};

pub use chuchi_codegen::*;

/// Prepares a server.
pub async fn build(addr: impl ToSocketAddrs) -> Result<Chuchi> {
	Chuchi::new(addr).await
}

/// `FireBuilder` gathers all materials needed to light a fire (start a server).
pub struct Chuchi {
	addr: SocketAddr,
	resources: Resources,
	routes: Routes,
	configs: RequestConfigs,
}

impl Chuchi {
	pub(crate) async fn new<A>(addr: A) -> Result<Self>
	where
		A: ToSocketAddrs,
	{
		let addr = tokio::net::lookup_host(addr)
			.await
			.map_err(Error::from_server_error)?
			.next()
			.unwrap();
		Ok(Self {
			addr,
			resources: Resources::new(),
			routes: Routes::new(),
			configs: RequestConfigs::new(),
		})
	}

	/// Returns a reference to the current data.
	pub fn resources(&self) -> &Resources {
		&self.resources
	}

	pub fn add_resource<R>(&mut self, resource: R)
	where
		R: Any + Send + Sync,
	{
		self.resources.insert(resource);
	}

	/// Adds a `RawRoute` to the fire.
	pub fn add_raw_route<R>(&mut self, route: R)
	where
		R: RawRoute + 'static,
	{
		let path = route.path();
		let names = ParamsNames::parse(&path.path);
		route.validate_requirements(&names, &self.resources);
		self.routes.push_raw(path, route)
	}

	/// Adds a `Route` to the fire.
	pub fn add_route<R>(&mut self, route: R)
	where
		R: IntoRoute + 'static,
	{
		let route = route.into_route();
		let path = route.path();
		let names = ParamsNames::parse(&path.path);
		route.validate_requirements(&names, &self.resources);
		self.routes.push(path, route)
	}

	/// Adds a `Catcher` to the fire.
	pub fn add_catcher<C>(&mut self, catcher: C)
	where
		C: Catcher + 'static,
	{
		catcher.validate_data(&self.resources);
		self.routes.push_catcher(catcher)
	}

	/// Sets the request size limit. The default is 4 kilobytes.
	///
	/// This can be changed in every Route.
	///
	/// ## Panics
	/// If the size is zero.
	pub fn request_size_limit(&mut self, size_limit: usize) {
		self.configs.size_limit(size_limit)
	}

	/// Sets the request timeout. The default is 60 seconds.
	///
	/// This can be changed in every Route.
	pub fn request_timeout(&mut self, timeout: Duration) {
		self.configs.timeout(timeout)
	}

	/// Binds to the address and prepares to serve requests.
	///
	/// You need to call ignite on the `Fire` so that it starts handling
	/// requests.
	pub async fn build(self) -> Result<ChuchiServer> {
		let wood = Arc::new(ServerShared::new(
			self.resources,
			self.routes,
			self.configs,
		));

		let server = Server::bind(self.addr, wood.clone()).await?;

		Ok(ChuchiServer {
			shared: wood,
			server,
		})
	}

	/// Ignites the fire, which starts the server.
	///
	/// ## Note
	/// Under normal conditions this function should run forever.
	pub async fn run(self) -> Result<()> {
		let server = self.build().await?;
		server.run().await
	}

	/// Ignites the fire, and spawns it on a new tokio task.
	///
	/// ## Note
	/// Under normal conditions this task should run forever.
	pub fn run_task(self) -> JoinHandle<()> {
		tokio::spawn(async move { self.run().await.unwrap() })
	}

	/// Creates a FirePit without starting the server.
	///
	/// In most cases you should use `build` and then call `pit` on the `Fire`.
	///
	/// Creating a `FirePit` might be useful for testing or if you want to
	/// manually create a server.
	pub fn into_shared(self) -> ChuchiShared {
		let wood = Arc::new(ServerShared::new(
			self.resources,
			self.routes,
			self.configs,
		));

		ChuchiShared { inner: wood }
	}
}

/// A Fire that is ready to be ignited.
pub struct ChuchiServer {
	shared: Arc<ServerShared>,
	server: Server,
}

impl ChuchiServer {
	pub fn local_addr(&self) -> Option<SocketAddr> {
		self.server.local_addr().ok()
	}

	pub fn shared(&self) -> ChuchiShared {
		ChuchiShared {
			inner: self.shared.clone(),
		}
	}

	pub async fn run(self) -> Result<()> {
		info!("Running server on addr: {}", self.local_addr().unwrap());

		#[cfg(any(feature = "http1", feature = "http2"))]
		{
			self.server.serve().await
		}

		#[cfg(not(any(feature = "http1", feature = "http2")))]
		{
			panic!("http1 or http2 feature must be enabled")
		}
	}
}

#[derive(Clone)]
pub struct ChuchiShared {
	inner: Arc<ServerShared>,
}

impl ChuchiShared {
	pub fn data(&self) -> &Resources {
		self.inner.data()
	}

	/// Routes the request to normal routes and returns their result.
	///
	/// Useful for tests and niche applications.
	///
	/// Returns None if no route was found matching the request.
	pub async fn route(&self, req: &mut Request) -> Option<Result<Response>> {
		routing::route(&self.inner, req).await
	}
}