Former-commit-id: cabd2bd6a25ee3eb0843556adf976e703ab63f6c
TangShanKaiPing
wanggang 5 years ago
parent 8c7f0c6e73
commit 895d4271b3

@ -23,6 +23,8 @@
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="3.1.2" />
<PackageReference Include="Microsoft.AspNetCore.SignalR" Version="1.1.0" />
<PackageReference Include="nacos-sdk-csharp-unofficial" Version="0.2.5" />
<PackageReference Include="nacos-sdk-csharp-unofficial.AspNetCore" Version="0.2.5" />
<PackageReference Include="nacos-sdk-csharp-unofficial.Extensions.Configuration" Version="0.2.5" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="3.1.2" />
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="3.1.1" />
<PackageReference Include="Serilog.AspNetCore" Version="3.2.0" />

@ -32,12 +32,10 @@ using Microsoft.Extensions.Options;
using Microsoft.IdentityModel.Tokens;
using Microsoft.OpenApi.Models;
using Newtonsoft.Json;
using StackExchange.Redis;
using System;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Net;
using System.Reflection;
using System.Text;
using System.Text.Encodings.Web;
@ -48,20 +46,20 @@ namespace Infrastructure.Web
{
public class BaseStartup
{
private readonly IConfiguration _cfg;
private readonly IWebHostEnvironment _env;
protected readonly IConfiguration cfg;
protected readonly IWebHostEnvironment env;
private string _connectionString;
private readonly string _origins = "AllowAllHeaders";
public BaseStartup(IConfiguration configuration, IWebHostEnvironment env)
{
this._cfg = configuration;
this._env = env;
this.cfg = configuration;
this.env = env;
}
public virtual void ConfigureServices(IServiceCollection services)
{
if (!this._env.IsDevelopment())
if (!this.env.IsDevelopment())
{
services.AddResponseCompression(options =>
{
@ -69,7 +67,7 @@ namespace Infrastructure.Web
options.Providers.Add<GzipCompressionProvider>();
});
}
services.AddSingleton(_cfg as IConfigurationRoot);
services.AddSingleton(cfg as IConfigurationRoot);
services.AddCors(options => options.AddPolicy(_origins,
builder =>
{
@ -87,8 +85,8 @@ namespace Infrastructure.Web
this.ConfigureOptions(services);
EfDbContext.OnModelCreatingAction = this.OnModelCreating;
services.AddScoped<DbContext, EfDbContext>();
var dbConnectionName = this._cfg.GetSection("AppSettings").GetValue<string>("database");
_connectionString = this._cfg.GetConnectionString(dbConnectionName);
var dbConnectionName = this.cfg.GetSection("AppSettings").GetValue<string>("database");
_connectionString = this.cfg.GetConnectionString(dbConnectionName);
if (dbConnectionName == "sqlite")
{
services.AddDbContext<EfDbContext>(options => options.UseSqlite(_connectionString));
@ -165,7 +163,7 @@ namespace Infrastructure.Web
var signalRBuilder = services
.AddSignalR(o => o.EnableDetailedErrors = true)
.AddJsonProtocol();
var signalRRedisConnectionString = _cfg.GetConnectionString("redis");
var signalRRedisConnectionString = cfg.GetConnectionString("redis");
if (!string.IsNullOrEmpty(signalRRedisConnectionString))
{
signalRBuilder = signalRBuilder.AddStackExchangeRedis(signalRRedisConnectionString, o =>
@ -223,7 +221,7 @@ namespace Infrastructure.Web
public virtual void ConfigureOptions(IServiceCollection services)
{
services.ConfigureOptions(new FileConfigureOptions(this._env));
services.ConfigureOptions(new FileConfigureOptions(this.env));
}
public virtual void AddAuthentication(IServiceCollection services)
@ -239,11 +237,11 @@ namespace Infrastructure.Web
o.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuerSigningKey = true,
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_cfg["jwt:key"])),
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(cfg["jwt:key"])),
ValidateIssuer = false,
ValidIssuer = _cfg["jwt:issuer"],
ValidIssuer = cfg["jwt:issuer"],
ValidateAudience = false,
ValidAudience = _cfg["jwt:audience"]
ValidAudience = cfg["jwt:audience"]
};
o.Events = new JwtBearerEvents
{
@ -299,10 +297,10 @@ namespace Infrastructure.Web
{
services.AddSwaggerGen(c =>
{
c.SwaggerDoc(this._cfg.GetValue<string>("openapi.name", "v1"), new OpenApiInfo
c.SwaggerDoc(this.cfg.GetValue<string>("openapi.name", "v1"), new OpenApiInfo
{
Title = this._cfg.GetValue<string>("openapi.title", "web api"),
Version = this._cfg.GetValue<string>("openapi.version", "1.0")
Title = this.cfg.GetValue<string>("openapi.title", "web api"),
Version = this.cfg.GetValue<string>("openapi.version", "1.0")
});
c.EnableAnnotations();
});
@ -327,7 +325,7 @@ namespace Infrastructure.Web
app.UseExceptionHandler("/Error");
app.UseStatusCodePagesWithReExecute("/Error");
}
string basePath = this._cfg.GetValue<String>("BasePath", "");
string basePath = this.cfg.GetValue<String>("BasePath", "");
app.UsePathBase(basePath);
app.UseForwardedHeaders(new ForwardedHeadersOptions
{

@ -3,11 +3,20 @@ using Infrastructure.Configuration;
using Infrastructure.Extensions;
using Infrastructure.Web.Hosting;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Serilog;
using Serilog.Formatting.Json;
using Serilog.Sinks.Elasticsearch;
using Serilog.Sinks.RollingFile;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
namespace IoTCenter
@ -15,6 +24,52 @@ namespace IoTCenter
public class Program
{
public static void Main(string[] args)
{
var config = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: true)
.Build();
WebHost.CreateDefaultBuilder(args)
.ConfigureAppConfiguration(o => o.AddNacosConfiguration(o =>
{
var section = config.GetSection("nacos");
o.ServerAddresses = section.GetValue<string>("ServerAddresses").Split(';').ToList();
o.Optional = section.GetValue<bool>("Optional");
o.DataId = section.GetValue<string>("DataId");
o.Group = section.GetValue<string>("Group");
o.Tenant = section.GetValue<string>("Tenant");
}))
//.Configure(o =>
//{
// o.UseNacosAspNetCore();
//})
//.ConfigureServices((c, o) =>
//{
// o.AddNacosAspNetCore(c.Configuration);
//})
.ConfigureLogging((c, o) => //http://localhost:9200/_search?size=100
{
var url = c.Configuration["elasticsearch:url"];
Log.Logger = new LoggerConfiguration()
.WriteTo.Elasticsearch(new ElasticsearchSinkOptions(new Uri(url))
{
TypeName = "_doc",
AutoRegisterTemplate = true,
AutoRegisterTemplateVersion = AutoRegisterTemplateVersion.ESv7,
FailureCallback = e => Console.WriteLine("Unable to submit event " + e.MessageTemplate),
EmitEventFailure = EmitEventFailureHandling.WriteToSelfLog |
EmitEventFailureHandling.WriteToFailureSink |
EmitEventFailureHandling.RaiseCallback,
FailureSink = new RollingFileSink("logs/log.txt", new JsonFormatter(), fileSizeLimitBytes: 100 * 1024 * 1024, 1)
})
.CreateLogger();
o.AddSerilog();
})
.UseStartup<Startup>()
.Build()
.Run();
}
private static void NewMethod(string[] args)
{
Console.OutputEncoding = Encoding.UTF8;
var host = "localhost";

@ -9,11 +9,15 @@ using IoT.UI.Shard;
using IoTCenter.Services;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Hosting.Server.Features;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.AspNetCore.Routing;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Nacos.AspNetCore;
using System;
using System.Collections.Generic;
using System.Linq;
@ -23,11 +27,8 @@ namespace IoTCenter
{
public class Startup : IoTServiceStartup
{
private readonly IWebHostEnvironment _env;
public Startup(IConfiguration configuration, IWebHostEnvironment env) : base(configuration, env)
{
this._env = env;
}
public override void ConfigureServices(IServiceCollection services)
@ -37,11 +38,13 @@ namespace IoTCenter
services.AddTransient<IoTCenterJob>();
services.AddTransient<IRoleService, RoleService>();
base.ConfigureServices(services);
services.AddNacosAspNetCore(cfg);
}
public override void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerFactory loggerFactory)
{
base.Configure(app, env, loggerFactory);
app.UseNacosAspNetCore();
//
Task.Run(() =>
{
@ -64,7 +67,7 @@ namespace IoTCenter
public override void ConfigureOptions(IServiceCollection services)
{
//Console.WriteLine(IoT.Resources.Resource.WebFolder);
services.ConfigureOptions(new FileConfigureOptions(_env, new List<string> { "IoT.Shared" }));
services.ConfigureOptions(new FileConfigureOptions(env, new List<string> { "IoT.Shared" }));
}
public override void UseSignalR(IEndpointRouteBuilder endpoints)

@ -1,9 +1,30 @@
{
"Logging": {
"LogLevel": {
"Default": "Debug",
"System": "Information",
"Microsoft": "Warning"
}
"server.urls": "http://*:8011",
"nacos": {
"ServerAddresses": [ "localhost:8848" ],
"DefaultTimeOut": 15,
"Namespace": "",
"ListenInterval": 1000,
"ServiceName": "IoTCenter",
"Ip": "127.0.0.1"
},
"jwt": {
"key": "111111111111111111111111",
"issuer": "111111111111111111111111",
"audience": "111111111111111111111111"
},
"elasticsearch": {
"url": "http://localhost:9200"
},
"ConnectionStrings": {
"postgresql": "User ID=root;Host=localhost;Port=26257;Database=iotdb;CommandTimeout=120",
"sqlite": "Data Source=iotcenter.db",
"mysql": "Server=127.0.0.1;Port=3306;Database=iotcenter;Uid=root;Pwd=root;",
"redis": "127.0.0.1:6379,allowAdmin=true"
},
"AppSettings": {
"database": "sqlite",
"UseCookieSessionStore": false,
"upload": "http://10.10.24.104:8180/group1/upload"
}
}

@ -1,19 +1,15 @@
{
"Logging": {
"LogLevel": {
"Default": "Warning",
"Microsoft": "Warning"
"Default": "Warning"
}
},
"ConnectionStrings": {
"postgresql": "User ID=root;Host=localhost;Port=26257;Database=iotdb;CommandTimeout=120",
"sqlite": "Data Source=iotcenter.db",
"mysql": "Server=127.0.0.1;Port=3306;Database=iotcenter;Uid=root;Pwd=root;",
"redis": "127.0.0.1:6379,allowAdmin=true"
},
"AppSettings": {
"database": "sqlite",
"UseCookieSessionStore": false,
"upload": "http://10.10.24.104:8180/group1/upload"
"AllowedHosts": "*",
"nacos": {
"ServerAddresses": "localhost:8848",
"Optional": false,
"DataId": "IoTCenter",
"Group": "IoT",
"Tenant": ""
}
}
Loading…
Cancel
Save