Ejemplo n.º 1
0
 public void Register(Type type, ControllerAttribute ca, Action <EventActionRegistingArgs> callback = null)
 {
     try
     {
         EventControllerInstanceArgs e = new EventControllerInstanceArgs();
         e.Type = type;
         OnControllerInstance(e);
         if (e.Controller == null)
         {
             Register(Server.Options, type, Activator.CreateInstance(type), ca.BaseUrl, Server, ca, callback);
         }
         else
         {
             Register(Server.Options, type, e.Controller, ca.BaseUrl, Server, ca, callback);
         }
     }
     catch (Exception e_)
     {
         if (Server.EnableLog(EventArgs.LogType.Error))
         {
             string msg = $"{type} controller register error {e_.Message} {e_.StackTrace}";
             Server.Log(EventArgs.LogType.Error, msg);
         }
     }
 }
Ejemplo n.º 2
0
 public void Register(HttpConfig config, HttpApiServer server, params Assembly[] assemblies)
 {
     foreach (Assembly item in assemblies)
     {
         Type[] types = item.GetTypes();
         foreach (Type type in types)
         {
             ControllerAttribute ca = type.GetCustomAttribute <ControllerAttribute>(false);
             if (ca != null)
             {
                 EventControllerInstanceArgs e = new EventControllerInstanceArgs();
                 e.Type = type;
                 OnControllerInstance(e);
                 if (e.Controller == null)
                 {
                     Register(config, type, Activator.CreateInstance(type), ca.BaseUrl, server, ca);
                 }
                 else
                 {
                     Register(config, type, e.Controller, ca.BaseUrl, server, ca);
                 }
             }
         }
     }
 }
Ejemplo n.º 3
0
        public void Register(HttpConfig config, HttpApiServer server, object controller)
        {
            Type type = controller.GetType();
            ControllerAttribute ca = type.GetCustomAttribute <ControllerAttribute>(false);

            if (ca != null)
            {
                Register(config, type, controller, ca.BaseUrl, server);
            }
        }
Ejemplo n.º 4
0
        public void Register(object controller)
        {
            Type type = controller.GetType();
            ControllerAttribute ca = type.GetCustomAttribute <ControllerAttribute>(false);

            if (ca != null)
            {
                Register(this.Server.Options, type, controller, ca.BaseUrl, this.Server, ca);
            }
        }
Ejemplo n.º 5
0
 public void Register(HttpConfig config, HttpApiServer server, params Assembly[] assemblies)
 {
     foreach (Assembly item in assemblies)
     {
         foreach (Type type in item.GetTypes())
         {
             ControllerAttribute ca = type.GetCustomAttribute <ControllerAttribute>(false);
             if (ca != null)
             {
                 Register(config, type, Activator.CreateInstance(type), ca.BaseUrl, server);
             }
         }
     }
 }
 public void Register(HttpConfig config, params Assembly[] assemblies)
 {
     foreach (Assembly item in assemblies)
     {
         foreach (Type type in item.GetTypes())
         {
             ControllerAttribute ca = type.GetCustomAttribute <ControllerAttribute>(false);
             if (ca != null)
             {
                 Register(config, type, ca.BaseUrl);
             }
         }
     }
 }
Ejemplo n.º 7
0
 public void Register(params Assembly[] assemblies)
 {
     AssembliesLoading?.Invoke(assemblies);
     foreach (Assembly item in assemblies)
     {
         Type[] types = item.GetTypes();
         foreach (Type type in types)
         {
             PMapper mapper = type.GetCustomAttribute <PMapper>(false);
             if (mapper != null)
             {
                 RegisterParameterBinder(mapper.ParameterType, type);
             }
         }
     }
     foreach (Assembly item in assemblies)
     {
         Type[] types = item.GetTypes();
         foreach (Type type in types)
         {
             ControllerAttribute ca = type.GetCustomAttribute <ControllerAttribute>(false);
             if (ca != null)
             {
                 try
                 {
                     EventControllerInstanceArgs e = new EventControllerInstanceArgs();
                     e.Type = type;
                     OnControllerInstance(e);
                     if (e.Controller == null)
                     {
                         Register(Server.Options, type, Activator.CreateInstance(type), ca.BaseUrl, Server, ca, null);
                     }
                     else
                     {
                         Register(Server.Options, type, e.Controller, ca.BaseUrl, Server, ca, null);
                     }
                 }
                 catch (Exception e_)
                 {
                     if (Server.EnableLog(EventArgs.LogType.Error))
                     {
                         string msg = $"{type} controller register error {e_.Message} {e_.StackTrace}";
                         Server.Log(EventArgs.LogType.Error, msg);
                     }
                 }
             }
         }
     }
 }
