/// <summary> /// 缓存结果 /// </summary> /// <param name="info"></param> /// <param name="item"></param> private static void CacheResult(ApiGatewayUrl item, DataContext db, ReturnModel info = null, Dictionary <string, object> dic = null) { var model = new ApiGatewayCache(); model.Key = item.Key.ToLower(); model.TimeOut = DateTime.Now.AddDays(item.CacheTimeOut.ToStr().ToDouble(0)).AddHours(8); if (info != null) { model.result = BaseJson.ModelToJson(info.msg); } if (dic != null) { model.result = BaseJson.ModelToJson(dic); } if (FastRead.Query <ApiGatewayCache>(a => a.Key.ToUpper() == model.Key.ToUpper()).ToCount(db) <= 0) { FastWrite.Add(model); } else { FastWrite.Update <ApiGatewayCache>(model, a => a.Key.ToUpper() == model.Key.ToUpper(), a => new { a.result, a.TimeOut }, db); } }
public void addNewUrl(string url) { ApiGatewayUrl apiUrl = new ApiGatewayUrl(); apiUrl = apiUrl.CheckUrl(url); urlrepository.Add(apiUrl); }
/// <summary> /// 验证token /// </summary> /// <param name="item"></param> /// <param name="context"></param> private static bool CheckToken(ApiGatewayUrl item, HttpContext context, DataContext db, string urlParam) { var dic = new Dictionary <string, object>(); var token = GetUrlParamKey(urlParam, "token"); if (FastRead.Query <ApiGatewayUser>(a => a.AccessToken.ToUpper() == token.ToUpper()).ToCount(db) == 0) { context.Response.StatusCode = 200; dic.Add("success", false); dic.Add("result", "token不存在"); context.Response.WriteAsync(BaseJson.ModelToJson(dic), Encoding.UTF8); return(false); } else { var tokenInfo = FastRead.Query <ApiGatewayUser>(a => a.AccessToken.ToUpper() == token.ToUpper()).ToItem <ApiGatewayUser>(db); if (DateTime.Compare(tokenInfo.AccessExpires, DateTime.Now) < 0) { context.Response.StatusCode = 200; dic.Add("success", false); dic.Add("result", "token过期"); context.Response.WriteAsync(BaseJson.ModelToJson(dic), Encoding.UTF8); return(false); } if (tokenInfo.Ip != GetClientIp(context)) { context.Response.StatusCode = 200; dic.Add("success", false); dic.Add("result", "token授权ip地址异常"); context.Response.WriteAsync(BaseJson.ModelToJson(dic), Encoding.UTF8); return(false); } if (tokenInfo.Power.IndexOf(',') > 0) { foreach (var temp in tokenInfo.Power.Split(',')) { if (temp.ToLower() == item.Key.ToLower()) { return(true); } } context.Response.StatusCode = 200; dic.Add("success", false); dic.Add("result", string.Format("{0}没有权限访问", item.Key)); context.Response.WriteAsync(BaseJson.ModelToJson(dic), Encoding.UTF8); return(false); } else { context.Response.StatusCode = 200; dic.Add("success", false); dic.Add("result", string.Format("{0}没有权限访问", item.Key)); context.Response.WriteAsync(BaseJson.ModelToJson(dic), Encoding.UTF8); return(false); } } }
public OrderProxy(HttpClient httpClient, ApiGatewayUrl apiGatewayUrl, IHttpContextAccessor httpContextAccessor) { httpClient.AddBearerToken(httpContextAccessor); this.httpClient = httpClient; this.apiGatewayUrl = apiGatewayUrl.Value; }
public ProductProxy( HttpClient httpClient, ApiGatewayUrl apiGatewayUrl, IHttpContextAccessor httpContextAccessor) { httpClient.AddBearerToken(httpContextAccessor); _httpClient = httpClient; _apiGatewayUrl = apiGatewayUrl.Value; }
/// <summary> /// 普通请求 /// </summary> private static Task Normal(ApiGatewayUrl item, HttpContext context, DataContext db, List <ApiGatewayDownParam> list, string urlParamDecode, string urlParam, IHttpClientFactory client) { var actionId = Guid.NewGuid().ToStr(); var downparam = list.FirstOrDefault() ?? new ApiGatewayDownParam(); var param = downparam.IsDecode == 1 ? urlParamDecode : urlParam; var info = GetReuslt(downparam, param, item.Key, item.IsTxtLog, item.IsDbLog, db, context, actionId, 1, client); //缓存结果 if (item.IsCache == 1) { CacheResult(item, db, info); } context.Response.StatusCode = info.status; return(context.Response.WriteAsync(info.msg, Encoding.UTF8)); }
/// <summary> /// 合并请求 /// </summary> /// <param name="item"></param> /// <param name="context"></param> private static Task Composite(ApiGatewayUrl item, HttpContext context, DataContext db, List <ApiGatewayDownParam> list, string urlParamDecode, string urlParam, IHttpClientFactory client) { var actionId = Guid.NewGuid().ToStr(); var orderBy = 1; var result = new ReturnModel(); var lastResult = new ReturnModel(); foreach (var downparam in list) { var param = downparam.IsDecode == 1 ? urlParamDecode : urlParam; if (downparam.SourceParam == 2) { param = lastResult.msg; } lastResult = GetReuslt(downparam, param, item.Key, item.IsTxtLog, item.IsDbLog, db, context, actionId, orderBy, client); if (string.IsNullOrEmpty(lastResult.msg) || lastResult.status != 200) { result.msg = ""; break; } if (downparam.IsResult == 1) { if (string.IsNullOrEmpty(result.msg)) { result.msg = lastResult.msg; } else { result.msg = string.Format("{0}||{1}", result.msg, lastResult.msg); } } orderBy++; } //缓存结果 if (item.IsCache == 1) { CacheResult(item, db, result, null); } context.Response.StatusCode = 200; return(context.Response.WriteAsync(result.msg, Encoding.UTF8)); }
private void CheckUrl() { Console.WriteLine("\nEnter URL to check"); string url_text = Console.ReadLine(); ApiGatewayUrl url = controller.CheckURL(url_text); Console.WriteLine("{0}\t{1}\t{2}", url.Url, url.Code.CodeNumber, url.Code.Description); Console.WriteLine("\nPress '0' to go the previous page or 'enter to check another URL"); var key = Console.ReadLine(); if (key == "0") { new MainView(); } else { CheckUrl(); } }
public ApiGatewayUrl CheckURL(string url) { var Url = new ApiGatewayUrl(); Url.CheckUrl(url); urls.Add(Url); UrlRepository.addNewUrl(url); var config = ConfigurationManager.OpenMappedMachineConfiguration(new ConfigurationFileMap(@"..\..\..\ServiceConfiguration.config")); ConfigSection = config.GetSection("serviceSection") as ServiceSection; ConfigSection.Add(new Service { ServiceType = "ApiGateWay", ServiceName = url, Metric = "url", Value = Url.Code.Description }); config.Save(ConfigurationSaveMode.Modified); return(Url); }
/// <summary> /// 轮循请求 /// </summary> private static Task Polling(ApiGatewayUrl item, HttpContext context, DataContext db, List <ApiGatewayDownParam> list, string urlParamDecode, string urlParam, IHttpClientFactory client) { var orderBy = 1; var actionId = Guid.NewGuid().ToStr(); var rand = new Random(); var index = rand.Next(1, list.Count); var downparam = list[index]; var param = downparam.IsDecode == 1 ? urlParamDecode : urlParam; var info = GetReuslt(downparam, param, item.Key, item.IsTxtLog, item.IsDbLog, db, context, actionId, orderBy, client); if (info.status != 200 && list.Count > 1) { orderBy++; var tempIndex = rand.Next(1, list.Count); while (index == tempIndex) { tempIndex = rand.Next(1, list.Count); } downparam = list[tempIndex]; info = GetReuslt(downparam, param, item.Key, item.IsTxtLog, item.IsDbLog, db, context, actionId, orderBy, client); context.Response.StatusCode = info.status; return(context.Response.WriteAsync(info.msg, Encoding.UTF8)); } else { //缓存结果 if (item.IsCache == 1) { CacheResult(item, db, info); } context.Response.StatusCode = info.status; return(context.Response.WriteAsync(info.msg, Encoding.UTF8)); } }
private void Button_Click(object sender, RoutedEventArgs e) { UrlList.Visibility = Visibility.Visible; var url = new ApiGatewayUrl(); url.CheckUrl(UrlTxt.Text); UrlVM.UrlRepository.addNewUrl(UrlTxt.Text); UrlVM.urls.Add(url); var config = ConfigurationManager.OpenMappedMachineConfiguration(new ConfigurationFileMap(@"..\..\ServiceConfiguration.config")); ConfigSection = config.GetSection("serviceSection") as ServiceSection; ConfigSection.Add(new Service { ServiceType = "ApiGateWay", ServiceName = url.Url, Metric = "url", Value = url.Code.Description }); config.Save(ConfigurationSaveMode.Modified); UrlList.Items.Refresh(); }
public IActionResult UrlOption(UrlModel item) { using (var db = new DataContext(App.DbKey.Api)) { var success = true; var model = new ApiGatewayUrl(); model.CacheTimeOut = item.CacheTimeOut; model.IsAnonymous = item.IsAnonymous; model.IsCache = item.IsCache; model.IsDbLog = item.IsDbLog; model.IsGetToken = item.IsGetToken; model.IsTxtLog = item.IsTxtLog; model.Key = item.Key; model.Name = item.Name; model.Schema = item.Schema; if (FastRead.Query <ApiGatewayUrl>(a => a.Key.ToLower() == item.Key.ToLower()).ToCount(db) > 0) { success = db.Update <ApiGatewayUrl>(model, a => a.Key.ToLower() == item.Key.ToLower()).writeReturn.IsSuccess; } else { success = db.Add(model).writeReturn.IsSuccess; } if (success) { return(Json(new { success = true, msg = "操作成功" })); } else { return(Json(new { success = false, msg = "操作失败" })); } } }