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
//! Module that deals with a web server.
//!
//! See [run_web](fn.run_web.html) documentation for details.
use DbInterface;
use HttpResult;
use Duration;
use Listening;
use *;
use Pastebin;
use ToSocketAddrs;
use Tera;
/// Runs a web server.
///
/// This is the main function of the library. Starts a web server and serves the
/// following HTTP requests: `GET`, `POST` and `DELETE`.
///
/// Basically it is just a layer between an `Iron` web server and a `DbInterface` implementation.
///
/// The call returns a `HttpResult` which comes directly from `Iron`, which means you can possibly
/// terminate the server in a clean way. If you don't `close` it explicitly, the object will hang
/// forever in its `drop` implementation. For more details have a look at the
/// `iron::error::HttpResult` documentation.
///
/// # Arguments
///
/// * `default_ttl` represents the default expiration time which will be applied if not
/// `expires` argument for a `POST` request is given.
///
/// # Notice
///
/// * No matter how many ending slashes you added to `url_prefix` (even zero), all of them will be
/// removed and one slash will be added.
///
/// # Example
///
/// Let's say you have some kind of a database wrapper implemented (`DbImplementation`) and you
/// want to run a server with it:
///
/// ```
/// # extern crate pastebin;
/// # extern crate bson;
/// # extern crate chrono;
/// # use pastebin::{DbInterface, PasteEntry};
/// # use std::io;
/// # use chrono::{DateTime, Duration, Utc};
/// # struct DbImplementation;
/// # impl DbInterface for DbImplementation {
/// # type Error = io::Error;
/// # fn store_data(&self,
/// # _data: Vec<u8>,
/// # _file_name: Option<String>,
/// # _mime_type: String,
/// # _best_before: Option<DateTime<Utc>>)
/// # -> Result<u64, Self::Error> {
/// # unimplemented!()
/// # }
/// # fn load_data(&self, _: u64) -> Result<Option<PasteEntry>, Self::Error> {
/// # unimplemented!()
/// # }
/// # fn get_file_name(&self, _: u64) -> Result<Option<String>, Self::Error> {
/// # unimplemented!()
/// # }
/// # fn remove_data(&self, _: u64) -> Result<(), Self::Error> {
/// # unimplemented!()
/// # }
/// # fn max_data_size(&self) -> usize {
/// # unimplemented!()
/// # }
/// # }
/// # impl DbImplementation {
/// # fn new() -> Self { Self{} }
/// # }
/// # fn main() {
/// let mut web = pastebin::web::run_web(
/// DbImplementation::new(/* ... */),
/// "127.0.0.1:8000",
/// // ...
/// # Default::default(),
/// # Default::default(),
/// # Duration::zero(),
/// # Default::default(),
/// ).unwrap();
/// // ... do something ...
/// web.close(); // Graceful termination.
/// println!("Server terminated, exiting");
/// # }
/// ```
///
/// Simple, isn't it? It can be even simplier if you don't care about graceful termination and
/// you're okay with the application working forever:
///
/// ```no_run
/// # extern crate pastebin;
/// # extern crate bson;
/// # extern crate chrono;
/// # use pastebin::{DbInterface, PasteEntry};
/// # use std::io;
/// # use chrono::{DateTime, Duration, Utc};
/// # struct DbImplementation;
/// # impl DbInterface for DbImplementation {
/// # type Error = io::Error;
/// # fn store_data(&self,
/// # _data: Vec<u8>,
/// # _file_name: Option<String>,
/// # _mime_type: String,
/// # _best_before: Option<DateTime<Utc>>)
/// # -> Result<u64, Self::Error> {
/// # unimplemented!()
/// # }
/// # fn load_data(&self, _: u64) -> Result<Option<PasteEntry>, Self::Error> {
/// # unimplemented!()
/// # }
/// # fn get_file_name(&self, _: u64) -> Result<Option<String>, Self::Error> {
/// # unimplemented!()
/// # }
/// # fn remove_data(&self, _: u64) -> Result<(), Self::Error> {
/// # unimplemented!()
/// # }
/// # fn max_data_size(&self) -> usize {
/// # unimplemented!()
/// # }
/// # }
/// # impl DbImplementation {
/// # fn new() -> Self { Self{} }
/// # }
/// # fn main() {
/// pastebin::web::run_web(
/// DbImplementation::new(/* ... */),
/// "127.0.0.1:8000",
/// // ...
/// # Default::default(),
/// # Default::default(),
/// # Duration::zero(),
/// # Default::default(),
/// ).unwrap();
/// println!("Ok done"); // <-- will never be reached.
/// # }
/// ```