Skip to content

Instantly share code, notes, and snippets.

@FantailIO
Created September 18, 2017 09:06
Show Gist options
  • Save FantailIO/df16c2aec20efc6bfa829e74d2e289c6 to your computer and use it in GitHub Desktop.
Save FantailIO/df16c2aec20efc6bfa829e74d2e289c6 to your computer and use it in GitHub Desktop.
using System.IO;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.PlatformAbstractions;
using Swashbuckle.AspNetCore.Swagger;
using Timeapi.Data;
using Timeapi.Utils;
namespace Timeapi
{
public class Startup
{
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
.AddEnvironmentVariables();
Configuration = builder.Build();
}
public IConfigurationRoot Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
var connectionStringBuilder = new SqliteConnectionStringBuilder { DataSource = "times.db" };
var connectionString = connectionStringBuilder.ToString();
var connection = new SqliteConnection(connectionString);
services.AddDbContext<TimeContext>(options =>
options.UseSqlite(connection));
// Add CORS
services.AddCors(options =>
{
options.AddPolicy("AllowAllOrigins",
builder => builder.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader());
});
// Add framework services.
services.AddMvc();
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new Info
{
Title = "My API",
Version = "v1",
Description = "A simple example ASP.NET Core Web API",
TermsOfService = "None",
Contact = new Contact { Name = "Adam Fantail", Email = "adam at fantail.io", Url = "https://twitter.com/FantailIO" },
License = new License { Name = "Use under LICX", Url = "https://example.com/license" }
});
var basePath = PlatformServices.Default.Application.ApplicationBasePath;
var xmlPath = Path.Combine(basePath, "Timeapi.xml");
c.IncludeXmlComments(xmlPath);
});
services.Configure<LaunchDarklyConfig>(Configuration.GetSection("LaunchDarkly"));
services.AddSingleton<FeatureToggle>();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, TimeContext timeCtx)
{
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
loggerFactory.AddFile("Logs/Timeapi-{Date}.txt");
app.UseMvc();
// Enable middleware to serve generated Swagger as a JSON endpoint.
app.UseSwagger();
app.UseCors("AllowAllOrigins");
// Enable middleware to serve swagger-ui (HTML, JS, CSS etc.), specifying the Swagger JSON endpoint.
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
});
DbInitializer.Initialize(timeCtx);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment