From c2b3dd03ab97d24b25139bcf382922f6d961c383 Mon Sep 17 00:00:00 2001
From: smartwyy <645583145@qq.com>
Date: Fri, 31 May 2024 15:33:47 +0800
Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E7=B3=BB=E7=BB=9F=E6=A1=86?=
=?UTF-8?q?=E6=9E=B6?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../Configuration/AppSettingsHelper.cs | 71 +++++++++++++++----
WebStarter/Program.cs | 25 +++----
WebStarter/Properties/launchSettings.json | 6 +-
WebStarter/WebStarter.csproj | 2 +-
WebStarter/appsettings.Development.json | 8 ---
.../appsettings.json => appsettings.dev.json} | 9 ++-
WebStarter/appsettings.json | 28 +-------
WebStarter/appsettings.prod.json | 34 +++++++++
8 files changed, 115 insertions(+), 68 deletions(-)
delete mode 100644 WebStarter/appsettings.Development.json
rename WebStarter/{bin/Debug/net6.0/appsettings.json => appsettings.dev.json} (70%)
create mode 100644 WebStarter/appsettings.prod.json
diff --git a/HybirdFrameworkCore/Configuration/AppSettingsHelper.cs b/HybirdFrameworkCore/Configuration/AppSettingsHelper.cs
index b0d1d8a..118423a 100644
--- a/HybirdFrameworkCore/Configuration/AppSettingsHelper.cs
+++ b/HybirdFrameworkCore/Configuration/AppSettingsHelper.cs
@@ -1,4 +1,5 @@
-using Microsoft.Extensions.Configuration;
+using log4net;
+using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Configuration.Json;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
@@ -10,11 +11,8 @@ namespace HybirdFrameworkCore.Configuration;
///
public class AppSettingsHelper
{
- public AppSettingsHelper(string contentPath)
- {
- }
-
- private static IConfiguration Configuration { get; set; }
+ private static readonly ILog Log = LogManager.GetLogger(typeof(AppSettingsHelper));
+ private static IConfiguration? Configuration { get; set; }
///
/// 封装要操作的字符
@@ -22,21 +20,35 @@ public class AppSettingsHelper
///
/// 节点配置
///
- public static string GetContent(params string[] sections)
+ public static string? GetContent(params string[] sections)
{
try
{
- var contentPath = AppDomain.CurrentDomain.BaseDirectory;
- var Path = "appsettings.json";
- Configuration = new ConfigurationBuilder().SetBasePath(contentPath).Add(new JsonConfigurationSource
- { Path = Path, Optional = false, ReloadOnChange = true }).Build();
+ if (Configuration == null)
+ {
+ string? activeProfile = Environment.GetEnvironmentVariable("profiles_active");
+ if (activeProfile == null)
+ {
+ activeProfile = "";
+ }
+ else
+ {
+ activeProfile += ".";
+ }
+ var path = "appsettings."+ activeProfile+ "json";
+ Log.Info($"read path={path}");
+ var contentPath = AppDomain.CurrentDomain.BaseDirectory;
+ Configuration = new ConfigurationBuilder().SetBasePath(contentPath).Add(new JsonConfigurationSource
+ { Path = path, Optional = false, ReloadOnChange = true }).Build();
+ }
+
if (sections.Any()) return Configuration[string.Join(":", sections)];
}
catch (Exception)
{
}
- return "";
+ return null;
}
public static bool SetContent(string value, params string[] sections)
@@ -44,9 +56,18 @@ public class AppSettingsHelper
try
{
var contentPath = AppDomain.CurrentDomain.BaseDirectory;
- var Path = contentPath + "appsettings.json";
+ string? activeProfile = Environment.GetEnvironmentVariable("profiles_active");
+ if (activeProfile == null)
+ {
+ activeProfile = "";
+ }
+ else
+ {
+ activeProfile += ".";
+ }
+ var path = "appsettings."+ activeProfile+ "json";
JObject jsonObject;
- using (var file = new StreamReader(Path))
+ using (var file = new StreamReader(path))
using (var reader = new JsonTextReader(file))
{
jsonObject = (JObject)JToken.ReadFrom(reader);
@@ -54,7 +75,7 @@ public class AppSettingsHelper
jsonObject[sections[0]][sections[1]] = value;
}
- using (var writer = new StreamWriter(Path))
+ using (var writer = new StreamWriter(path))
using (var jsonwriter = new JsonTextWriter(writer))
{
jsonObject.WriteTo(jsonwriter);
@@ -67,4 +88,24 @@ public class AppSettingsHelper
return true;
}
+
+ ///
+ ///
+ ///
+ ///
+ ///
+ public static bool GetBool(params string[] sections)
+ {
+ string s = GetContent(sections);
+ if (string.IsNullOrWhiteSpace(s))
+ {
+ return false;
+ }
+ if (bool.TryParse(s, out var result))
+ {
+ return result;
+ }
+
+ return false;
+ }
}
\ No newline at end of file
diff --git a/WebStarter/Program.cs b/WebStarter/Program.cs
index efc00d2..8b30de2 100644
--- a/WebStarter/Program.cs
+++ b/WebStarter/Program.cs
@@ -4,11 +4,13 @@ using HybirdFrameworkCore.Autofac;
using HybirdFrameworkCore.Configuration;
using HybirdFrameworkCore.Entity;
using HybirdFrameworkCore.Redis;
+using log4net;
using Service.Car.Server;
using SqlSugar;
using SqlSugar.IOC;
var builder = WebApplication.CreateBuilder(args);
+var log = LogManager.GetLogger(typeof(Program));
builder.Host.UseServiceProviderFactory(new AutofacServiceProviderFactory());
builder.Host.ConfigureContainer(cb =>
@@ -60,15 +62,11 @@ builder.Services.AddControllers().AddJsonOptions(configure =>
configure.JsonSerializerOptions.Converters.Add(new DatetimeJsonConverter());
});
//redis
-var section = builder.Configuration.GetSection("Redis");
-//连接字符串
-var redisConnectionString = section.GetSection("Connection").Value;
-//实例名称
-var instanceName = section.GetSection("InstanceName").Value;
-//默认数据库
-var _defaultDB = int.Parse(section.GetSection("DefaultDB").Value ?? "0");
+var redisConnectionString = AppSettingsHelper.GetContent("Redis", "Connection");
+var instanceName = AppSettingsHelper.GetContent("Redis", "InstanceName");//默认数据库
+var defaultDb = int.Parse(AppSettingsHelper.GetContent("Redis", "DefaultDB") ?? "0");
if (redisConnectionString != null && instanceName != null)
- builder.Services.AddSingleton(new RedisHelper(redisConnectionString, instanceName, _defaultDB));
+ builder.Services.AddSingleton(new RedisHelper(redisConnectionString, instanceName, defaultDb));
builder.Services.AddControllers();
@@ -80,12 +78,10 @@ var app = builder.Build();
// Configure the HTTP request pipeline.
-if (app.Environment.IsDevelopment())
-{
- app.UseSwagger();
- app.UseSwaggerUI();
- app.UseStaticFiles();
-}
+// Configure the HTTP request pipeline.
+app.UseSwagger();
+app.UseSwaggerUI();
+app.UseStaticFiles();
app.UseAuthorization();
app.UseCors("myCors");
@@ -94,6 +90,7 @@ app.MapControllers();
var list = AppSettingsHelper.GetContent("Kestrel", "Endpoints", "http", "Url");
foreach (var s in list.Split(";"))
{
+ log.Info($"use url={s}");
app.Urls.Add(s);
}
diff --git a/WebStarter/Properties/launchSettings.json b/WebStarter/Properties/launchSettings.json
index e7e364f..b37c499 100644
--- a/WebStarter/Properties/launchSettings.json
+++ b/WebStarter/Properties/launchSettings.json
@@ -16,7 +16,8 @@
"launchUrl": "swagger",
"applicationUrl": "http://localhost:5034",
"environmentVariables": {
- "ASPNETCORE_ENVIRONMENT": "Development"
+ "ASPNETCORE_ENVIRONMENT": "Development",
+ "profiles_active": "dev"
}
},
"IIS Express": {
@@ -24,7 +25,8 @@
"launchBrowser": true,
"launchUrl": "swagger",
"environmentVariables": {
- "ASPNETCORE_ENVIRONMENT": "Development"
+ "ASPNETCORE_ENVIRONMENT": "Development",
+ "profiles_active": "dev"
}
}
}
diff --git a/WebStarter/WebStarter.csproj b/WebStarter/WebStarter.csproj
index df95a28..e90e535 100644
--- a/WebStarter/WebStarter.csproj
+++ b/WebStarter/WebStarter.csproj
@@ -29,7 +29,7 @@
-
+
Always
diff --git a/WebStarter/appsettings.Development.json b/WebStarter/appsettings.Development.json
deleted file mode 100644
index 0c208ae..0000000
--- a/WebStarter/appsettings.Development.json
+++ /dev/null
@@ -1,8 +0,0 @@
-{
- "Logging": {
- "LogLevel": {
- "Default": "Information",
- "Microsoft.AspNetCore": "Warning"
- }
- }
-}
diff --git a/WebStarter/bin/Debug/net6.0/appsettings.json b/WebStarter/appsettings.dev.json
similarity index 70%
rename from WebStarter/bin/Debug/net6.0/appsettings.json
rename to WebStarter/appsettings.dev.json
index 520ea7e..42adf99 100644
--- a/WebStarter/bin/Debug/net6.0/appsettings.json
+++ b/WebStarter/appsettings.dev.json
@@ -2,7 +2,7 @@
"ConnectionStrings": {
"ConfigId": "master",
"DbType": "MySql",
- "SqlConnection": "server=localhost;Port=3306;Database=winformdevframework;Uid=root;Pwd=123456;Charset=utf8;"
+ "SqlConnection": "server=192.168.2.2;Port=3306;Database=huanneng_dev;Uid=root;Pwd=Rszn123;Charset=utf8;"
},
"Update": {
"AutoUpdate": "false",
@@ -23,5 +23,12 @@
"Listen": {
"Port": 5588
},
+ "Kestrel": {
+ "Endpoints": {
+ "http": {
+ "Url": "http://*:15036"
+ }
+ }
+ },
"AllowedHosts": "*"
}
diff --git a/WebStarter/appsettings.json b/WebStarter/appsettings.json
index 3ef6773..0c208ae 100644
--- a/WebStarter/appsettings.json
+++ b/WebStarter/appsettings.json
@@ -1,34 +1,8 @@
{
- "ConnectionStrings": {
- "ConfigId": "master",
- "DbType": "MySql",
- "SqlConnection": "server=localhost;Port=3306;Database=winformdevframework;Uid=root;Pwd=123456;Charset=utf8;"
- },
- "Update": {
- "AutoUpdate": "false",
- "Version": "1.1.0.1",
- "Url": "http://1:8090/Updates/AutoUpdaterStarter.xml"
- },
- "Redis": {
- "Connection": "1:6379,password=123456",
- "InstanceName": "local",
- "DefaultDB": "8"
- },
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
- },
- "Listen": {
- "Port": 5588
- },
- "Kestrel": {
- "Endpoints": {
- "http": {
- "Url": "http://0.0.0.0:5036"
- }
- }
- },
- "AllowedHosts": "*"
+ }
}
diff --git a/WebStarter/appsettings.prod.json b/WebStarter/appsettings.prod.json
new file mode 100644
index 0000000..16fcaa2
--- /dev/null
+++ b/WebStarter/appsettings.prod.json
@@ -0,0 +1,34 @@
+{
+ "ConnectionStrings": {
+ "ConfigId": "master",
+ "DbType": "MySql",
+ "SqlConnection": "server=localhost;Port=3306;Database=huanneng_dev;Uid=root;Pwd=123456;Charset=utf8;"
+ },
+ "Update": {
+ "AutoUpdate": "false",
+ "Version": "1.1.0.1",
+ "Url": "http://1:8090/Updates/AutoUpdaterStarter.xml"
+ },
+ "Redis": {
+ "Connection": "localhost:6379,password=123456",
+ "InstanceName": "local",
+ "DefaultDB": "8"
+ },
+ "Logging": {
+ "LogLevel": {
+ "Default": "Information",
+ "Microsoft.AspNetCore": "Warning"
+ }
+ },
+ "Listen": {
+ "Port": 5588
+ },
+ "Kestrel": {
+ "Endpoints": {
+ "http": {
+ "Url": "http://*:5036"
+ }
+ }
+ },
+ "AllowedHosts": "*"
+}