BotSharp/BotSharp.WebHost/Startup.cs

149 lines
5.7 KiB
C#
Raw Normal View History

2018-10-03 17:21:19 +00:00
using Colorful;
using DotNetToolkit.JwtHelper;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.PlatformAbstractions;
using Newtonsoft.Json.Serialization;
using Swashbuckle.AspNetCore.Swagger;
using System;
2018-08-03 22:18:40 +00:00
using System.Collections.Generic;
2018-10-03 17:21:19 +00:00
using System.Drawing;
using System.IO;
using System.Linq;
2018-10-04 02:40:00 +00:00
using System.Reflection;
2018-10-03 17:21:19 +00:00
using Console = Colorful.Console;
namespace BotSharp.WebHost
{
public partial class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
services.AddCors();
2018-08-08 21:39:00 +00:00
services.AddJwtAuth(Configuration);
services.AddMvc(options =>
{
options.RespectBrowserAcceptHeader = true;
}).AddJsonOptions(options =>
{
options.SerializerSettings.Converters.Add(new Newtonsoft.Json.Converters.StringEnumConverter());
options.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
});
services.AddSwaggerGen(c =>
{
2018-08-08 21:39:00 +00:00
c.AddSecurityDefinition("Bearer", new ApiKeyScheme()
{
In = "header",
Description = "Please insert JWT with Bearer schema. Example: \"Authorization: Bearer {token}\"",
Name = "Authorization",
Type = "apiKey"
});
c.AddSecurityRequirement(new Dictionary<string, IEnumerable<string>> {
{ "Bearer", Enumerable.Empty<string>() },
});
var info = Configuration.GetSection("Swagger").Get<Info>();
c.SwaggerDoc(info.Version, info);
2018-10-04 02:40:00 +00:00
//var filePath = Path.Combine(PlatformServices.Default.Application.ApplicationBasePath, "BotSharp.RestApi.xml");
//c.IncludeXmlComments(filePath);
c.OperationFilter<SwaggerFileUploadOperation>();
});
// register platform dependency
2018-10-03 17:21:19 +00:00
/*services.AddTransient<IBotEngine>((provider) =>
{
return instance;
2018-10-03 17:21:19 +00:00
});*/
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
2018-09-28 00:49:43 +00:00
app.UseCors(builder => builder.AllowAnyHeader().AllowAnyMethod().AllowAnyOrigin().AllowCredentials());
app.UseDefaultFiles();
app.UseStaticFiles();
2018-08-08 21:39:00 +00:00
app.UseSwagger();
app.UseSwaggerUI(c =>
{
var info = Configuration.GetSection("Swagger").Get<Info>();
c.SupportedSubmitMethods(SubmitMethod.Get, SubmitMethod.Post, SubmitMethod.Put, SubmitMethod.Patch, SubmitMethod.Delete);
c.ShowExtensions();
c.SwaggerEndpoint(Configuration.GetValue<String>("Swagger:Endpoint"), info.Title);
c.RoutePrefix = String.Empty;
c.DocumentTitle = info.Title;
c.InjectStylesheet(Configuration.GetValue<String>("Swagger:Stylesheet"));
2018-09-29 18:18:34 +00:00
2018-10-04 03:39:39 +00:00
Console.WriteLine($"{info.Title} [{info.Version}] {info.License.Name}", Color.Gray);
2018-10-03 17:21:19 +00:00
Console.WriteLine($"{info.Description}", Color.Gray);
Console.WriteLine($"{info.Contact.Name}", Color.Gray);
});
app.Use(async (context, next) =>
{
string token = context.Request.Headers["Authorization"];
2018-08-08 21:39:00 +00:00
if (!string.IsNullOrWhiteSpace(token) && (token = token.Split(' ').Last()).Length == 32)
{
2018-08-08 21:39:00 +00:00
var config = (IConfiguration)AppDomain.CurrentDomain.GetData("Configuration");
context.Request.Headers["Authorization"] = token;
2018-08-08 21:39:00 +00:00
context.Request.Headers["Authorization"] = "Bearer " + JwtToken.GenerateToken(config, token);
}
await next.Invoke();
});
app.UseAuthentication();
app.UseMvc();
AppDomain.CurrentDomain.SetData("DataPath", Path.Combine(env.ContentRootPath, "App_Data"));
2018-08-03 22:18:40 +00:00
AppDomain.CurrentDomain.SetData("Configuration", Configuration);
AppDomain.CurrentDomain.SetData("ContentRootPath", env.ContentRootPath);
AppDomain.CurrentDomain.SetData("Assemblies", Configuration.GetValue<String>("Assemblies").Split(','));
2018-08-29 22:24:12 +00:00
/*InitializationLoader loader = new InitializationLoader();
2018-08-03 22:18:40 +00:00
loader.Env = env;
loader.Config = Configuration;
2018-08-29 22:24:12 +00:00
loader.Load();*/
2018-10-03 17:21:19 +00:00
2018-10-04 02:40:00 +00:00
// load dll dynamic
/*Assembly library = Assembly.LoadFile(Path.Combine(PlatformServices.Default.Application.ApplicationBasePath, "BotSharp.Platform.Articulate.dll"));
Type myClass = (from type in library.GetExportedTypes()
where typeof(IMyInterface).IsAssignableFrom(type)
select type)
.Single();*/
2018-10-03 17:21:19 +00:00
var platform = Configuration.GetValue<string>("Platform");
var engine = Configuration.GetValue<string>($"{platform}:BotEngine");
Formatter[] settings = new Formatter[]
{
new Formatter(platform, Color.Yellow),
new Formatter(engine, Color.Yellow),
};
2018-10-04 03:39:39 +00:00
Console.WriteLine();
Console.WriteLineFormatted("Platform Emulator: {0} powered by {1} engine.", Color.White, settings);
Console.WriteLine();
}
}
2018-08-03 22:18:40 +00:00
}