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
mod embed;
mod utils;

use embed::impl_embed;
use proc_macro2::{Span, TokenStream};
use quote::quote;
use syn::{
    parse_macro_input, spanned::Spanned, DeriveInput, Error, FnArg, Meta, NestedMeta, Pat, Path,
    Result,
};
use utils::{FromLit, LitWrap};

#[proc_macro_derive(SPAServer, attributes(spa_server))]
pub fn derive_spa_server(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
    let input = parse_macro_input!(input as DeriveInput);
    expand(input)
        .unwrap_or_else(|e| e.into_compile_error())
        .into()
}

fn get_name_value<'a, T, I, S>(metas: I, key: S) -> Option<T>
where
    T: FromLit,
    I: Iterator<Item = &'a Meta>,
    S: AsRef<str>,
{
    for m in metas {
        if let Meta::NameValue(nv) = m {
            if let Some(ident) = nv.path.get_ident() {
                if ident == key.as_ref() {
                    let lw = LitWrap { inner: &nv.lit };
                    if let Ok(r) = lw.parse() {
                        return Some(r);
                    }
                }
            }
        }
    }

    None
}

fn get_path<'a>(metas: impl Iterator<Item = &'a Meta>, key: impl AsRef<str>) -> bool {
    for m in metas {
        if let Meta::Path(p) = m {
            if let Some(ident) = p.get_ident() {
                if ident == key.as_ref() {
                    return true;
                }
            }
        }
    }

    false
}

