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

mod fire;
use fire::RequestConfigs;

#[cfg(feature = "fs")]
pub mod fs;

#[cfg(feature = "json")]
pub mod json;

#[cfg(feature = "ws")]
pub mod ws;

use std::io;
use std::net::SocketAddr;
use std::time::Duration;
use std::any::Any;

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

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

pub use codegen::*;

/// Prepares a server.
pub async fn build(addr: impl ToSocketAddrs) -> io::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) -> io::Result<Self>
	where A: ToSocketAddrs {
		let addr = tokio::net::lookup_host(addr).await?.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;
	}

	/// Lights the fire, which starts the server.
	/// 
	/// ## Note
	/// Under normal conditions this function should run forever.
	pub async fn light(self) -> Result<()> {

		if self.show_startup_msg {
			println!("Running server on addr: {}", self.addr);
		}

		let wood = fire::Wood::new(self.data, self.routes, self.configs);

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

		server.serve().await
			.map_err(Error::from_server_error)
	}

	#[doc(hidden)]
	pub async fn test_light(self) -> (SocketAddr, task::JoinHandle<()>) {
		let wood = fire::Wood::new(self.data, self.routes, self.configs);

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

		let addr = server.local_addr().unwrap();
		let task = tokio::spawn(async move {
			server.serve().await.expect("test server failed");
		});

		(addr, task)
	}

}