コード例 #1
0
        public JimuServiceDesc Parse(MethodInfo methodInfo)
        {
            JimuServiceDesc desc = new JimuServiceDesc();
            var             descriptorAttributes = methodInfo.GetCustomAttributes <JimuServiceDescAttribute>();

            foreach (var attr in descriptorAttributes)
            {
                attr.Apply(desc);
            }

            if (string.IsNullOrEmpty(desc.Comment))
            {
                var xml = GetXmlComment(methodInfo.DeclaringType);
                var key = XmlCommentsMemberNameHelper.GetMemberNameForMethod(methodInfo);
                if (xml != null && xml.TryGetValue(key, out var node))
                {
                    var summaryNode = node.SelectSingleNode("summary");
                    if (summaryNode != null)
                    {
                        desc.Comment = summaryNode.Value.Trim();
                    }
                }
            }

            desc.ReturnDesc = GetReturnDesc(methodInfo);

            if (string.IsNullOrEmpty(desc.HttpMethod))
            {
                desc.HttpMethod = GetHttpMethod(methodInfo);
            }

            desc.Parameters = JimuHelper.Serialize <string>(GetParameters(methodInfo));

            if (string.IsNullOrEmpty(desc.Id))
            {
                desc.Id = JimuHelper.GenerateServiceId(methodInfo);
            }

            var type          = methodInfo.DeclaringType;
            var routeTemplate = type.GetCustomAttribute <JimuAttribute>();

            if (routeTemplate != null)
            {
                if (string.IsNullOrEmpty(desc.RoutePath))
                {
                    var setPath = string.IsNullOrEmpty(desc.Rest) ? methodInfo.Name : desc.Rest;
                    desc.ServiceClassPath = JimuServiceRoute.ParseClassPath(routeTemplate.GetTemplate(type), type.Name, type.IsInterface);
                    desc.RoutePath        = JimuServiceRoute.ParseRoutePath(desc.HttpMethod, desc.ServiceClassPath, setPath, methodInfo.GetParameters().Select(x => x.Name).ToArray(), type.IsInterface);
                }
            }

            desc.Service        = methodInfo.DeclaringType.FullName;
            desc.ServiceComment = GetServiceComment(methodInfo);
            return(desc);
        }
コード例 #2
0
ファイル: JwtAuthServerModule.cs プロジェクト: yuyu2you/jimu
        public override void DoInit(IContainer container)
        {
            if (_options != null)
            {
                var logger = container.Resolve <ILogger>();
                logger.Info($"[config]use jose.jwt for Auth");

                //while (!container.IsRegistered<IServer>() || !container.IsRegistered<IServiceDiscovery>())
                //{
                //    Thread.Sleep(200);
                //}
                var server = container.Resolve <IServer>();
                server.UseMiddleware <JwtAuthorizationMiddleware>(_options, container);

                if (string.IsNullOrEmpty(_options.TokenEndpointPath))
                {
                    return;
                }
                var discovery  = container.Resolve <IServiceDiscovery>();
                var addr       = new JimuAddress(_options.ServiceInvokeIp, Convert.ToInt32(_options.ServiceInvokePort), _options.Protocol);
                var tokenRoute =
                    new JimuServiceRoute
                {
                    Address = new List <JimuAddress> {
                        addr
                    },
                    ServiceDescriptor = new JimuServiceDesc
                    {
                        Id         = _options.GetServiceId(),
                        Service    = "Token",
                        RoutePath  = JimuServiceRoute.ParseRoutePath("", "", _options.TokenEndpointPath, new[] { "username", "password" }, false),
                        Parameters = JimuHelper.Serialize <string>(new List <JimuServiceParameterDesc> {
                            new JimuServiceParameterDesc
                            {
                                Comment = "username",
                                Format  = "System.String",
                                Name    = "username",
                                Type    = "object"
                            },
                            new JimuServiceParameterDesc
                            {
                                Comment = "password",
                                Format  = "System.String",
                                Name    = "password",
                                Type    = "object"
                            },
                        }),
                        ReturnDesc = JimuHelper.Serialize <string>(new JimuServiceReturnDesc
                        {
                            Comment      = "Token",
                            ReturnType   = "object",
                            ReturnFormat = "{\"access_token\":\"System.String | token\", \"expired_in\":\"System.Int32 | expired timestamp which is the number of seconds between 1970-01-01 and expired datetime\"}"
                        })
                    }
                };
                //discovery.ClearServiceAsync(tokenRoute.First().ServiceDescriptor.Id).Wait();
                ////discovery.SetRoutesAsync(tokenRoute);
                //discovery.AddRouteAsync(tokenRoute).Wait();
                discovery.OnBeforeSetRoutes += (routes) =>
                {
                    routes.Add(tokenRoute);
                };
            }

            base.DoInit(container);
        }
