2018-10-05 06:39:39 +00:00
|
|
|
|
using BotSharp.Core.Modules;
|
|
|
|
|
|
using DotNetToolkit.JwtHelper;
|
2018-07-13 12:21:40 +00:00
|
|
|
|
using Microsoft.AspNetCore.Builder;
|
|
|
|
|
|
using Microsoft.AspNetCore.Hosting;
|
|
|
|
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
|
using Newtonsoft.Json.Serialization;
|
2018-07-15 12:31:52 +00:00
|
|
|
|
using Swashbuckle.AspNetCore.Swagger;
|
2018-11-19 15:02:51 +00:00
|
|
|
|
using Swashbuckle.AspNetCore.SwaggerUI;
|
2018-08-15 22:37:23 +00:00
|
|
|
|
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;
|
2018-08-15 22:37:23 +00:00
|
|
|
|
using System.IO;
|
|
|
|
|
|
using System.Linq;
|
2018-07-13 12:21:40 +00:00
|
|
|
|
|
|
|
|
|
|
namespace BotSharp.WebHost
|
|
|
|
|
|
{
|
|
|
|
|
|
public partial class Startup
|
|
|
|
|
|
{
|
|
|
|
|
|
public Startup(IConfiguration configuration)
|
|
|
|
|
|
{
|
|
|
|
|
|
Configuration = configuration;
|
2018-10-05 06:39:39 +00:00
|
|
|
|
this.modulesStartup = new ModulesStartup(configuration);
|
2018-07-13 12:21:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-10-05 06:39:39 +00:00
|
|
|
|
private readonly ModulesStartup modulesStartup;
|
|
|
|
|
|
|
2018-07-13 12:21:40 +00:00
|
|
|
|
public IConfiguration Configuration { get; }
|
|
|
|
|
|
|
|
|
|
|
|
public void ConfigureServices(IServiceCollection services)
|
|
|
|
|
|
{
|
2018-07-15 12:31:52 +00:00
|
|
|
|
services.AddCors();
|
2018-08-08 21:39:00 +00:00
|
|
|
|
services.AddJwtAuth(Configuration);
|
2018-07-15 12:31:52 +00:00
|
|
|
|
|
2018-10-05 01:57:10 +00:00
|
|
|
|
var mvcBuilder = services.AddMvc(options =>
|
2018-07-13 12:21:40 +00:00
|
|
|
|
{
|
|
|
|
|
|
options.RespectBrowserAcceptHeader = true;
|
|
|
|
|
|
}).AddJsonOptions(options =>
|
|
|
|
|
|
{
|
|
|
|
|
|
options.SerializerSettings.Converters.Add(new Newtonsoft.Json.Converters.StringEnumConverter());
|
|
|
|
|
|
options.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
|
|
|
|
|
|
});
|
2018-07-15 12:31:52 +00:00
|
|
|
|
|
2018-10-22 03:47:51 +00:00
|
|
|
|
//PlatformModuleAssembyLoader.LoadAssemblies(Configuration, assembly => mvcBuilder.AddApplicationPart(assembly));
|
2018-10-05 12:52:02 +00:00
|
|
|
|
|
2018-10-05 06:39:39 +00:00
|
|
|
|
this.modulesStartup.ConfigureServices(services);
|
2018-10-05 01:57:10 +00:00
|
|
|
|
|
2018-07-15 12:31:52 +00:00
|
|
|
|
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>() },
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2018-07-15 12:31:52 +00:00
|
|
|
|
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);
|
|
|
|
|
|
|
2018-08-27 12:59:47 +00:00
|
|
|
|
c.OperationFilter<SwaggerFileUploadOperation>();
|
2018-07-15 12:31:52 +00:00
|
|
|
|
});
|
2018-08-01 04:29:55 +00:00
|
|
|
|
|
|
|
|
|
|
// register platform dependency
|
2018-10-03 17:21:19 +00:00
|
|
|
|
/*services.AddTransient<IBotEngine>((provider) =>
|
2018-08-01 04:29:55 +00:00
|
|
|
|
{
|
|
|
|
|
|
return instance;
|
2018-10-03 17:21:19 +00:00
|
|
|
|
});*/
|
2018-07-13 12:21:40 +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());
|
|
|
|
|
|
|
2018-07-15 12:31:52 +00:00
|
|
|
|
app.UseDefaultFiles();
|
|
|
|
|
|
app.UseStaticFiles();
|
|
|
|
|
|
|
2018-08-08 21:39:00 +00:00
|
|
|
|
app.UseSwagger();
|
2018-07-15 12:31:52 +00:00
|
|
|
|
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-12 02:42:56 +00:00
|
|
|
|
Console.WriteLine();
|
2018-10-05 01:57:10 +00:00
|
|
|
|
Console.WriteLine($"{info.Title} [{info.Version}] {info.License.Name}");
|
|
|
|
|
|
Console.WriteLine($"{info.Description}");
|
2018-10-12 02:42:56 +00:00
|
|
|
|
Console.WriteLine($"{info.Contact.Name}, {DateTime.UtcNow.ToString()}");
|
|
|
|
|
|
Console.WriteLine();
|
2018-07-15 12:31:52 +00:00
|
|
|
|
});
|
|
|
|
|
|
|
2018-08-01 04:29:55 +00:00
|
|
|
|
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-01 04:29:55 +00:00
|
|
|
|
{
|
2018-08-08 21:39:00 +00:00
|
|
|
|
var config = (IConfiguration)AppDomain.CurrentDomain.GetData("Configuration");
|
2018-09-04 20:39:51 +00:00
|
|
|
|
context.Request.Headers["Authorization"] = token;
|
2018-08-08 21:39:00 +00:00
|
|
|
|
|
2018-09-04 20:39:51 +00:00
|
|
|
|
context.Request.Headers["Authorization"] = "Bearer " + JwtToken.GenerateToken(config, token);
|
2018-08-01 04:29:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
await next.Invoke();
|
|
|
|
|
|
});
|
|
|
|
|
|
app.UseAuthentication();
|
|
|
|
|
|
|
2018-07-13 12:21:40 +00:00
|
|
|
|
app.UseMvc();
|
|
|
|
|
|
|
2018-10-14 14:15:27 +00:00
|
|
|
|
this.modulesStartup.Configure(app, env);
|
|
|
|
|
|
|
2018-08-15 22:37:23 +00:00
|
|
|
|
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-07-13 12:21:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2018-08-03 22:18:40 +00:00
|
|
|
|
}
|