diff --git a/projects/Infrastructure/Infrastructure.csproj b/projects/Infrastructure/Infrastructure.csproj index c024eb97..c0da67c8 100644 --- a/projects/Infrastructure/Infrastructure.csproj +++ b/projects/Infrastructure/Infrastructure.csproj @@ -23,6 +23,8 @@ + + diff --git a/projects/Infrastructure/Web/BaseStartup.cs b/projects/Infrastructure/Web/BaseStartup.cs index c074c562..0fdbeda1 100644 --- a/projects/Infrastructure/Web/BaseStartup.cs +++ b/projects/Infrastructure/Web/BaseStartup.cs @@ -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(); }); } - 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(); - var dbConnectionName = this._cfg.GetSection("AppSettings").GetValue("database"); - _connectionString = this._cfg.GetConnectionString(dbConnectionName); + var dbConnectionName = this.cfg.GetSection("AppSettings").GetValue("database"); + _connectionString = this.cfg.GetConnectionString(dbConnectionName); if (dbConnectionName == "sqlite") { services.AddDbContext(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("openapi.name", "v1"), new OpenApiInfo + c.SwaggerDoc(this.cfg.GetValue("openapi.name", "v1"), new OpenApiInfo { - Title = this._cfg.GetValue("openapi.title", "web api"), - Version = this._cfg.GetValue("openapi.version", "1.0") + Title = this.cfg.GetValue("openapi.title", "web api"), + Version = this.cfg.GetValue("openapi.version", "1.0") }); c.EnableAnnotations(); }); @@ -327,7 +325,7 @@ namespace Infrastructure.Web app.UseExceptionHandler("/Error"); app.UseStatusCodePagesWithReExecute("/Error"); } - string basePath = this._cfg.GetValue("BasePath", ""); + string basePath = this.cfg.GetValue("BasePath", ""); app.UsePathBase(basePath); app.UseForwardedHeaders(new ForwardedHeadersOptions { diff --git a/projects/IoTCenter/Program.cs b/projects/IoTCenter/Program.cs index 3388aa9c..ff2c1222 100644 --- a/projects/IoTCenter/Program.cs +++ b/projects/IoTCenter/Program.cs @@ -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("ServerAddresses").Split(';').ToList(); + o.Optional = section.GetValue("Optional"); + o.DataId = section.GetValue("DataId"); + o.Group = section.GetValue("Group"); + o.Tenant = section.GetValue("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() + .Build() + .Run(); + } + + private static void NewMethod(string[] args) { Console.OutputEncoding = Encoding.UTF8; var host = "localhost"; diff --git a/projects/IoTCenter/Startup.cs b/projects/IoTCenter/Startup.cs index 55a4acc2..585ea498 100644 --- a/projects/IoTCenter/Startup.cs +++ b/projects/IoTCenter/Startup.cs @@ -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(); services.AddTransient(); 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 { "IoT.Shared" })); + services.ConfigureOptions(new FileConfigureOptions(env, new List { "IoT.Shared" })); } public override void UseSignalR(IEndpointRouteBuilder endpoints) diff --git a/projects/IoTCenter/appsettings.Development.json b/projects/IoTCenter/appsettings.Development.json index 4b4f5614..f60dd1b2 100644 --- a/projects/IoTCenter/appsettings.Development.json +++ b/projects/IoTCenter/appsettings.Development.json @@ -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" } } \ No newline at end of file diff --git a/projects/IoTCenter/appsettings.json b/projects/IoTCenter/appsettings.json index 824a1ac7..df93ed60 100644 --- a/projects/IoTCenter/appsettings.json +++ b/projects/IoTCenter/appsettings.json @@ -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": "" } } \ No newline at end of file