Ejemplo n.º 8
0
        private void Register(HttpOptions config, Type controllerType, object controller, string rooturl, HttpApiServer server, ControllerAttribute ca)
        {
            DataConvertAttribute controllerDataConvert      = controllerType.GetCustomAttribute <DataConvertAttribute>(false);
            OptionsAttribute     controllerOptionsAttribute = controllerType.GetCustomAttribute <OptionsAttribute>(false);

            if (string.IsNullOrEmpty(rooturl))
            {
                rooturl = "/";
            }
            else
            {
                if (rooturl[0] != '/')
                {
                    rooturl = "/" + rooturl;
                }
                if (rooturl[rooturl.Length - 1] != '/')
                {
                    rooturl += "/";
                }
            }
            RequestMaxRPS          control_maxRPS = controllerType.GetCustomAttribute <RequestMaxRPS>();
            List <FilterAttribute> filters        = new List <FilterAttribute>();

            filters.AddRange(config.Filters);
            IEnumerable <FilterAttribute> fas = controllerType.GetCustomAttributes <FilterAttribute>(false);

            filters.AddRange(fas);
            IEnumerable <SkipFilterAttribute> skipfilters = controllerType.GetCustomAttributes <SkipFilterAttribute>(false);

            foreach (SkipFilterAttribute item in skipfilters)
            {
                RemoveFilter(filters, item.Types);
            }
            object obj = controller;

            if (obj is IController)
            {
                string path = System.IO.Path.GetDirectoryName(controllerType.Assembly.Location) + System.IO.Path.DirectorySeparatorChar;
                ((IController)obj).Init(server, path);
                server.Log(EventArgs.LogType.Info, $"init {controllerType} controller path {path}");
            }
            foreach (MethodInfo mi in controllerType.GetMethods(BindingFlags.Instance | BindingFlags.Public))
            {
                if (string.Compare("Equals", mi.Name, true) == 0 ||
                    string.Compare("GetHashCode", mi.Name, true) == 0 ||
                    string.Compare("GetType", mi.Name, true) == 0 ||
                    string.Compare("ToString", mi.Name, true) == 0 || mi.Name.IndexOf("set_") >= 0 ||
                    mi.Name.IndexOf("get_") >= 0)
                {
                    continue;
                }
                if (mi.GetCustomAttribute <NotActionAttribute>(false) != null)
                {
                    continue;
                }
                bool          noconvert = false;
                RequestMaxRPS maxRPS    = mi.GetCustomAttribute <RequestMaxRPS>();
                if (maxRPS == null)
                {
                    maxRPS = control_maxRPS;
                }
                DataConvertAttribute actionConvert          = mi.GetCustomAttribute <DataConvertAttribute>();
                OptionsAttribute     methodOptionsAttribute = mi.GetCustomAttribute <OptionsAttribute>();
                if (mi.GetCustomAttribute <NoDataConvertAttribute>(false) != null)
                {
                    noconvert     = true;
                    actionConvert = null;
                }
                else
                {
                    if (actionConvert == null)
                    {
                        actionConvert = controllerDataConvert;
                    }
                }
                string       sourceUrl = rooturl + mi.Name;
                string       url       = sourceUrl;
                string       method    = HttpParse.GET_TAG;
                string       route     = null;
                GetAttribute get       = mi.GetCustomAttribute <GetAttribute>(false);
                if (get != null)
                {
                    method = HttpParse.GET_TAG;
                    route  = get.Route;
                }
                PostAttribute post = mi.GetCustomAttribute <PostAttribute>(false);
                if (post != null)
                {
                    method = HttpParse.POST_TAG;
                    route  = post.Route;
                }
                DelAttribute del = mi.GetCustomAttribute <DelAttribute>(false);
                if (del != null)
                {
                    method = HttpParse.DELETE_TAG;
                    route  = del.Route;
                }
                PutAttribute put = mi.GetCustomAttribute <PutAttribute>(false);
                if (put != null)
                {
                    method = HttpParse.PUT_TAG;
                    route  = put.Route;
                }

                if (server.Options.UrlIgnoreCase)
                {
                    url = sourceUrl.ToLower();
                }
                RouteTemplateAttribute ra = null;
                if (!string.IsNullOrEmpty(route))
                {
                    ra = new RouteTemplateAttribute(route);
                    string reurl = ra.Analysis(url);
                    if (reurl != null)
                    {
                        server.UrlRewrite.Add(reurl, url);
                    }
                }
                ActionHandler handler = GetAction(url);
                if (handler != null)
                {
                    server.Log(EventArgs.LogType.Warring, "{0} already exists!replaced with {1}.{2}!", url, controllerType.Name,
                               mi.Name);
                }

                handler = new ActionHandler(obj, mi);
                if (mi.ReturnType == typeof(Task) || mi.ReturnType.BaseType == typeof(Task))
                {
                    handler.Async = true;
                    PropertyInfo pi = mi.ReturnType.GetProperty("Result", BindingFlags.Public | BindingFlags.Instance);
                    if (pi != null)
                    {
                        handler.PropertyHandler = new PropertyHandler(pi);
                    }
                }
                handler.Path = rooturl;
                if (methodOptionsAttribute == null)
                {
                    handler.OptionsAttribute = controllerOptionsAttribute;
                }
                else
                {
                    handler.OptionsAttribute = methodOptionsAttribute;
                }
                handler.NoConvert      = noconvert;
                handler.SingleInstance = ca.SingleInstance;
                handler.DataConvert    = actionConvert;
                handler.Route          = ra;
                handler.Method         = method;
                handler.SourceUrl      = sourceUrl;
                handler.Filters.AddRange(filters);
                fas = mi.GetCustomAttributes <FilterAttribute>(false);
                handler.Filters.AddRange(fas);
                handler.Url = url;
                if (maxRPS != null)
                {
                    handler.MaxRPS = maxRPS.Value;
                }
                int rpsSetting = server.Options.GetActionMaxrps(handler.SourceUrl);
                if (rpsSetting > 0)
                {
                    handler.MaxRPS = rpsSetting;
                }
                skipfilters = mi.GetCustomAttributes <SkipFilterAttribute>(false);
                foreach (SkipFilterAttribute item in skipfilters)
                {
                    RemoveFilter(handler.Filters, item.Types);
                }
                AddHandlers(url, handler);
                server.Log(EventArgs.LogType.Info, $"register { controllerType.Name}.{mi.Name} to [{handler.Method}:{url}]");
            }
        }