fn expand(input: DeriveInput) -> Result<TokenStream> {
    let name = &input.ident;
    let attrs = &input.attrs;
    let mut opt = Options::default();
    for attr in attrs {
        if let Meta::List(l) = attr.parse_meta()? {
            if let Some(id) = l.path.get_ident() {
                if id != "spa_server" {
                    return Err(Error::new(l.span(), "only support attribute spa_server"));
                }
            }

            let metas = l.nested.iter().filter_map(|x| {
                if let NestedMeta::Meta(m) = x {
                    Some(m)
                } else {
                    None
                }
            });

            opt.static_files = get_name_value(metas.clone(), "static_files").ok_or(Error::new(
                Span::call_site(),
                "must set static files path in attribute",
            ))?;

            opt.cors = get_path(metas.clone(), "cors");
            if !opt.cors {
                if let Some(cors) = get_name_value(metas.clone(), "cors") {
                    opt.cors = cors;
                }
            }

            for m in metas {
                match m {
                    Meta::List(l) => {
                        if let Some(id) = l.path.get_ident() {
                            if id == "apis" {
                                for api in &l.nested {
                                    if let NestedMeta::Meta(meta) = api {
                                        if let Meta::List(pl) = meta {
                                            if let Some(iid) = pl.path.get_ident() {
                                                if iid == "api" {
                                                    let mut api_path = Vec::new();
                                                    let mut prefix = None;
                                                    for ppl in &pl.nested {
                                                        if let NestedMeta::Meta(mm) = ppl {
                                                            match mm {
                                                                Meta::Path(p) => {
                                                                    api_path.push(p.clone())
                                                                }
                                                                Meta::NameValue(nv) => {
                                                                    if let Some(iiid) =
                                                                        nv.path.get_ident()
                                                                    {
                                                                        if iiid == "prefix" {
                                                                            let lw = LitWrap {
                                                                                inner: &nv.lit,
                                                                            };
                                                                            if let Ok(r) =
                                                                                lw.parse::<String>()
                                                                            {
                                                                                prefix = Some(r);
                                                                            }
                                                                        }
                                                                    }
                                                                }
                                                                _ => {}
                                                            }
                                                        }
                                                    }

                                                    opt.apis.push(Api {
                                                        path: api_path,
                                                        prefix,
                                                    });
                                                }
                                            }
                                        }
                                    }
                                }
                            } else if id == "identity" {
                                let mut identity = Identity::default();
                                for nm in &l.nested {
                                    if let NestedMeta::Meta(meta) = nm {
                                        if let Meta::NameValue(nv) = meta {
                                            if let Some(iid) = nv.path.get_ident() {
                                                if iid == "name" {
                                                    let lit = LitWrap { inner: &nv.lit };
                                                    if let Ok(name) = lit.parse() {
                                                        identity.name = name;
                                                    }
                                                } else if iid == "age" {
                                                    let lit = LitWrap { inner: &nv.lit };
                                                    if let Ok(age) = lit.parse() {
                                                        identity.age = age;
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }

                                if !identity.name.is_empty() && identity.age != 0 {
                                    opt.identity = Some(identity);
                                }
                            }
                        }
                    }
                    _ => {}
                }
            }
        }
    }

    let mut services = Vec::new();
    for api in opt.apis {
        let api_list = api.path;
        match api.prefix {
            Some(p) => {
                services.push(quote! {
                    .service(
                        web::scope(#p)
                        #(.service(#api_list))*
                        .app_data(data.clone())
                    )
                });
            }
            None => {
                services.push(quote! {
                    #(.service(#api_list))*
                    .app_data(data.clone())
                });
            }
        }
    }

    let cors = if opt.cors {
        quote! { .wrap(spa_server::re_export::Cors::permissive()) }
    } else {
        TokenStream::new()
    };

    let identity = if let Some(id) = opt.identity {
        let name = id.name;
        let age = id.age;
        quote! {
            .wrap(spa_server::re_export::IdentityService::new(
                spa_server::re_export::CookieIdentityPolicy::new(&[0; 32])
                    .name(#name)
                    .max_age_time(spa_server::Duration::minutes(#age))
                    .http_only(true)
                    .secure(false)
            ))
        }
    } else {
        TokenStream::new()
    };

    let embed_tokens = impl_embed(name, &opt.static_files, None);

    Ok(quote! {
        use spa_server::re_export::{
            App, HttpServer, rt::System, web, Files
        };
        use spa_server::{Embed, Filenames};
        use std::borrow::Cow;

        impl #name {
            pub async fn run(self, port: u16) -> Result<(), Box<dyn std::error::Error>> {
                let root_path = spa_server::release_asset::<#name>()?;
                let data = web::Data::new(self);

                HttpServer::new(move || {
                    App::new()
                        #identity
                        #cors
                        #(#services)*
                        .data(root_path.clone())
                        .service(spa_server::index)
                        .service(Files::new("/", root_path.clone()).index_file("index.html"))
                })
                .bind(format!("0.0.0.0:{}", port))?
                .run()
                .await?;

                Ok(())
            }
        }

        #embed_tokens
    })
}

#[derive(Default)]
struct Options {
    apis: Vec<Api>,
    static_files: String,
    cors: bool,
    identity: Option<Identity>,
}

#[derive(Default)]
struct Api {
    path: Vec<Path>,
    prefix: Option<String>,
}

#[allow(dead_code)]
#[derive(Default)]
struct Identity {
    name: String,
    age: i64,
}

#[proc_macro_attribute]
pub fn main(_: proc_macro::TokenStream, item: proc_macro::TokenStream) -> proc_macro::TokenStream {
    let mut input = syn::parse_macro_input!(item as syn::ItemFn);
    let attrs = &input.attrs;
    let vis = &input.vis;
    let sig = &mut input.sig;
    let body = &input.block;

    if sig.asyncness.is_none() {
        return Error::new_spanned(sig.fn_token, "only async fn is supported")
            .to_compile_error()
            .into();
    }

    sig.asyncness = None;

    (quote! {
        #(#attrs)*
        #vis #sig {
            spa_server::re_export::rt::System::new()
                .block_on(async move { #body })
        }
    })
    .into()
}

mod route;

macro_rules! method_macro {
    (
        $($variant:ident, $method:ident,)+
    ) => {
        $(
            #[proc_macro_attribute]
            pub fn $method(args: proc_macro::TokenStream, input: proc_macro::TokenStream) -> proc_macro::TokenStream {
                route::with_method(Some(route::MethodType::$variant), args, input)
            }
        )+
    };
}

method_macro! {
    Get,       get,
    Post,      post,
    Put,       put,
    Delete,    delete,
    Head,      head,
    Connect,   connect,
    Options,   options,
    Trace,     trace,
    Patch,     patch,
}

#[proc_macro_attribute]
pub fn error_to_json(
    _: proc_macro::TokenStream,
    item: proc_macro::TokenStream,
) -> proc_macro::TokenStream {
    let mut input = syn::parse_macro_input!(item as syn::ItemFn);
    let attrs = &input.attrs;
    let vis = &input.vis;
    let sig = &mut input.sig;
    let body = &input.block;

    let mut sig_impl = sig.clone();
    sig_impl.ident = syn::Ident::new(&format!("_{}_impl", sig.ident), sig.span());
    let sig_impl_ident = &sig_impl.ident;

    let mut args = Vec::new();
    for i in &sig.inputs {
        if let FnArg::Typed(p) = i {
            if let Pat::Ident(id) = &*p.pat {
                args.push(id.ident.clone());
            }
        }
    }

    (quote! {
        #[allow(unused_mut)]
        #(#attrs)*
        #vis #sig {
            Ok(match #sig_impl_ident(#(#args),*).await {
                Ok(a) => a,
                Err(e) => spa_server::re_export::HttpResponse::Ok().json(spa_server::quick_err(format!("{:?}", e)))
            })
        }

        #vis #sig_impl {
            #body
        }
    })
    .into()
}