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
#![cfg_attr(docsrs, feature(doc_cfg))]
#![doc = include_str!("../README.md")]

mod data;
pub use data::Data;

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

#[macro_use]
pub mod util;

pub mod into;
use into::IntoRoute;

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

mod server;
use server::Server;

mod fire;
use fire::{RequestConfigs, Wood};

#[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;

pub mod service {
	pub use crate::server::{MakeFireService, FireService};
}

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

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

pub use types;
pub use types::{Request, Response, Body, header, body};

pub use codegen::*;

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

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

impl FireBuilder {
	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,
			data: Data::new(),
			routes: Routes::new(),
			configs: RequestConfigs::new(),
			show_startup_msg: true
		})
	}

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

	pub fn add_data<D>(&mut self, data: D)
	where D: Any + Send + Sync {
		self.data.insert(data);
	}

	/// Adds a `RawRoute` to the fire.
	pub fn add_raw_route<R>(&mut self, route: R)
	where R: RawRoute + 'static {
		route.validate_data(&self.data);
		self.routes.push_raw(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();
		route.validate_data(&self.data);
		self.routes.push(route)
	}

	/// Adds a `Catcher` to the fire.
	pub fn add_catcher<C>(&mut self, catcher: C)
	where C: Catcher + 'static {
		catcher.validate_data(&self.data);
		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)
	}

	/// Prevents the fire from showing a message when the server get's started.
	pub fn hide_startup_message(&mut self) {
		self.show_startup_msg = false;
	}

	/// 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<Fire> {
		let wood = Arc::new(Wood::new(self.data, self.routes, self.configs));

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

		Ok(Fire {
			wood, server,
			show_startup_msg: self.show_startup_msg
		})
	}

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

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

	/// Creates a tower service which can be used to build a manual hyper
	/// server. Unless you wan't to use a custom transport layer don't use this.
	pub fn into_make_fire_service(self) -> service::MakeFireService {
		let wood = Arc::new(Wood::new(self.data, self.routes, self.configs));

		service::MakeFireService { wood }
	}
}

/// A Fire that is ready to be ignited.
pub struct Fire {
	wood: Arc<Wood>,
	server: Server,
	show_startup_msg: bool
}

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

	pub fn pit(&self) -> FirePit {
		FirePit { wood: self.wood.clone() }
	}

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

		self.server.serve().await
	}
}

#[derive(Clone)]
pub struct FirePit {
	wood: Arc<Wood>
}

impl FirePit {
	pub fn data(&self) -> &Data {
		self.wood.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>> {
		fire::route(&self.wood, req).await
	}
}