/// <summary> /// 初始化 /// </summary> public static void Initialize() { ZeroApplication.Initialize(); RouteOption.CheckOption(); RouteChahe.InitCache(); ZeroApplication.ZeroNetEvent += OnZeroNetEvent; ZeroApplication.Run(); }
/// <summary> /// 调用 /// </summary> internal async void Call() { if (!CheckCall()) { return; } if (Data.HostName.Equals("Zero", StringComparison.OrdinalIgnoreCase)) { var manager = new ZeroManager(); manager.Command(Data); return; } // 1 初始化路由信息 if (!FindHost() || Data.RouteHost.Failed) { Data.Status = ZeroOperatorStatus.FormalError; Data.ResultMessage = ApiResult.NoFindJson; return; } // 2 安全检查 if (!TokenCheck()) { Data.Status = ZeroOperatorStatus.DenyAccess; Data.ResultMessage = ApiResult.DenyAccessJson; return; } // 3 缓存快速处理 if (RouteChahe.LoadCache(Data.Uri, Data.Token, out Data.CacheSetting, out Data.CacheKey, ref Data.ResultMessage)) { //找到并返回缓存 Data.Status = ZeroOperatorStatus.Success; return; } // 4 远程调用 if (!Data.RouteHost.ByZero) { Data.ResultMessage = await CallHttp(); } else if (ZeroApplication.ZerCenterIsRun) { Data.ResultMessage = CallZero(); } else { Data.Status = ZeroOperatorStatus.LocalError; Data.ResultMessage = ApiResult.NoReadyJson; return; } // 5 结果检查 SecurityChecker.CheckResult(Data); }
/* * /// <summary> * /// 数据 * /// </summary> * internal static List<RouteData> Datas = new List<RouteData>(); * * public static Mutex Mutex = new Mutex(); * * /// <summary> * /// 流程结束后的处理 * /// </summary> * internal static void PushFlowExtend(RouteData data) * { * Datas.Add(data); * Mutex.ReleaseMutex(); * } * * /// <summary> * /// 流程结束后的处理 * /// </summary> * internal static void FlowExtendTask() * { * while (true) * { * if (!Mutex.WaitOne(1000)) * { * continue; * } * var datas = Datas; * Datas = new List<RouteData>(); * Mutex.ReleaseMutex(); * //最多处理后50行 * var index = 0; * if (datas.Count > 50) * { * index = datas.Count - 50; * } * for (; index < datas.Count; index++) * { * ApiResult.CacheData(datas[index]); * } * } * }*/ #endregion #region 内部命令处理 /// <summary> /// 内部命令处理 /// </summary> /// <param name="url"></param> /// <param name="response"></param> /// <returns></returns> public static bool InnerCommand(string url, HttpResponse response) { //命令 switch (url) { case "/": response.WriteAsync("Wecome ZeroNet Http Router!", Encoding.UTF8); return(true); case "/_1_clear_1_": RouteChahe.Flush(); response.WriteAsync(JsonConvert.SerializeObject(RouteOption.Option, Formatting.Indented), Encoding.UTF8); return(true); //case "/_1_counter_1_/info": // response.WriteAsync(JsonConvert.SerializeObject(RouteCounter.Station, Formatting.Indented), Encoding.UTF8); // return true; //case "/_1_counter_1_/save": // RouteCounter.Save(); // response.WriteAsync(JsonConvert.SerializeObject(RouteCounter.Station, Formatting.Indented), Encoding.UTF8); // return true; case "/_1_config_1_": response.WriteAsync(JsonConvert.SerializeObject(RouteOption.Option, Formatting.Indented), Encoding.UTF8); return(true); //case "/_1_warings_1_": // response.WriteAsync(JsonConvert.SerializeObject(RuntimeWaring.WaringsTime, Formatting.Indented), Encoding.UTF8); // return true; //case "/_1_cache_1_": // response.WriteAsync(JsonConvert.SerializeObject(RouteChahe.Cache, Formatting.Indented), Encoding.UTF8); // return true; } return(false); }
/// <summary> /// 调用 /// </summary> /// <param name="arg"></param> /// <returns></returns> private static void CallTask(object arg) { HttpContext context = (HttpContext)arg; var router = new Router(context); //跨域支持 if (router.Data.HttpMethod == "OPTIONS") { HttpProtocol.Cros(context.Response); return; } IoHandler.OnBegin(router.Data); try { if (router.Data.Uri.LocalPath == "/publish") { bool suc = ZeroPublisher.Publish(context.Request.Form["Host"], context.Request.Form["Title"], context.Request.Form["Sub"], (string)context.Request.Form["Arg"]); context.Response.WriteAsync(suc ? ApiResult.SucceesJson : ApiResult.NetworkErrorJson, Encoding.UTF8); return; } //内容页转向 if (router.Data.Uri.LocalPath.IndexOf(".", StringComparison.OrdinalIgnoreCase) > 0) { context.Response.Redirect(RouteOption.Option.SystemConfig.ContextHost + router.Data.Uri.LocalPath.Trim('/')); return; } //内容页转向 if (router.Data.Uri.LocalPath.IndexOf(".", StringComparison.OrdinalIgnoreCase) > 0) { context.Response.Redirect(RouteOption.Option.SystemConfig.ContextHost + router.Data.Uri.LocalPath.Trim('/')); return; } HttpProtocol.FormatResponse(context.Response); //命令 if (InnerCommand(router.Data.Uri.LocalPath, context.Response)) { return; } //开始调用 if (!router.SecurityChecker.PreCheck()) { router.Data.Status = ZeroOperatorStatus.DenyAccess; context.Response.WriteAsync(ApiResult.DenyAccessJson, Encoding.UTF8); } else { // 正常调用 router.Call(); // 写入返回 router.WriteResult(); // 缓存 RouteChahe.CacheResult(router.Data); } } catch (Exception e) { OnError(router, e, context); } finally { IoHandler.OnEnd(router.Data); } }
/// <summary> /// 刷新 /// </summary> public static void Flush() { RouteChahe.Flush(); }