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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
/// The `vial::run!` macro is the preferred way of starting your Vial
/// application after you've defined one or more routes using
/// [`vial::routes!`](macro.routes.html). `run!` performs a bit of
/// necessary setup, then starts listening for client requests at
/// http://0.0.0.0:7667 by default.
///
/// There are four ways to use `run!`:
///
/// 1. `vial::run!()`: No arguments. Starts listening at
///    http://0.0.0.0:7667 and expects you to have called
///    [`vial::routes!`](macro.routes.html) in the current module.
///
/// 2. `vial::run!("localhost:9999")`: With your own address.
///
/// 3. `vial::run!(blog, wiki)`: With modules that you've called
///    `vial::routes!` from within. This will combine all the routes.
///
///    For example:
///
/// ```no_run
/// mod wiki {
///     vial::routes! {
///         GET "/wiki" => |_| Response::from_file("wiki.html");
///     }
/// }
///
/// mod blog {
///     vial::routes! {
///         GET "/blog" => show_blog;
///         // etc...
///     }
///
///     fn show_blog(req: vial::Request) -> String {
///         // ...
///         "blog".to_string()
///     }
/// }
///
/// fn main() {
///     vial::run!(wiki, blog).unwrap();
/// }
/// ```
///
/// 4. Using a combination of the above:
///
/// ```no_run
/// # mod blog { vial::routes! { GET "/blog" => |_| ""; }}
/// # mod wiki { vial::routes! { GET "/wiki" => |_| ""; }}
///
/// fn main() {
///     vial::run!("localhost:1111", blog, wiki).unwrap();
/// }
/// ```
#[macro_export]
macro_rules! run {
    () => {
        vial::run!("0.0.0.0:7667")
    };
    ($addr:expr) => {{
        vial::run!($addr, self)
    }};
    ($($module:ident),+) => {{
        vial::run!("0.0.0.0:7667", $($module),+)
    }};
    ($addr:expr, $($module:ident),+) => {{
        vial::setup!();
        let mut router = ::vial::Router::new();
        $($module::vial_add_to_router(&mut router);)+
        vial::run($addr, router, None)
    }};
}

/// Gives Vial a state object to manage globally. You can access it
/// by enabling the `state` feature and calling
/// [`request.state::<YourStruct>()`](struct.Request.html#method.state)
/// in an action.
///
/// The `vial::use_state!()` macro should be called immediately before
/// calling [`vial::run!()`](macro.run.html) in your application.
///
/// It expects one argument: a `Send + Sync + 'static` object you want
/// to share between all requests.
///
/// ```no_run
/// use std::sync::atomic::{AtomicUsize, Ordering};
/// use vial::prelude::*;
///
/// routes! {
///     GET "/" => hello;
///     GET "/count" => count;
/// }
///
/// fn hello(req: Request) -> impl Responder {
///     req.state::<HitCount>().0.fetch_add(1, Ordering::Relaxed);
///     format!("Hits: {}", count(req))
/// }
///
/// fn count(req: Request) -> String {
///     req.state::<HitCount>()
///         .0
///         .load(Ordering::Relaxed)
///         .to_string()
/// }
///
/// #[derive(Default)]
/// struct HitCount(AtomicUsize);
///
/// fn main() {
///     use_state!(HitCount::default());
///     run!().unwrap();
/// }
/// ```
#[cfg(feature = "state")]
#[macro_export]
macro_rules! use_state {
    ($state:expr) => {
        vial::storage::init();
        vial::storage::set($state);
    };
}

/// This is called by `vial::run!`. You probably should leave it be.
#[doc(hidden)]
#[macro_export]
macro_rules! setup {
    () => {
        #[cfg(bundle_assets)]
        #[macro_export]
        macro_rules! vial_bundled_assets {
            () => { include!(concat!(env!("OUT_DIR"), "/bundle.rs")) };
        }
        #[cfg(bundle_assets)]
        vial::include_assets!();
        vial::asset_dir!(@option option_env!("ASSET_DIR"));
    };
}

/// This is called by `vial::setup!`.
#[doc(hidden)]
#[macro_export]
macro_rules! include_assets {
    () => {
        unsafe {
            vial::BUNDLED_ASSETS = Some(vial_bundled_assets!());
        }
    };
}

/// Vial can serve static files out of an asset directory, complete
/// with ETag support so your browser isn't constantly re-downloading.
///
/// To enable this, put all your `.js` and `.css` and other static
/// assets into a directory in the root of your project, then
/// reference them as if the root of your Vial web application was
/// that asset directory. Next call `vial::asset_dir!()` with the path
/// to your asset directory (maybe `assets/`?) before starting your
/// application with [`vial::run!`](macro.run.html):
///
/// If we had a directory structure like this:
///     .
///     ├── README.md
///     ├── assets
///     │   └── img
///     │       ├── banker.png
///     │       └── doctor.png
///     └── src
///         └── main.rs
///
/// We could serve our images like so:
///
/// ```no_run
/// vial::routes! {
///     GET "/" => |_| "
///         <p><img src='/img/doctor.png'/></p>
///         <p><img src='/img/banker.png'/></p>
///     ";
/// }
///
/// fn main() {
///     vial::asset_dir!("assets/");
///     vial::run!().unwrap();
/// }
/// ```
#[macro_export]
macro_rules! asset_dir {
    (@option $opt:expr) => {
        if let Some(dir) = $opt {
            ::vial::asset_dir!(dir);
        }
    };
    ($dir:expr) => {
        unsafe {
            ::vial::ASSET_DIR = Some($dir);
        }
    };
}

