infrastructure

This commit is contained in:
Deep-Blue-2013 2018-03-18 21:07:36 -05:00
parent 1773a4afc6
commit 3d4134764a
27 changed files with 286 additions and 75 deletions

1
.gitignore vendored
View file

@ -287,3 +287,4 @@ __pycache__/
*.odx.cs
*.xsd.cs
/App_Data
/Bot.WebStarter/App_Data/bot-rasa.db

View file

@ -1,5 +1,5 @@
using Bot.Rasa.Agents;
using Bot.Rasa.Console;
using Bot.Rasa.Consoles;
using EntityFrameworkCore.BootKit;
using Microsoft.AspNetCore.Mvc;
using System;

View file

@ -10,9 +10,8 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="DotNetToolkit" Version="1.0.0" />
<PackageReference Include="EntityFrameworkCore.BootKit" Version="1.3.0" />
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.3" />
<PackageReference Include="DotNetToolkit" Version="1.4.0" />
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.6" />
</ItemGroup>
<ItemGroup>

View file

@ -0,0 +1,25 @@
using EntityFrameworkCore.BootKit;
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.Text;
namespace Microsoft.AspNetCore.Builder
{
public static class EntityDbContextBuilderExtensions
{
/// <summary>
/// Use CustomEntityFoundation
/// </summary>
/// <param name="app"></param>
/// <param name="configuration"></param>
/// <param name="contentRootPath"></param>
/// <param name="assembles"></param>
public static void UseEntityDbContext(this IApplicationBuilder app, IConfiguration configuration, String contentRootPath, String[] assembles)
{
Database.Configuration = configuration;
Database.Assemblies = assembles;
Database.ContentRootPath = contentRootPath;
}
}
}

View file

