Пример #1
0
 public static ExpressApplication Post <T, TResult>(this ExpressApplication app, string url, Func <T, TResult> func)
 {
     return(app.Post(url, req =>
     {
         var arg = req.ParseJson <T>();
         var result = func(arg);
         Send(req, result);
     }));
 }
Пример #2
0
        private static void MethodHandler(this ExpressApplication app, string urlPrefix,
                                          Func <object> serviceInstance, MethodInfo method, WebMethodAttribute meta)
        {
            // TODO support WebMethodAttribute options (caching, etc)
            var invoke = DynamicMethods.CompileMethod(method.DeclaringType, method);

            var parameters = method.GetParameters();

            if (parameters.Length == 0)
            {
                app.Get(
                    Combine(urlPrefix, method.Name),
                    req =>
                {
                    req.SetContext();
                    var result = invoke(serviceInstance(), new object[0]);
                    req.Json(result);
                });
            }
            else
            {
                app.Get(
                    Combine(urlPrefix, method.Name),
                    req =>
                {
                    req.SetContext();
                    var args   = ParseQueryArgs(req, parameters);
                    var result = invoke(serviceInstance(), args);
                    req.Json(result);
                });

                app.Post(
                    Combine(urlPrefix, method.Name),
                    req =>
                {
                    req.SetContext();
                    var args   = ParseArgs(req, parameters);
                    var result = invoke(serviceInstance(), args);
                    req.Json(result);
                });
            }
        }
Пример #3
0
        public static ExpressApplication HttpHandler <T>(this ExpressApplication app, string url, string verb)
        {
            if (string.IsNullOrEmpty(verb))
            {
                verb = "*";
            }

            Func <string, bool> hasVerb = v =>
            {
                if (string.Equals(verb, "*"))
                {
                    return(true);
                }
                return(verb.IndexOf(v, StringComparison.OrdinalIgnoreCase) >= 0);
            };

            IHttpHandler handler = null;

            Action <RequestContext> action = req =>
            {
                if (handler == null)
                {
                    handler = (IHttpHandler)Activator.CreateInstance <T>();
                }

                var context = req.HttpContext.Unwrap();
                handler.ProcessRequest(context);
            };

            if (hasVerb("GET"))
            {
                app.Get(url, action);
            }

            if (hasVerb("POST"))
            {
                app.Post(url, action);
            }

            return(app);
        }
Пример #4
0
        private static ExpressApplication RestImpl(this ExpressApplication app,
                                                   string url, Type type, Func <int> nextIndex,
                                                   Func <RequestContext, object> getInstance,
                                                   Func <RequestContext, object> getModel)
        {
            app.Get(url, req => req.Json(getInstance(req)));

            var props = type
                        .GetProperties(BindingFlags.Public | BindingFlags.Instance)
                        .Where(p => FilterProperty(p))
                        .ToList();

            props.ForEach(prop =>
            {
                var propertyUrl = Combine(url, prop.Name);
                var getter      = DynamicMethods.CompileGetter(type, prop);
                var setter      = DynamicMethods.CompileSetter(type, prop);

                Func <RequestContext, object> propertyInstance = req =>
                {
                    var instance = getInstance(req);
                    return(getter(instance));
                };

                app.Get(propertyUrl, req => req.Json(propertyInstance(req)));

                if (setter != null)
                {
                    app.Update(propertyUrl, req =>
                    {
                        req.SetContext();

                        var json       = req.ParseJson();
                        var hasChanges = ChangesDetector(req, getModel);
                        var instance   = getInstance(req);

                        // TODO get default value from metadata
                        var defval = GetDefaultValue(prop.PropertyType);
                        var value  = json.Get("value", defval, prop.PropertyType);

                        setter(instance, value);

                        req.Json(hasChanges() ? ModelPayload(req, getModel) : null);
                    });
                }

                if (IsCollection(prop.PropertyType))
                {
                    RestCollection(app, propertyUrl, prop.PropertyType, nextIndex, propertyInstance, getModel);
                }
                else if (IsObject(prop.PropertyType))
                {
                    RestImpl(app, propertyUrl, prop.PropertyType, nextIndex, propertyInstance, getModel);
                }
            });

            var methods = type
                          .GetMethods(BindingFlags.Public | BindingFlags.Instance)
                          .Where(m => m.GetAttribute <RestAttribute>(true) != null)
                          .ToList();

            methods.ForEach(method =>
            {
                var call = DynamicMethods.CompileMethod(type, method);

                app.Post(url, req =>
                {
                    req.SetContext();

                    var hasChanges = ChangesDetector(req, getModel);

                    var args       = req.ParseArgs(method.GetParameters());
                    var collection = getInstance(req);
                    var result     = call(collection, args);

                    req.Json(hasChanges() ? ModelPayload(req, getModel) : result);
                });
            });

            return(app);
        }