/// If you want to bundle your assets into your final binary in
/// release mode, then you need to call `vial::bundle_assets!()` with
/// the path to your asset directory in a `build.rs` file.
///
/// Bundling assets and setting an asset path using
/// [`vial::asset_dir!()`](macro.asset_dir.html) are mutually
/// exclusive - you can't do both, as enabling bundling will set the
/// asset path for you. Therefor if you are making the transition from
/// using-assets-but-not-bundling to using-assets-and-bundling-them,
/// make sure to remove your call to `vial::asset_dir!`.
///
/// To bundle your assets, first add `vial` as a `build-dependency` in
/// your toml file:
///
/// ```toml
/// [build-dependencies]
/// vial-v0.0.12-
/// ```
///
/// Then either create or open your existing `build.rs` file in the
/// root of your project and call `vial::bundle_assets!` with the path
/// to your asset directory:
///
/// ```no_run
/// fn main() {
///     vial::bundle_assets!("assets/").unwrap();
/// }
/// ```
///
/// This will now bundle your assets in `--release` mode and use the
/// disk in debug and test mode. All calls to functions in the
/// [`assets`](assets/index.html) module should work.
#[macro_export]
macro_rules! bundle_assets {
    ($dir:expr) => {
        ::vial::bundle_assets($dir)
    };
}

/// The `vial::routes!` macro, they say, is half of the battle, with
/// the other 50% being a toss up between "knowledge" and the
/// [`vial::run!`](macro.run.html) macro you use to start your app.
///
/// In Vial, routes are defined within the `routes!` macro in this
/// format:
///
/// > `HTTP_METHOD ROUTE_PATTERN => ACTION;`
///
/// The order in which routes are written matters - routes written
/// first will be checked for matches first, meaning you can declare
/// many routes that point to `"/"`, but only the first one defined
/// will ever match.
///
/// ### HTTP Methods
///
/// `HTTP_METHOD` should be an all caps HTTP method. It will get
/// converted into a [Method](enum.Method.html) enum and can be any
/// one of:
///
/// - `GET`
/// - `HEAD`
/// - `POST`
/// - `PUT`
/// - `DELETE`
/// - `PATCH`
/// - `OPTIONS`
/// - `TRACE`
///
/// ### Route Patterns
///
/// `ROUTE_PATTERN` can be an exact match, such as `"/user"` or
/// `"/v2/search.php3"`, or can include a named URL parameter:
///
/// 1. `"/:name"` — This will match anything except paths with `/` or `.`
///    in them.
/// 2. `"/:name.md"` — Use this format to match on a specific file extension.
/// 3. `"/*name"` — This will match everything, including `/` and `.`
///
/// In the three examples above, calling `request.arg("name")` in an
/// Action will return `Some(&str)`.
///
/// Note that you can have multiple parameters in the same route, as
/// long as the wildcard pattern occurs last:
///
/// ```no_run
/// vial::routes! {
///     GET "/:category/:id/*name" => |req| format!(
///         "<p>Category: {}</p>
///         <p>ID: {}</p>
///         <p>Name: {}</p>",
///         req.arg("category").unwrap_or("None"),
///         req.arg("id").unwrap_or("None"),
///         req.arg("name").unwrap_or("None"),
///     );
/// }
///
/// fn main() {
///     vial::run!();
/// }
/// ```
///
/// ### Actions
///
/// Actions are what routes actually route to.
///
/// They are functions or closures take a
/// [Request](struct.Request.html) and return a
/// [Responder](trait.Responder.html) of some kind.
///
/// ```no_run
/// use vial::prelude::*;
///
/// routes! {
///     GET "/info" => |req| format!(
///         "<p>Name: {}</p>", req.query("name").unwrap_or("None")
///     );
///     GET "/" => index;
/// }
///
/// fn index(req: Request) -> impl Responder {
///     "<form method='GET'>
///         <p>Enter your name: <input type='text' name='name'/></p>
///         <input type='submit'/>
///     </form>"
/// }
///
/// fn main() {
///     run!();
/// }
/// ```
///
/// Returning `impl Responder` is easy -
/// [Responder](trait.Responder.html) is a Vial trait that defines a
/// single conversion method that returns a
/// [Response](struct.Response.html):
///
/// ```rust
/// # use vial::Response;
/// pub trait Responder {
///     fn to_response(self) -> Response;
/// }
/// ```
///
/// These types implement `Responder` by default:
///
/// - `&str`
/// - `String`
/// - `usize` - Empty response with this number as the status code.
/// - `Option<impl Responder>` - 404 on `None`
/// - `Result<impl Responder, Error>` - 500 on Error
///
#[macro_export]
macro_rules! routes {
    (
        $(#![filter($($all_filter:ident),+)])*

        $(
            $(#[filter($($action_filter:ident),+)])*
            $method:ident $path:expr => $body:expr;)*
        ) => {
        fn vial_check_method() {
            #![allow(non_snake_case)]
            fn GET() {}
            fn POST() {}
            fn PUT() {}
            fn DELETE() {}
            fn UPDATE() {}
            fn PATCH() {}
            $($method();)*
        }

        fn vial_filter(req: &mut ::vial::Request) -> Option<::vial::Response> {
            $($({
                if let Some(res) = $all_filter(req) {
                    return Some(res);
                }
            })+)*

            None
        }

        pub fn vial_add_to_router(router: &mut ::vial::Router) {
            $( router.insert(::vial::Method::$method, $path, |mut req| {
                use ::vial::{Request, Response, Responder};

                let b: fn(::vial::Request) -> _ = $body;
                let mut res = vial_filter(&mut req);

                $($({
                    if res.is_none() {
                        res = $action_filter(&mut req);
                    }
                })+)*

                res.unwrap_or_else(|| b(req.into()).to_response())
            }); )*
        }
    };
}