@ -1,6 +1,7 @@
using Bot.Rasa.Console;
using Bot.Rasa.Consoles;
using DotNetToolkit;
using EntityFrameworkCore.BootKit;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Data.Sqlite;
using MySql.Data.MySqlClient;
@ -12,9 +13,7 @@ using System.Text;
namespace Bot.Rasa.RestApi
{
#if !DEBUG
[Authorize]
#endif
//[Authorize]
[Produces("application/json")]
[Route("bot/[controller]")]
public class EssentialController : ControllerBase
@ -23,46 +22,7 @@ namespace Bot.Rasa.RestApi
public EssentialController()
{
dc = new Database();
string db = RasaConsole.Options.DbName;
string connectionString = RasaConsole.Options.DbConnectionString;
if (db.Equals("SqlServer"))
{
dc.BindDbContext<IDbRecord, DbContext4SqlServer>(new DatabaseBind
{
MasterConnection = new SqlConnection(connectionString),
CreateDbIfNotExist = true,
AssemblyNames = RasaConsole.Options.Assembles
});
}
else if (db.Equals("Sqlite"))
{
connectionString = connectionString.Replace("|DataDirectory|\\", RasaConsole.Options.ContentRootPath + "\\App_Data\\");
dc.BindDbContext<IDbRecord, DbContext4Sqlite>(new DatabaseBind
{
MasterConnection = new SqliteConnection(connectionString),
CreateDbIfNotExist = true,
AssemblyNames = RasaConsole.Options.Assembles
});
}
else if (db.Equals("MySql"))
{
dc.BindDbContext<IDbRecord, DbContext4MySql>(new DatabaseBind
{
MasterConnection = new MySqlConnection(connectionString),
CreateDbIfNotExist = true,
AssemblyNames = RasaConsole.Options.Assembles
});
}
else if (db.Equals("InMemory"))
{
dc.BindDbContext<IDbRecord, DbContext4Memory>(new DatabaseBind
{
AssemblyNames = RasaConsole.Options.Assembles
});
}
dc = new DefaultDataContextLoader().GetDefaultDc();
}
[HttpPatch("{table}/{id}")]

View file

@ -0,0 +1,20 @@
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.Text;
using Bot.Rasa.Loader;
namespace Microsoft.AspNetCore.Builder
{
public static class InitLoaderBuilderExtensions
{
/// <summary>
/// Initialize Loader
/// </summary>
/// <param name="app"></param>
public static void UseInitLoader(this IApplicationBuilder app)
{
new InitializationLoader().Load();
}
}
}

View file

@ -6,9 +6,10 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="EntityFrameworkCore.BootKit" Version="1.3.0" />
<PackageReference Include="Newtonsoft.Json" Version="10.0.3" />
<PackageReference Include="RestSharp" Version="106.1.0" />
<PackageReference Include="DotNetToolkit" Version="1.4.0" />
<PackageReference Include="EntityFrameworkCore.BootKit" Version="1.5.1" />
<PackageReference Include="Newtonsoft.Json" Version="11.0.1" />
<PackageReference Include="RestSharp" Version="106.2.1" />
</ItemGroup>
</Project>

View file

@ -11,7 +11,7 @@ using System.IO;
using System.Linq;
using System.Text;
namespace Bot.Rasa.Console
namespace Bot.Rasa.Consoles
{
public class RasaConsole
{

View file

@ -2,7 +2,7 @@
using System.Collections.Generic;
using System.Text;
namespace Bot.Rasa.Console
namespace Bot.Rasa.Consoles
{
public class RasaOptions
{

View file

@ -9,7 +9,7 @@ using System.Collections.Generic;
using System.IO;
using System.Text;
namespace Bot.Rasa.Console
namespace Bot.Rasa.Consoles
{
public static class RequestExtension
{

View file

@ -0,0 +1,28 @@
using Microsoft.EntityFrameworkCore;
using System;
using System.Linq;
using EntityFrameworkCore.BootKit;
using DotNetToolkit;
namespace Bot.Rasa.Loader
{
public class DbInitializer : IInitializationLoader
{
public int Priority => 1;
public void Initialize()
{
var dc = new DefaultDataContextLoader().GetDefaultDc();
var instances = TypeHelper.GetInstanceWithInterface<IHookDbInitializer>(Database.Assemblies).OrderBy(x => x.Priority).ToList();
for (int idx = 0; idx < instances.Count; idx++)
{
DateTime start = DateTime.UtcNow;
Console.WriteLine($"{instances[idx].ToString()} P:{instances[idx].Priority} started at {DateTime.UtcNow}");
int effected = dc.DbTran(() => instances[idx].Load(Database.Configuration, dc));
Console.WriteLine($"{instances[idx].ToString()} effected [{effected}] records in {(DateTime.UtcNow - start).TotalMilliseconds} ms");
}
}
}
}

View file

@ -0,0 +1,27 @@
using EntityFrameworkCore.BootKit;
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.Text;
namespace Bot.Rasa.Loader
{
/// <summary>
/// Initialize data for modules
/// </summary>
public interface IHookDbInitializer
{
/// <summary>
/// value smaller is higher priority
/// </summary>
int Priority { get; }
/// <summary>
/// Load data
/// </summary>
/// <param name="config"></param>
/// <param name="dc"></param>
/// <returns></returns>
void Load(IConfiguration config, Database dc);
}
}

View file

@ -0,0 +1,13 @@
using System.Text;
namespace Bot.Rasa.Loader
{
/// <summary>
/// Implement a customzied loader
/// </summary>
public interface IInitializationLoader
{
int Priority { get; }
void Initialize();
}
}

View file

@ -0,0 +1,34 @@
using DotNetToolkit;
using EntityFrameworkCore.BootKit;
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.Text;
namespace Bot.Rasa.Loader
{
public class InitializationLoader
{
public void Load()
{
DateTime startAll = DateTime.UtcNow;
Console.WriteLine();
Console.WriteLine($"*** *** *** *** InitializationLoader running *** *** *** ***");
Console.WriteLine();
var coreLoaders = TypeHelper.GetInstanceWithInterface<IInitializationLoader>(Database.Assemblies);
coreLoaders.ForEach(loader =>
{
DateTime start = DateTime.UtcNow;
Console.WriteLine($"{loader.ToString()} P:{loader.Priority}...");
loader.Initialize();
Console.WriteLine($"{loader.ToString()} completed in {(DateTime.UtcNow - start).TotalSeconds} s.");
Console.WriteLine();
});
Console.WriteLine($"*** *** *** *** InitializationLoader completed in {(DateTime.UtcNow - startAll).TotalSeconds} s. *** *** *** ***");
Console.WriteLine();
}
}
}

View file

@ -1,6 +1,6 @@
using Bot.Rasa;
using Bot.Rasa.Agents;
using Bot.Rasa.Console;
using Bot.Rasa.Consoles;
using EntityFrameworkCore.BootKit;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;

View file

@ -7,7 +7,9 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.5.0" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="2.0.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.0.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.6.1" />
<PackageReference Include="MSTest.TestAdapter" Version="1.2.0" />
<PackageReference Include="MSTest.TestFramework" Version="1.2.0" />
</ItemGroup>

View file

@ -1,21 +1,39 @@
using Bot.Rasa.Console;
using Bot.Rasa.Consoles;
using EntityFrameworkCore.BootKit;
using Microsoft.Data.Sqlite;
using Microsoft.Extensions.Configuration;
using System;
using System.IO;
using System.Linq;
namespace Bot.UnitTest
{
public abstract class TestEssential
{
protected Database dc { get; set; }
protected string contentRoot;
public TestEssential()
{
contentRoot = $"{Directory.GetCurrentDirectory()}\\..\\..\\..\\..\\Bot.WebStarter";
ConfigurationBuilder configurationBuilder = new ConfigurationBuilder();
var settings = Directory.GetFiles(contentRoot, "settings.*.json");
settings.ToList().ForEach(setting =>
{
configurationBuilder.AddJsonFile(setting, optional: false, reloadOnChange: true);
});
Database.Configuration = configurationBuilder.Build();
Database.Assemblies = new String[] { "Bot.Rasa" };
Database.ContentRootPath = contentRoot;
dc = new DefaultDataContextLoader().GetDefaultDc();
RasaConsole.Options = new RasaOptions
{
HostUrl = "http://192.168.56.101:5000",
ContentRootPath = $"{Directory.GetCurrentDirectory()}\\..\\..\\..\\..\\Bot.WebStarter",
ContentRootPath = contentRoot,
Assembles = new String[] { "Bot.Rasa" }
};
@ -24,8 +42,7 @@ namespace Bot.UnitTest
dc.BindDbContext<IDbRecord, DbContext4Sqlite>(new DatabaseBind
{
MasterConnection = new SqliteConnection($"Data Source={RasaConsole.Options.ContentRootPath}\\App_Data\\bot-rasa.db"),
CreateDbIfNotExist = true,
AssemblyNames = new string[] { "Bot.Rasa" }
CreateDbIfNotExist = true
});
}
}

View file

@ -13,7 +13,10 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.3" />
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.6" />
<PackageReference Include="Swashbuckle.AspNetCore.Swagger" Version="2.3.0" />
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerGen" Version="2.3.0" />
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerUI" Version="2.3.0" />
</ItemGroup>
<ItemGroup>

View file

@ -19,6 +19,16 @@ namespace Bot.WebStarter
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.ConfigureAppConfiguration((hostingContext, config) =>
{
var env = hostingContext.HostingEnvironment;
var settings = Directory.GetFiles("./", "settings.*.json");
settings.ToList().ForEach(setting =>
{
config.AddJsonFile(setting, optional: false, reloadOnChange: true);
});
})
.UseUrls("http://localhost:9900")
.UseStartup<Startup>()
.Build();
}

View file

@ -1,14 +1,19 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Bot.Rasa.Console;
using Bot.Rasa.Consoles;
using DotNetToolkit.JwtHelper;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Microsoft.Extensions.PlatformAbstractions;
using Newtonsoft.Json.Serialization;
using Swashbuckle.AspNetCore.Swagger;
namespace Bot.WebStarter
{
@ -24,7 +29,32 @@ namespace Bot.WebStarter
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddCors();
services.AddJwtAuth(Configuration);
services.AddMvc();
// Add framework services.
services.AddMvc(options =>
{
options.RespectBrowserAcceptHeader = true;
}).AddJsonOptions(options =>
{
options.SerializerSettings.Converters.Add(new Newtonsoft.Json.Converters.StringEnumConverter());
options.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
//options.SerializerSettings.NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore;
});
services.AddSwaggerGen(c =>
{
c.AddSecurityDefinition("Bearer", new ApiKeyScheme() { In = "header", Description = "Please insert JWT with Bearer into field", Name = "Authorization", Type = "apiKey" });
var info = Configuration.GetSection("Swagger").Get<Swashbuckle.AspNetCore.Swagger.Info>();
c.SwaggerDoc(info.Version, info);
var filePath = Path.Combine(PlatformServices.Default.Application.ApplicationBasePath, "Bot.Rasa.RestApi.xml");
c.IncludeXmlComments(filePath);
});
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
@ -35,17 +65,24 @@ namespace Bot.WebStarter
app.UseDeveloperExceptionPage();
}
app.UseStaticFiles();
app.UseSwagger();
app.UseSwaggerUI(c =>
{
c.SupportedSubmitMethods(SubmitMethod.Get, SubmitMethod.Post, SubmitMethod.Put, SubmitMethod.Patch, SubmitMethod.Delete);
c.ShowExtensions();
c.SwaggerEndpoint("/swagger/v1/swagger.json", "API v1");
c.RoutePrefix = "api";
});
app.UseCors(builder => builder.AllowAnyHeader().AllowAnyMethod().AllowAnyOrigin().AllowCredentials());
app.UseAuthentication();
app.UseMvc();
string db = RasaConsole.Configuration.GetSection("Database:Default").Value;
RasaConsole.Options = new RasaOptions
{
HostUrl = Configuration.GetSection("Rasa:Host").Value,
Assembles = new String[] { "Bot.Rasa" },
ContentRootPath = env.ContentRootPath,
DbName = db,
DbConnectionString = Configuration.GetSection("Database:ConnectionStrings")[db]
};
app.UseEntityDbContext(Configuration, env.ContentRootPath, new String[] { "Bot.Rasa" });
app.UseInitLoader();
}
}
}

View file

@ -11,9 +11,5 @@
"Default": "Warning"
}
}
},
"Rasa": {
"Host": "http://192.168.56.101:5000"
}
}