Ejemplo n.º 9
0
 public void Register(object controller, ControllerAttribute ca, Action <EventActionRegistingArgs> callback = null)
 {
     Register(Server.Options, controller.GetType(), controller, ca.BaseUrl, Server, ca, callback);
 }
Ejemplo n.º 10
0
        private void Register(HttpOptions config, Type controllerType, object controller, string rooturl, HttpApiServer server, ControllerAttribute ca,
                              Action <EventActionRegistingArgs> callback)
        {
            DataConvertAttribute controllerDataConvert      = controllerType.GetCustomAttribute <DataConvertAttribute>(false);
            OptionsAttribute     controllerOptionsAttribute = controllerType.GetCustomAttribute <OptionsAttribute>(false);

            if (string.IsNullOrEmpty(rooturl))
            {
                rooturl = "/";
            }
            else
            {
                if (rooturl[0] != '/')
                {
                    rooturl = "/" + rooturl;
                }
                if (rooturl[rooturl.Length - 1] != '/')
                {
                    rooturl += "/";
                }
            }
            RequestMaxRPS          control_maxRPS = controllerType.GetCustomAttribute <RequestMaxRPS>();
            List <FilterAttribute> filters        = new List <FilterAttribute>();

            if (!ca.SkipPublicFilter)
            {
                filters.AddRange(config.Filters);
            }
            IEnumerable <FilterAttribute> fas = controllerType.GetCustomAttributes <FilterAttribute>(false);

            filters.AddRange(fas);
            LoadBaseFilter(controllerType, filters);
            IEnumerable <SkipFilterAttribute> skipfilters = controllerType.GetCustomAttributes <SkipFilterAttribute>(false);

            foreach (SkipFilterAttribute item in skipfilters)
            {
                RemoveFilter(filters, item.Types);
            }
            object obj = controller;

            if (obj is IController)
            {
                string path = System.IO.Path.GetDirectoryName(controllerType.Assembly.Location) + System.IO.Path.DirectorySeparatorChar;
                ((IController)obj).Init(server, path);
                if (server.EnableLog(EventArgs.LogType.Info))
                {
                    server.Log(EventArgs.LogType.Info, $"init {controllerType} controller path {path}");
                }
            }
            foreach (MethodInfo mi in controllerType.GetMethods(BindingFlags.Instance | BindingFlags.Public))
            {
                if (string.Compare("Equals", mi.Name, true) == 0 ||
                    string.Compare("GetHashCode", mi.Name, true) == 0 ||
                    string.Compare("GetType", mi.Name, true) == 0 ||
                    string.Compare("ToString", mi.Name, true) == 0 || mi.Name.IndexOf("set_") >= 0 ||
                    mi.Name.IndexOf("get_") >= 0)
                {
                    continue;
                }
                if (mi.GetCustomAttribute <NotActionAttribute>(false) != null)
                {
                    continue;
                }
                bool          noconvert = false;
                RequestMaxRPS maxRPS    = mi.GetCustomAttribute <RequestMaxRPS>();
                if (maxRPS == null)
                {
                    maxRPS = control_maxRPS;
                }
                DataConvertAttribute actionConvert          = mi.GetCustomAttribute <DataConvertAttribute>();
                OptionsAttribute     methodOptionsAttribute = mi.GetCustomAttribute <OptionsAttribute>();
                if (mi.GetCustomAttribute <NoDataConvertAttribute>(false) != null)
                {
                    noconvert     = true;
                    actionConvert = null;
                }
                else
                {
                    if (actionConvert == null)
                    {
                        actionConvert = controllerDataConvert;
                    }
                }
                string       sourceUrl = rooturl + mi.Name;
                string       url       = sourceUrl;
                string       method    = HttpParse.GET_TAG + "/" + HttpParse.POST_TAG;
                string       route     = null;
                GetAttribute get       = mi.GetCustomAttribute <GetAttribute>(false);
                if (get != null)
                {
                    method = HttpParse.GET_TAG;
                    route  = get.Route;
                }
                PostAttribute post = mi.GetCustomAttribute <PostAttribute>(false);
                if (post != null)
                {
                    method = HttpParse.POST_TAG;
                    route  = post.Route;
                }
                DelAttribute del = mi.GetCustomAttribute <DelAttribute>(false);
                if (del != null)
                {
                    method = HttpParse.DELETE_TAG;
                    route  = del.Route;
                }
                PutAttribute put = mi.GetCustomAttribute <PutAttribute>(false);
                if (put != null)
                {
                    method = HttpParse.PUT_TAG;
                    route  = put.Route;
                }

                //if (server.Options.UrlIgnoreCase)
                //{
                //    url = sourceUrl.ToLower();
                //}
                RouteTemplateAttribute ra = null;
                if (!string.IsNullOrEmpty(route))
                {
                    ra = new RouteTemplateAttribute(route);
                    string reurl;
                    if (route[0] == '/')
                    {
                        reurl = ra.Analysis(null);
                    }
                    else if (route[0] == '{')
                    {
                        reurl = ra.Analysis(url + "/");
                    }
                    else
                    {
                        reurl = ra.Analysis(route.IndexOf('/', 0) > 0 ? rooturl : url + "/");
                    }
                    if (reurl == null)
                    {
                        if (route[0] == '/')
                        {
                            reurl = route;
                        }
                        else
                        {
                            reurl = rooturl + route;
                        }
                    }
                    server.UrlRewrite.Add(null, reurl, url);
                }
                ActionHandler handler = GetAction(url);
                if (handler != null)
                {
                    if (server.EnableLog(EventArgs.LogType.Error))
                    {
                        server.Log(EventArgs.LogType.Error, $"{url} already exists! replaced with {controllerType.Name}.{mi.Name}!");
                    }
                }
                handler          = new ActionHandler(obj, mi, this.Server);
                handler.AuthMark = controllerType.GetCustomAttribute <AuthMarkAttribute>(false);
                var authmark = mi.GetCustomAttribute <AuthMarkAttribute>(false);
                if (authmark != null)
                {
                    handler.AuthMark = authmark;
                }
                if (handler.AuthMark == null)
                {
                    handler.AuthMark = new AuthMarkAttribute(AuthMarkType.None);
                }
                if (mi.ReturnType == typeof(Task) || mi.ReturnType.BaseType == typeof(Task))
                {
                    handler.Async = true;
                    PropertyInfo pi = mi.ReturnType.GetProperty("Result", BindingFlags.Public | BindingFlags.Instance);
                    if (pi != null)
                    {
                        handler.PropertyHandler = new PropertyHandler(pi);
                    }
                }
                handler.Path = rooturl;
                if (methodOptionsAttribute == null)
                {
                    handler.OptionsAttribute = controllerOptionsAttribute;
                }
                else
                {
                    handler.OptionsAttribute = methodOptionsAttribute;
                }
                if (handler.OptionsAttribute == null && !ca.SkipPublicFilter)
                {
                    handler.OptionsAttribute = handler.HttpApiServer.Options.CrossDomain;
                }
                handler.ThreadQueue = controllerType.GetCustomAttribute <ThreadQueueAttribute>(false);
                var queue = mi.GetCustomAttribute <ThreadQueueAttribute>(false);
                if (queue != null)
                {
                    handler.ThreadQueue = queue;
                }
                handler.NoConvert     = noconvert;
                handler.InstanceType  = ca.InstanceType;
                handler.DataConverter = actionConvert;
                handler.Route         = ra;
                handler.Method        = method;
                handler.SourceUrl     = sourceUrl;
                handler.Filters.AddRange(filters);
                fas = mi.GetCustomAttributes <FilterAttribute>(false);
                handler.Filters.AddRange(fas);
                handler.Url = url;
                if (maxRPS != null)
                {
                    handler.MaxRPS = maxRPS.Value;
                }
                int rpsSetting = server.Options.GetActionMaxrps(handler.SourceUrl);
                if (rpsSetting > 0)
                {
                    handler.MaxRPS = rpsSetting;
                }
                skipfilters = mi.GetCustomAttributes <SkipFilterAttribute>(false);
                foreach (SkipFilterAttribute item in skipfilters)
                {
                    RemoveFilter(handler.Filters, item.Types);
                }
                EventActionRegistingArgs registing = new EventActionRegistingArgs();
                registing.Url     = url;
                registing.Handler = handler;
                registing.Cancel  = false;
                registing.Server  = this.Server;
                callback?.Invoke(registing);
                if (!registing.Cancel)
                {
                    Server.OnActionRegisting(registing);
                }

                if (!registing.Cancel)
                {
                    AddHandlers(url, handler);
                    server.ActionSettings(handler);
                    if (server.EnableLog(EventArgs.LogType.Info))
                    {
                        server.Log(EventArgs.LogType.Info, $"register { controllerType.Name}.{mi.Name} to [{handler.Method}:{url}]");
                    }
                }
                else
                {
                    if (server.EnableLog(EventArgs.LogType.Info))
                    {
                        server.Log(EventArgs.LogType.Info, $"register { controllerType.Name}.{mi.Name} cancel ");
                    }
                }
            }
        }