コード例 #3
0
        public override void DoInit(IContainer container)
        {
            if (_options != null)
            {
                var loggerFactory = container.Resolve <ILoggerFactory>();
                var logger        = loggerFactory.Create(this.GetType());
                logger.Info($"[config]use jose.jwt for Auth");

                //while (!container.IsRegistered<IRemoteServiceCaller>() || !container.IsRegistered<IClientServiceDiscovery>())
                //{
                //    Thread.Sleep(100);
                //}

                var caller = container.Resolve <IRemoteServiceCaller>();
                caller.UseMiddleware <JwtAuthorizationMiddleware>(_options);

                if (string.IsNullOrEmpty(_options.TokenEndpointPath))
                {
                    return;
                }
                var discovery  = container.Resolve <IClientServiceDiscovery>();
                var addr       = new JimuAddress(_options.ServiceInvokeIp, Convert.ToInt32(_options.ServiceInvokePort), _options.Protocol);
                var tokenRoute = new List <JimuServiceRoute> {
                    new JimuServiceRoute
                    {
                        Address = new List <JimuAddress> {
                            addr
                        },
                        ServiceDescriptor = new JimuServiceDesc
                        {
                            Id               = _options.GetServiceId(),
                            Service          = "Token",
                            HttpMethod       = "POST",
                            AllowAnonymous   = true,
                            ServiceClassPath = "",
                            RoutePath        = JimuServiceRoute.ParseRoutePath("POST", "", _options.TokenEndpointPath, new[] { "username", "password", "grant_type" }, false),
                            Parameters       = JimuHelper.Serialize <string>(new List <JimuServiceParameterDesc> {
                                new JimuServiceParameterDesc
                                {
                                    Comment = "username",
                                    Name    = "username",
                                    Type    = "System.String"
                                },
                                new JimuServiceParameterDesc
                                {
                                    Comment = "password",
                                    Name    = "password",
                                    Type    = "System.String"
                                },
                                new JimuServiceParameterDesc
                                {
                                    Comment = "grant_type",
                                    Default = "password",
                                    Name    = "grant_type",
                                    Type    = "System.String"
                                },
                            }),
                            ReturnDesc = JimuHelper.Serialize <string>(new JimuServiceReturnDesc {
                                Comment    = "Token",
                                ReturnType = "object",
                                Properties = new List <JimuServiceParameterDesc>
                                {
                                    new JimuServiceParameterDesc {
                                        Comment = "token",
                                        Name    = "access_token",
                                        Type    = "System.String"
                                    },
                                    new JimuServiceParameterDesc {
                                        Comment = "expired timestamp which is the number of seconds between 1970-01-01 and expired datetime",
                                        Name    = "expired_in",
                                        Type    = "System.Int32"
                                    }
                                }
                            })
                        }
                    }
                };
                discovery.AddRoutesGetter(() =>
                {
                    return(Task.FromResult(tokenRoute));
                });
            }

            base.DoInit(container);
        }