View file

@ -0,0 +1,11 @@
{
"TokenAuthentication": {
"SecretKey": "tK5aNR0FTEm4htInv+HA3A==",
"Subject": "Voicecoin",
"Issuer": "Voicecoin",
"Audience": "Voicecoin",
"TokenPath": "/token",
"CookieName": "token",
"LoginPath": "/login"
}
}

View file

@ -0,0 +1,10 @@
{
"AWS": {
"AWSRegionEndPoint": "us-east-1",
"AWSSecretKey": "38+UqlSqOVbKfL3LrS6hSmgW/ZSPgZzUa/Hom7ip",
"AWSAccessKey": "AKIAIPXFRDXOTKZWOVXQ",
"AWSEncoding": "utf-8",
"SESVerifiedEmail": "haiping008@gmail.com",
"AWSBucketPrefix": "voicecoin.ico"
}
}

View file

View file

@ -0,0 +1,5 @@
{
"Rasa": {
"Host": "http://192.168.56.101:5000"
}
}

View file

@ -0,0 +1,12 @@
{
"Swagger": {
"Version": "v1",
"Title": "RasaBot API",
"Description": "RasaBot API",
"TermsOfService": "MIT",
"Contact": {
"Name": "Haiping Chen",
"Email": "haiping008@gmail.com"
}
}
}