[성현모] Config 기능 추가, Socket Recv Json, string 기능 분리
This commit is contained in:
@ -1,7 +1,23 @@
|
||||
using HubX.Library.Config;
|
||||
using HubX.Library.Socket.Session;
|
||||
using HubX.Server;
|
||||
using HubX.Server.TaskManager;
|
||||
using System.Net;
|
||||
using System.Net.Sockets;
|
||||
using SystemX.Core.Communication;
|
||||
using SystemX.Core.Services;
|
||||
|
||||
string configDir = @"../Config";
|
||||
|
||||
//raed log4net config
|
||||
if (Log4net.IsConfigLoad == true)
|
||||
{
|
||||
Log4net.WriteLine("Log4net Init Success");
|
||||
}
|
||||
else
|
||||
{
|
||||
Log4net.WriteLine("Log4net Init Failed", LogType.Error);
|
||||
}
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
@ -12,41 +28,61 @@ builder.Services.AddControllers();
|
||||
builder.Services.AddEndpointsApiExplorer();
|
||||
builder.Services.AddSwaggerGen();
|
||||
|
||||
//singleton
|
||||
builder.Services.AddSingleton<ConfigService<WebApiConfig>>();
|
||||
|
||||
//config preload
|
||||
ConfigService<WebApiConfig> preloadConfig = new ConfigService<WebApiConfig>();
|
||||
if (preloadConfig.OpenConfig($@"{configDir}/HubX.WebApiConfig.json") == true)
|
||||
{
|
||||
var config = preloadConfig.GetConfig();
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine("Config Preload Load Error.");
|
||||
return;
|
||||
}
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
//read api config and set
|
||||
string serverUrl = string.Empty;
|
||||
var configService = app.Services.GetService<ConfigService<WebApiConfig>>();
|
||||
bool isIIS = false;
|
||||
|
||||
int socketPort = 0;
|
||||
|
||||
if (configService?.OpenConfig($@"{configDir}/HubX.WebApiConfig.json") == true)
|
||||
{
|
||||
Log4net.WriteLine("WebApi Config Success.");
|
||||
var apiConfig = ConfigService<WebApiConfig>.Config;
|
||||
if (apiConfig != null)
|
||||
{
|
||||
serverUrl = $"{apiConfig.Server.Address}:{apiConfig.Server.Port}";
|
||||
isIIS = apiConfig.Server.IIS;
|
||||
|
||||
//socket
|
||||
socketPort = apiConfig.Socket.Port;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Log4net.WriteLine("WebApi Config Error.");
|
||||
return;
|
||||
}
|
||||
|
||||
// Configure the HTTP request pipeline.
|
||||
if (app.Environment.IsDevelopment())
|
||||
{
|
||||
Log4net.WriteLine($"IsDevelopment:{app.Environment.IsDevelopment()}");
|
||||
Log4net.WriteLine($"Swagger Url: {serverUrl}/swagger");
|
||||
|
||||
app.UseSwagger();
|
||||
app.UseSwaggerUI();
|
||||
}
|
||||
|
||||
Log4net.WriteLine("Run");
|
||||
Log4net.WriteLine("Custom LogLevel",LogType.DB);
|
||||
|
||||
WeatherForecast weatherForecast = new WeatherForecast();
|
||||
weatherForecast.Summary = "so hot";
|
||||
var strJson = weatherForecast.ToJson();
|
||||
|
||||
var deep = weatherForecast.DeepCopy();
|
||||
deep.Summary = "so cool";
|
||||
|
||||
var rr = strJson.ToObject<WeatherForecast>();
|
||||
|
||||
Task.Run(async() =>
|
||||
{
|
||||
await Task.Delay(2000);
|
||||
Listener _listener = new Listener();
|
||||
|
||||
// string host = Dns.GetHostName();
|
||||
IPHostEntry ipHost = Dns.GetHostEntry("127.0.0.1");
|
||||
IPAddress ipAddr = ipHost.AddressList[0];
|
||||
IPEndPoint endPoint = new IPEndPoint(ipAddr, 7777);
|
||||
|
||||
_listener.Init(endPoint, () => { return SessionManager.Instance.Generate(); });
|
||||
|
||||
Console.WriteLine("Listening...");
|
||||
});
|
||||
TaskSocket taskSocket = new TaskSocket();
|
||||
taskSocket?.Run(socketPort);
|
||||
|
||||
app.UseHttpsRedirection();
|
||||
|
||||
@ -54,4 +90,12 @@ app.UseAuthorization();
|
||||
|
||||
app.MapControllers();
|
||||
|
||||
app.Run();
|
||||
if (isIIS == true)
|
||||
{
|
||||
app.Run();
|
||||
}
|
||||
else
|
||||
{
|
||||
Log4net.WriteLine($"Operation Url: {serverUrl}");
|
||||
app.Run($"{serverUrl}");
|
||||
}
|
||||
|
||||
29
Projects/HubX/HubX.Server/TaskManager/TaskSocket.cs
Normal file
29
Projects/HubX/HubX.Server/TaskManager/TaskSocket.cs
Normal file
@ -0,0 +1,29 @@
|
||||
using HubX.Library.Socket.Session;
|
||||
using System.Net;
|
||||
using SystemX.Core.Communication;
|
||||
|
||||
namespace HubX.Server.TaskManager
|
||||
{
|
||||
public class TaskSocket
|
||||
{
|
||||
public async Task Run(int socketPort = 7777)
|
||||
{
|
||||
try
|
||||
{
|
||||
await Task.Delay(1000);
|
||||
Listener _listener = new Listener();
|
||||
IPEndPoint endPoint = new IPEndPoint(IPAddress.Any, socketPort);
|
||||
|
||||
_listener.Init(endPoint, () => { return SessionManager.Instance.Generate(); });
|
||||
|
||||
Log4net.WriteLine($"Address:{endPoint.Address}, Port:{socketPort}", LogType.SOCKET);
|
||||
Log4net.WriteLine($"Socket Listening Start", LogType.SOCKET);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Log4net.WriteLine("Socket Run Failed",LogType.Error);
|
||||
Log4net.WriteLine(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user