コード例 #4
0
        public IServiceEntryContainer AddServices(Type[] types)
        {
            //var serviceTypes = types.Where(x =>
            //{
            //    var typeinfo = x.GetTypeInfo();
            //    return typeinfo.IsInterface && typeinfo.GetCustomAttribute<JimuServiceRouteAttribute>() != null;
            //}).Distinct();

            var serviceTypes = types
                               .Where(x => x.GetMethods().Any(y => y.GetCustomAttribute <JimuServiceAttribute>() != null)).Distinct();

            foreach (var type in serviceTypes)
            {
                var routeTemplate = type.GetCustomAttribute <JimuServiceRouteAttribute>();
                foreach (var methodInfo in type.GetTypeInfo().GetMethods().Where(x => x.GetCustomAttributes <JimuServiceDescAttribute>().Any()))
                {
                    JimuServiceDesc desc = new JimuServiceDesc();
                    var             descriptorAttributes = methodInfo.GetCustomAttributes <JimuServiceDescAttribute>();
                    foreach (var attr in descriptorAttributes)
                    {
                        attr.Apply(desc);
                    }

                    desc.ReturnDesc = GetReturnDesc(methodInfo);

                    if (string.IsNullOrEmpty(desc.HttpMethod))
                    {
                        desc.HttpMethod = GetHttpMethod(methodInfo);
                    }

                    desc.Parameters = _serializer.Serialize <string>(GetParameters(methodInfo));

                    if (string.IsNullOrEmpty(desc.Id))
                    {
                        desc.Id = _serviceIdGenerate.GenerateServiceId(methodInfo);
                    }

                    var fastInvoker = GetHandler(desc.Id, methodInfo);
                    if (routeTemplate != null)
                    {
                        desc.RoutePath = JimuServiceRoute.ParseRoutePath(routeTemplate.RouteTemplate, type.Name,
                                                                         methodInfo.Name, methodInfo.GetParameters(), type.IsInterface);
                    }

                    var service = new JimuServiceEntry
                    {
                        Descriptor = desc,
                        Func       = (paras, payload) =>
                        {
                            var instance   = GetInstance(null, methodInfo.DeclaringType, payload);
                            var parameters = new List <object>();
                            foreach (var para in methodInfo.GetParameters())
                            {
                                paras.TryGetValue(para.Name, out var value);
                                var paraType  = para.ParameterType;
                                var parameter = _typeConvertProvider.Convert(value, paraType);
                                parameters.Add(parameter);
                            }

                            var result = fastInvoker(instance, parameters.ToArray());
                            return(Task.FromResult(result));
                        }
                    };

                    _services.Add(service);
                }
            }

            return(this);
        }
コード例 #5
0
ファイル: JwtAuthClientModule.cs プロジェクト: InvokerLM/jimu
        public override void DoInit(IContainer container)
        {
            if (_options != null)
            {
                var logger = container.Resolve <ILogger>();
                logger.Info($"[config]use jose.jwt for Auth");

                while (!container.IsRegistered <IRemoteServiceCaller>() || !container.IsRegistered <IClientServiceDiscovery>())
                {
                    Thread.Sleep(100);
                }

                var caller = container.Resolve <IRemoteServiceCaller>();
                caller.UseMiddleware <JwtAuthorizationMiddleware>(_options);

                if (string.IsNullOrEmpty(_options.TokenEndpointPath))
                {
                    return;
                }
                var discovery  = container.Resolve <IClientServiceDiscovery>();
                var addr       = new JimuAddress(_options.ServerIp, _options.ServerPort, _options.Protocol);
                var tokenRoute = new List <JimuServiceRoute> {
                    new JimuServiceRoute
                    {
                        Address = new List <JimuAddress> {
                            addr
                        },
                        ServiceDescriptor = new JimuServiceDesc
                        {
                            Id         = _options.GetServiceId(),
                            RoutePath  = JimuServiceRoute.ParseRoutePath("", "", _options.TokenEndpointPath, new[] { "username", "password" }, false),
                            Parameters = JimuHelper.Serialize <string>(new List <JimuServiceParameterDesc> {
                                new JimuServiceParameterDesc
                                {
                                    Comment = "username",
                                    Format  = "System.String",
                                    Name    = "username",
                                    Type    = "object"
                                },
                                new JimuServiceParameterDesc
                                {
                                    Comment = "password",
                                    Format  = "System.String",
                                    Name    = "password",
                                    Type    = "object"
                                },
                            }),
                            ReturnDesc = JimuHelper.Serialize <string>(new JimuServiceReturnDesc {
                                Comment      = "Token",
                                ReturnType   = "object",
                                ReturnFormat = "{\"access_token\":\"System.String | token\", \"expired_in\":\"System.Int32 | expired timestamp which is the number of seconds between 1970-01-01 and expired datetime\"}"
                            })
                        }
                    }
                };
                discovery.AddRoutesGetter(() =>
                {
                    return(Task.FromResult(tokenRoute));
                });
            }

            base.DoInit(container);
        }