internal ServicesBase(string apiKey, string apiBaseUrl, int metarTafCacheTimeInMinutes, int stationCacheTimeInMinutes, string acceptHeader) { Http = new Http(apiKey, apiBaseUrl, metarTafCacheTimeInMinutes, stationCacheTimeInMinutes, acceptHeader); ApiHandles = new ApiHandles(); RawStreakApiResponseFactory = new ApiResponseFactory(); MetarTafCacheTimeInMinutes = metarTafCacheTimeInMinutes; StationCacheTimeInMinutes = stationCacheTimeInMinutes; }
/// <summary> /// 添加处理程序。 /// </summary> /// <param name="uriPrefix"></param> /// <param name="handle"></param> /// <returns></returns> public virtual Result AddHandle(string uriPrefix, Type handle) { if (!uriPrefix.StartsWith("/")) { return(new Result() { Message = $"参数{nameof(uriPrefix)}必须以/开头。" }); } ApiHandles.Add(uriPrefix, handle); return(true); }
/// <summary> /// 当收到请求时发生。 /// </summary> /// <param name="request"></param> protected virtual void OnRequest(HttpRequest request) { using (var response = request.Context.Response) { var apiHandle = ApiHandles.FirstOrDefault((item) => request.Url.AbsolutePath.StartsWith(item.Key, StringComparison.OrdinalIgnoreCase)); if (!string.IsNullOrEmpty(apiHandle.Key)) //api处理器 { response.AddHeader("Content-type", "application/json;charset=utf-8"); //添加响应头信息 response.ContentEncoding = Encoding.UTF8; DotNet.Result result = new DotNet.Result() { Message = "系统错误" }; try { result = OnBeginRequest(request, apiHandle); } catch (Exception ex) { result = new DotNet.Result() { Message = "系统错误:" + ex.Message }; } finally { using (System.IO.StreamWriter sw = new System.IO.StreamWriter(response.OutputStream)) { sw.Write(result.ToJson()); } } response.Close(); return; } var staticFile = StaticFiles.FirstOrDefault((item) => request.Url.AbsolutePath.StartsWith(item.Key, StringComparison.OrdinalIgnoreCase)); if (!string.IsNullOrEmpty(staticFile.Value))//静态资源 { var path = staticFile.Value; var urlPath = request.Url.AbsolutePath.Remove(0, staticFile.Key.Length); if (!string.IsNullOrEmpty(urlPath)) { urlPath = urlPath.Remove(0, 1).Replace('/', System.IO.Path.DirectorySeparatorChar); } WriteStaticFile(request, urlPath, staticFile.Value); return; } else { WriteStaticFile(request, request.Url.AbsolutePath.Remove(0, 1), WorkPath); return; } } }
internal RawServicesBase(string apiKey, string apiBaseUrl) { Http = new Http(apiKey, apiBaseUrl); ApiHandles = new ApiHandles(); RawStreakApiResponseFactory = new RawStreakApiResponseFactory(); }