Ejemplo n.º 1
0
        public void ProcessRequest(IHttpRequest request, IHttpResponse response, string operationName)
        {
            try
            {
                response.ContentType = "application/json";
                var basePath = request.GetBaseUrl();
                var result   = new SwaggerResourcesResponse
                {
                    BasePath   = basePath,
                    Apis       = new List <SwaggerResourceRef>(),
                    ApiVersion = _config.ApiVersion,
                    Info       = new SwaggerInfo
                    {
                        Title = _config.Title ?? "Ant-SOA-API",
                    }
                };

                if (_config.UseBasicAuth)
                {
                    var basicAuth = request.GetBasicAuthUserAndPassword();
                    if (basicAuth == null)
                    {
                        result.Info.Title = "Auth Error";
                        response.Write(result.ToJson());
                        response.EndRequest(true);
                        return;
                    }
                    else
                    {
                        var userName  = basicAuth.Value.Key;
                        var password  = basicAuth.Value.Value;
                        var localAuth = _config.GetLocalAuthModel();
                        if (!localAuth.UserName.Equals(userName) && !localAuth.Password.Equals(password))
                        {
                            result.Info.Title = "Auth Error";
                            response.Write(result.ToJson());
                            response.EndRequest(true);
                            return;
                        }
                    }
                }
                result.Apis.Add(new SwaggerResourceRef
                {
                    Path        = request.ResolveAbsoluteUrl("~/" + SwaggerApiService.RESOURCE_PATH),
                    Description = _config.HostConfig.MetadataMap.FirstOrDefault().Value.ServiceName
                });

                result.Apis = result.Apis.OrderBy(a => a.Path).ToList();
                if (ResourcesResponseFilter != null)
                {
                    ResourcesResponseFilter(result);
                }

                response.Write(result.ToJson());
                response.EndRequest(true);
            }
            catch (Exception)
            {
                response.EndRequestWithNoContent();
            }
        }