public static IServiceCollection AddHubApi(this IServiceCollection serviceCollection, Action <HubApiConfiguration> configure) { var hubApiDoc = new HubApiConfiguration(); configure?.Invoke(hubApiDoc); serviceCollection.AddSingleton(hubApiDoc); return(serviceCollection); }
public static IApplicationBuilder UseHubApi(this IApplicationBuilder app) { var config = app.ApplicationServices.GetService <HubApiConfiguration>(); if (config == null) { config = new HubApiConfiguration(); } app.UseMiddleware <HubApiMiddleware>(config); return(app); }
public HubApiMiddleware(RequestDelegate next, HubApiConfiguration hubApiConfiguration) { _next = next; _hubApiConfiguration = hubApiConfiguration; }
public static FileContent GetFile(string requestPath, HubApiConfiguration hubApiConfiguration) { var basePath = hubApiConfiguration.ApiInfo?.HubApiEndpoint ?? "/hubapi"; string filePath; if (!basePath.StartsWith("/")) { basePath = "/" + basePath; } if (!requestPath.StartsWith(basePath)) { return(new FileContent()); } if (requestPath.EndsWith("/")) { requestPath = requestPath.Substring(0, requestPath.Length - 1); } if (requestPath.EndsWith("api.json")) { var schemaSettings = new JsonSchemaGeneratorSettings { SerializerSettings = JsonSerializerSettings, GenerateExamples = true, SchemaType = SchemaType.JsonSchema, GenerateAbstractSchemas = false, DefaultReferenceTypeNullHandling = ReferenceTypeNullHandling.NotNull }; var schema = new JsonSchema(); var resolver = new JsonSchemaResolver(schema, schemaSettings); var generator = new JsonSchemaGenerator(schemaSettings); generator.Generate(schema, typeof(object), resolver); var jDoc = JObject.FromObject(hubApiConfiguration); // generate schema definitions foreach (var hub in hubApiConfiguration.Hubs) { foreach (var hubMethod in hub.Methods) { if (hubMethod.Response.Type != typeof(void) && hubMethod.Response.Type != typeof(Task)) { generator.Generate(hubMethod.Response.Type, resolver); } foreach (var parameter in hubMethod.Parameters) { generator.Generate(parameter.Type, resolver); } } foreach (var clientMethod in hub.ClientMethods) { if (clientMethod.Response.Type != typeof(void) && clientMethod.Response.Type != typeof(Task)) { generator.Generate(clientMethod.Response.Type, resolver); } foreach (var parameter in clientMethod.Parameters) { generator.Generate(parameter.Type, resolver); } } } var schemaJObject = JObject.Parse(schema.ToJson()); jDoc["definitions"] = schemaJObject["definitions"]; var buffer = Encoding.UTF8.GetBytes(jDoc.ToString(Formatting.None)); var fileResult = new FileContent(requestPath, buffer); return(fileResult); } if (requestPath.Equals(basePath)) { filePath = "index.html"; } else { filePath = requestPath.Substring(basePath.Length + 1); filePath = filePath.Replace("/", "."); } var embeddedResource = $"{typeof(HubApiConfiguration).Namespace}.web.{filePath}"; using (var stream = typeof(HubApiConfiguration).Assembly.GetManifestResourceStream(embeddedResource)) { byte[] buffer = null; if (stream != null) { buffer = ReadToEnd(stream); } var fileResult = new FileContent(filePath, buffer); return(fileResult); } }