Skip to content
This repository was archived by the owner on Aug 16, 2021. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions src/Stratis.Bitcoin.Features.Api/ResourceFileProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using Microsoft.Extensions.FileProviders;
using Microsoft.Extensions.FileProviders.Embedded;
using Microsoft.Extensions.Primitives;

namespace Stratis.Bitcoin.Features.Api
{
public class ResourceFileProvider : IFileProvider, IDisposable
{
private readonly string rootPath;
private readonly Assembly entryAssembly;

public IDirectoryContents DirectoryContents { get; set; }

public ResourceFileProvider(Assembly entryAssembly, string rootPath)
{
this.rootPath = rootPath;
this.entryAssembly = entryAssembly;
}

public IFileInfo GetFileInfo(string subpath)
{
if (subpath == null)
{
return new NotFoundFileInfo(subpath);
}

var assemblyName = this.entryAssembly.GetName().Name;
var name = Path.GetFileName(subpath);
var resourcePath = string.Concat(assemblyName + ".", this.rootPath, subpath.Replace("/", "."));

if (this.entryAssembly.GetManifestResourceInfo(resourcePath) == null)
{
return new NotFoundFileInfo(name);
}

return new EmbeddedResourceFileInfo(this.entryAssembly, resourcePath, name, DateTime.UtcNow);
}

public IDirectoryContents GetDirectoryContents(string subpath)
{
return null;
}

public IChangeToken Watch(string filter)
{
return NullChangeToken.Singleton;
}

public void Dispose()
{
}
}
}
45 changes: 40 additions & 5 deletions src/Stratis.Bitcoin.Features.Api/Startup.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,28 @@
using System.IO;
using System;
using System.Text;
using System.Linq;
using System.IO;
using System.Reflection;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Hosting.Server.Features;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.PlatformAbstractions;
using Microsoft.Extensions.FileProviders;
using Swashbuckle.AspNetCore.Swagger;

namespace Stratis.Bitcoin.Features.Api
{
public class Startup
{
private readonly string currentFolder;

public Startup(IHostingEnvironment env)
{
this.currentFolder = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

IConfigurationBuilder builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
Expand Down Expand Up @@ -68,7 +78,22 @@ public void ConfigureServices(IServiceCollection services)
// Register the Swagger generator, defining one or more Swagger documents
services.AddSwaggerGen(setup =>
{
setup.SwaggerDoc("v1", new Info { Title = "Stratis.Bitcoin.Api", Version = "v1" });
setup.SwaggerDoc("v1", new Info
{
Title = "Stratis.Bitcoin.Api",
Description = "Stratis Full Node API Swagger",
Contact = new Contact()
{
Name = "Stratis Group Limited",
Url = "https://stratisplatform.com"
},
License = new License
{
Name = "MIT License",
Url = "https://github.com/stratisproject/StratisBitcoinFullNode/blob/master/LICENSE"
},
Version = "v1"
});

//Set the comments path for the swagger json and ui.
string basePath = PlatformServices.Default.Application.ApplicationBasePath;
Expand Down Expand Up @@ -97,17 +122,27 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerF

app.UseCors("CorsPolicy");

app.UseStaticFiles(new StaticFileOptions
{
FileProvider = new ResourceFileProvider(Assembly.GetExecutingAssembly(), "wwwroot"),
RequestPath = string.Empty
});

app.UseMvc();

// Enable middleware to serve generated Swagger as a JSON endpoint.
app.UseSwagger();

// Enable middleware to serve swagger-ui (HTML, JS, CSS etc.), specifying the Swagger JSON endpoint.
app.UseSwaggerUI(c =>
app.UseSwaggerUI(options =>
{
c.DefaultModelRendering(ModelRendering.Model);
c.SwaggerEndpoint("/swagger/v1/swagger.json", "Stratis.Bitcoin.Api V1");
options.DocumentTitle = "Stratis.Bitcoin.Api V1";
options.DefaultModelRendering(ModelRendering.Model);
options.InjectStylesheet("/css/swagger.css");
options.IndexStream = GetDefaultTemplateFromResource(app.ServerFeatures.Get<IServerAddressesFeature>().Addresses.First());
options.SwaggerEndpoint("/swagger/v1/swagger.json", "Stratis.Bitcoin.Api V1");
});
}
private Func<Stream> GetDefaultTemplateFromResource(string endpoint) => () => new MemoryStream(Encoding.UTF8.GetBytes(new StreamReader(Assembly.GetExecutingAssembly().GetManifestResourceStream(Assembly.GetExecutingAssembly().GetName().Name + ".wwwroot.swagger.default.html")).ReadToEnd().Replace("$endpoint", string.Concat(endpoint, "api"))));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
<DocumentationFile></DocumentationFile>
</PropertyGroup>

<ItemGroup>
<EmbeddedResource Include="wwwroot/**/*" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.App" Version="2.1.5" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="2.5.0" />
Expand Down
Loading