예제 #1
0
        public async Task InvokeAsync(HttpContext httpContext)
        {
            var opts = httpContext.RequestServices.GetService <LogDashboardOptions>();

            var requestUrl = httpContext.Request.Path.Value;

            //EmbeddedFile
            if (requestUrl.Contains("css") || requestUrl.Contains("js"))
            {
                await httpContext.Response.WriteAsync(LogDashboardEmbeddedFiles.IncludeEmbeddedFile(httpContext, requestUrl));

                return;
            }

            // Find Router
            var router = LogDashboardRoutes.Routes.FindRoute(requestUrl);

            if (router == null)
            {
                httpContext.Response.StatusCode = 404;
                return;
            }


            //Authorization
            if (!await AuthorizeHelper.AuthorizeAsync(httpContext, opts.AuthorizeData))
            {
                return;
            }

            var logDashboardContext = new LogDashboardContext(httpContext, router,
                                                              httpContext.RequestServices.GetService <IRazorLightEngine>(),
                                                              opts);

            if (!AuthorizationFilterHelper.Authorization(opts.AuthorizationFiles, logDashboardContext))
            {
                httpContext.Response.StatusCode = (int)HttpStatusCode.Unauthorized;
                return;
            }


            //Activate Handle
            var handleType = Assembly.GetAssembly(typeof(LogDashboardRoute))
                             .GetTypes().FirstOrDefault(x => x.Name.Contains(router.Handle + "Handle"));

            var handle = httpContext.RequestServices.GetRequiredService(handleType.MakeGenericType(opts.LogModelType)) as ILogDashboardHandle;

            if (handle == null)
            {
                httpContext.Response.StatusCode = 404;
                return;
            }

            using (var uow = httpContext.RequestServices.GetService <IUnitOfWork>())
            {
                handle.Context = logDashboardContext;

                string html;

                var method = handle.GetType().GetMethod(router.Action);
                // ReSharper disable once PossibleNullReferenceException
                var parametersLength = method.GetParameters().Length;

                if (parametersLength == 0)
                {
                    html = await(Task <string>) method.Invoke(handle, null);
                }
                else
                {
                    if (httpContext.Request.ContentLength == null && httpContext.Request.Query.Count <= 0)
                    {
                        html = await(Task <string>) method.Invoke(handle, new Object[] { null });
                    }
                    else
                    {
                        object args;
                        if (httpContext.Request.Query.Count > 0)
                        {
                            var dict = new Dictionary <string, string>();
                            httpContext.Request.Query.ToList().ForEach(x => dict.Add(x.Key, x.Value));
                            args = JsonConvert.DeserializeObject(JsonConvert.SerializeObject(dict), method.GetParameters().First().ParameterType);
                        }
                        else
                        {
                            // ReSharper disable once PossibleInvalidOperationException
                            var bytes = new byte[(int)httpContext.Request.ContentLength];
                            await httpContext.Request.Body.ReadAsync(bytes, 0, (int)httpContext.Request.ContentLength);

                            string requestJson = Encoding.Default.GetString(bytes);

                            args = JsonConvert.DeserializeObject(requestJson, method.GetParameters().First().ParameterType);
                        }

                        html = await(Task <string>) method.Invoke(handle, new[] { args });
                    }
                }

                await httpContext.Response.WriteAsync(html);
            }
        }
예제 #2
0
        public async Task InvokeAsync(HttpContext httpContext)
        {
            var opts = httpContext.RequestServices.GetService <LogDashboardOptions>();

            var requestUrl = httpContext.Request.Path.Value;
            var configs    = httpContext.RequestServices.GetService <IConfiguration>();
            var log        = configs["LogDashboard"];

            if (log != "open")
            {
                var Engine = httpContext.RequestServices.GetService <IRazorLightEngine>();
                var Route  = LogDashboardRoutes.Routes.FindRoute("/Dashboard/Tip");
                var vhtml  = await Engine.CompileRenderAsync(Route.View, null, null);

                await httpContext.Response.WriteAsync(vhtml);

                return;
                //httpContext.Response.ContentType = "text/html; charset=utf-8";
                //await httpContext.Response.WriteAsync("<div style='margin:10px auto;padding:20px; width:600px; height:300px; border:1px solid #ccc;background:yellow;line-height:50px;size:font-size:40px;'>");
                //await httpContext.Response.WriteAsync("日志面板被锁定<br>请联系华瑞技术中心: 0571-83786666 <br>提示:在配置中心设置 LogDashboard=open </div>");
            }

            //EmbeddedFile
            if (requestUrl.Contains("css") || requestUrl.Contains("js"))
            {
                await httpContext.Response.WriteAsync(LogDashboardEmbeddedFiles.IncludeEmbeddedFile(httpContext, requestUrl));

                return;
            }

            // Find Router
            var router = LogDashboardRoutes.Routes.FindRoute(requestUrl);

            if (router == null)
            {
                httpContext.Response.StatusCode = 404;
                return;
            }


            //Authorization
            if (!await AuthorizeHelper.AuthorizeAsync(httpContext, opts.AuthorizeData))
            {
                return;
            }

            var logDashboardContext = new LogDashboardContext(httpContext, router,
                                                              httpContext.RequestServices.GetService <IRazorLightEngine>(),
                                                              opts);

            if (!AuthorizationFilterHelper.Authorization(opts.AuthorizationFiles, logDashboardContext))
            {
                httpContext.Response.StatusCode = (int)HttpStatusCode.Unauthorized;
                return;
            }


            //Activate Handle
            var handleType = Assembly.GetAssembly(typeof(LogDashboardRoute))
                             .GetTypes().FirstOrDefault(x => x.Name.Contains(router.Handle + "Handle"));

            var handle = httpContext.RequestServices.GetRequiredService(handleType.MakeGenericType(opts.LogModelType)) as ILogDashboardHandle;

            if (handle == null)
            {
                httpContext.Response.StatusCode = 404;
                return;
            }

            using (var uow = httpContext.RequestServices.GetService <IUnitOfWork>())
            {
                handle.Context = logDashboardContext;

                string html;

                var method = handle.GetType().GetMethod(router.Action);
                // ReSharper disable once PossibleNullReferenceException
                var parametersLength = method.GetParameters().Length;

                if (parametersLength == 0)
                {
                    html = await(Task <string>) method.Invoke(handle, null);
                }
                else
                {
                    if (httpContext.Request.ContentLength == null && httpContext.Request.Query.Count <= 0)
                    {
                        html = await(Task <string>) method.Invoke(handle, new Object[] { null });
                    }
                    else
                    {
                        object args;
                        if (httpContext.Request.Query.Count > 0)
                        {
                            var dict = new Dictionary <string, string>();
                            httpContext.Request.Query.ToList().ForEach(x => dict.Add(x.Key, x.Value));
                            args = JsonConvert.DeserializeObject(JsonConvert.SerializeObject(dict), method.GetParameters().First().ParameterType);
                        }
                        else
                        {
                            // ReSharper disable once PossibleInvalidOperationException
                            var bytes = new byte[(int)httpContext.Request.ContentLength];
                            await httpContext.Request.Body.ReadAsync(bytes, 0, (int)httpContext.Request.ContentLength);

                            string requestJson = Encoding.Default.GetString(bytes);

                            args = JsonConvert.DeserializeObject(requestJson, method.GetParameters().First().ParameterType);
                        }

                        html = await(Task <string>) method.Invoke(handle, new[] { args });
                    }
                }

                await httpContext.Response.WriteAsync(html);
            }
        }