/// <summary> /// 初始化 /// </summary> public static void Initialize() { // 日志支持 //Agebull.Common.Logging.LogRecorder.GetRequestIdFunc = () => ApiContext.RequestContext.RequestId;. LogRecorder.Initialize(new RemoteRecorder()); AppConfig.Initialize(Path.Combine(Startup.Configuration["contentRoot"], "route_config.json")); StationProgram.Run(); RouteChahe.Flush(); RuntimeWaring.Flush(); RouteCommand.ZeroFlush(); //Datas = new List<RouteData>(); }
/// <summary> /// 调用 /// </summary> /// <param name="context"></param> /// <returns></returns> public static void CallTask(HttpContext context) { var uri = context.Request.GetUri(); try { HttpProtocol.FormatResponse(context.Response); //内容页转向 if (uri.LocalPath.IndexOf(".", StringComparison.OrdinalIgnoreCase) > 0) { context.Response.Redirect(AppConfig.Config.SystemConfig.ContextHost + uri.LocalPath.Trim('/')); return; } //跨域支持 if (context.Request.Method.ToUpper() == "OPTIONS") { HttpProtocol.Cros(context.Response); return; } //命令 if (RouteCommand.InnerCommand(uri.LocalPath, context.Response)) { return; } } catch (Exception e) { LogRecorder.Exception(e); RuntimeWaring.Waring("Route", uri.LocalPath, e.Message); context.Response.WriteAsync(RouteRuntime.InnerError, Encoding.UTF8); return; } var router = new HttpRouter(context); HttpIoLog.OnBegin(router.Data); var counter = PerformanceCounter.OnBegin(router.Data); try { var checker = new SecurityChecker { Request = context.Request }; if (!checker.PreCheck()) { router.Data.Status = RouteStatus.DenyAccess; context.Response.WriteAsync(RouteRuntime.Inner2Error, Encoding.UTF8); } else { // 正常调用 router.Call(); LogRecorder.BeginStepMonitor("End"); // 写入返回 router.WriteResult(); // 缓存 RouteChahe.CacheResult(router.Data); } } catch (Exception e) { router.Data.Status = RouteStatus.LocalError; LogRecorder.Exception(e); RuntimeWaring.Waring("Route", uri.LocalPath, e.Message); context.Response.WriteAsync(RouteRuntime.InnerError, Encoding.UTF8); } finally { //计时 counter.End(router.Data); HttpIoLog.OnEnd(router.Data); } }