Files
reallife-gamemode/ReallifeGamemode.DataService/Startup.cs
2019-09-22 15:43:29 +02:00

141 lines
3.9 KiB
C#

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Microsoft.IdentityModel.Tokens;
using Microsoft.OpenApi.Models;
using ReallifeGamemode.Database.Models;
using ReallifeGamemode.DataService.Logic;
using Swashbuckle.AspNetCore.Swagger;
namespace ReallifeGamemode.DataService
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.Configure<ServerConfig>(cfg => Configuration.Bind(cfg));
services.AddDbContext<DatabaseContext>();
services.AddLogic();
services
.AddMvc()
.AddJsonOptions(j =>
{
j.SerializerSettings.NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore;
j.SerializerSettings.DateTimeZoneHandling = Newtonsoft.Json.DateTimeZoneHandling.Utc;
j.SerializerSettings.DateFormatHandling = Newtonsoft.Json.DateFormatHandling.IsoDateFormat;
});
var tokenKey = Encoding.UTF8.GetBytes(Configuration["TokenSecret"]);
services.AddAuthentication(o =>
{
o.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
o.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
})
.AddJwtBearer(o =>
{
o.RequireHttpsMetadata = true;
o.SaveToken = false;
o.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuerSigningKey = true,
IssuerSigningKey = new SymmetricSecurityKey(tokenKey),
ValidateIssuer = false,
ValidateAudience = false
};
});
string debugToken = null;
#if DEBUG
debugToken = services.BuildServiceProvider().GetService<JwtTokenGenerator>().GetDebugToken(tokenKey);
#endif
services.AddSwaggerGen(c =>
{
OpenApiInfo info = new OpenApiInfo
{
Title = "GTA:V ControlPanl DataService",
Version = "0.1",
};
if (debugToken != null)
{
info.Description = $"Debug-Token: {debugToken}";
}
c.SwaggerDoc("DataService", info);
c.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
{
Name = "Authorization",
In = ParameterLocation.Header,
Type = SecuritySchemeType.ApiKey,
Scheme = "Bearer"
});
c.AddSecurityRequirement(new OpenApiSecurityRequirement()
{
{
new OpenApiSecurityScheme()
{
Reference = new OpenApiReference
{
Type = ReferenceType.SecurityScheme,
Id = "Bearer"
}
},
new string[] {}
}
});
});
}
// 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)
{
app.UseAuthentication();
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseSwaggerUI(c =>
{
c.RoutePrefix = "doc";
c.SwaggerEndpoint("DataService.json", "DataService");
});
app.UseSwagger(c =>
{
c.RouteTemplate = "doc/{documentName}.json";
});
app.UseMvc();
}
}
}