Ejemplo n.º 11
0
        private void Register(HttpConfig config, Type controllerType, object controller, string rooturl, HttpApiServer server, ControllerAttribute ca)
        {
            DataConvertAttribute controllerDataConvert = controllerType.GetCustomAttribute <DataConvertAttribute>(false);

            if (string.IsNullOrEmpty(rooturl))
            {
                rooturl = "/";
            }
            else
            {
                if (rooturl[0] != '/')
                {
                    rooturl = "/" + rooturl;
                }
                if (rooturl[rooturl.Length - 1] != '/')
                {
                    rooturl += "/";
                }
            }
            List <FilterAttribute> filters = new List <FilterAttribute>();

            filters.AddRange(config.Filters);
            IEnumerable <FilterAttribute> fas = controllerType.GetCustomAttributes <FilterAttribute>(false);

            filters.AddRange(fas);
            IEnumerable <SkipFilterAttribute> skipfilters = controllerType.GetCustomAttributes <SkipFilterAttribute>(false);

            foreach (SkipFilterAttribute item in skipfilters)
            {
                RemoveFilter(filters, item.Types);
            }
            object obj = controller;

            if (obj is IController)
            {
                ((IController)obj).Init(server);
            }
            foreach (MethodInfo mi in controllerType.GetMethods(BindingFlags.Instance | BindingFlags.Public))
            {
                if (string.Compare("Equals", mi.Name, true) == 0 ||
                    string.Compare("GetHashCode", mi.Name, true) == 0 ||
                    string.Compare("GetType", mi.Name, true) == 0 ||
                    string.Compare("ToString", mi.Name, true) == 0 || mi.Name.IndexOf("set_") >= 0 ||
                    mi.Name.IndexOf("get_") >= 0)
                {
                    continue;
                }
                if (mi.GetCustomAttribute <NotActionAttribute>(false) != null)
                {
                    continue;
                }
                DataConvertAttribute actionConvert = mi.GetCustomAttribute <DataConvertAttribute>();
                if (mi.GetCustomAttribute <NoDataConvertAttribute>(false) != null)
                {
                    actionConvert = null;
                }
                else
                {
                    if (actionConvert == null)
                    {
                        actionConvert = controllerDataConvert;
                    }
                    if (actionConvert == null)
                    {
                        actionConvert = new JsonDataConvertAttribute();
                    }
                }

                string       sourceUrl = rooturl + mi.Name;
                string       url       = sourceUrl;
                string       method    = HttpParse.GET_TAG;
                string       route     = null;
                GetAttribute get       = mi.GetCustomAttribute <GetAttribute>(false);
                if (get != null)
                {
                    method = HttpParse.GET_TAG;
                    route  = get.Route;
                }
                PostAttribute post = mi.GetCustomAttribute <PostAttribute>(false);
                if (post != null)
                {
                    method = HttpParse.POST_TAG;
                    route  = post.Route;
                }
                DelAttribute del = mi.GetCustomAttribute <DelAttribute>(false);
                if (del != null)
                {
                    method = HttpParse.DELETE_TAG;
                    route  = del.Route;
                }
                PutAttribute put = mi.GetCustomAttribute <PutAttribute>(false);
                if (put != null)
                {
                    method = HttpParse.PUT_TAG;
                    route  = put.Route;
                }

                if (server.ServerConfig.UrlIgnoreCase)
                {
                    url = sourceUrl.ToLower();
                }
                RouteTemplateAttribute ra = null;
                if (!string.IsNullOrEmpty(route))
                {
                    ra = new RouteTemplateAttribute(route);
                    string reurl = ra.Analysis(url);
                    if (reurl != null)
                    {
                        server.UrlRewrite.Add(reurl, url);
                    }
                }
                ActionHandler handler = GetAction(url);
                if (handler != null)
                {
                    server.Log(EventArgs.LogType.Error, "{0} already exists!duplicate definition {1}.{2}!", url, controllerType.Name,
                               mi.Name);
                    continue;
                }
                handler = new ActionHandler(obj, mi);
                handler.SingleInstance = ca.SingleInstance;
                handler.DataConvert    = actionConvert;
                handler.Route          = ra;
                handler.Method         = method;
                handler.SourceUrl      = sourceUrl;
                handler.Filters.AddRange(filters);
                fas = mi.GetCustomAttributes <FilterAttribute>(false);
                handler.Filters.AddRange(fas);
                skipfilters = mi.GetCustomAttributes <SkipFilterAttribute>(false);
                foreach (SkipFilterAttribute item in skipfilters)
                {
                    RemoveFilter(handler.Filters, item.Types);
                }
                mMethods[url] = handler;
                server.Log(EventArgs.LogType.Info, "register {0}.{1} to {2}", controllerType.Name, mi.Name, url);
            }
        }