add configbuilder

This commit is contained in:
SpringHgui 2020-09-04 17:32:59 +08:00
parent 8691c01078
commit b7705878fa
7 changed files with 87 additions and 13 deletions

View File

@ -4,16 +4,16 @@ using System.Text;
namespace FastTunnel.Core.Config
{
public class ServerConfig
public class DefaultServerConfig : IServerConfig
{
public string BindAddr { get; set; }
public int BindPort { get; set; }
public int ProxyPort_HTTP { get; set; }
public int ProxyPort_HTTP { get; set; } = 1270;
public string Domain { get; set; }
public bool HasNginxProxy { get; set; }
public bool HasNginxProxy { get; set; } = false;
}
}

View File

@ -0,0 +1,50 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace FastTunnel.Core.Config
{
public class DefaultServerConfigBuilder
{
readonly DefaultServerConfig _options = new DefaultServerConfig();
public DefaultServerConfigBuilder WithBindInfo(string host, int port)
{
_options.BindAddr = host;
_options.BindPort = port;
return this;
}
public DefaultServerConfigBuilder WithWebDomain(string domain)
{
_options.Domain = domain;
return this;
}
public DefaultServerConfigBuilder WithHasNginxProxy(bool has)
{
_options.HasNginxProxy = has;
return this;
}
public DefaultServerConfigBuilder WithHTTPPort(int port)
{
_options.ProxyPort_HTTP = port;
return this;
}
public DefaultServerConfig Build()
{
if (string.IsNullOrEmpty(_options.BindAddr))
throw new ArgumentNullException("You must use WithBindInfo to set host");
if (_options.BindPort == 0)
throw new ArgumentNullException("You must use WithBindInfo to set port");
if (string.IsNullOrEmpty(_options.Domain))
throw new ArgumentNullException("You must use WithWebDomain to set domain");
return _options;
}
}
}

View File

@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace FastTunnel.Core.Config
{
public interface IServerConfig
{
string BindAddr { get; set; }
int BindPort { get; set; }
int ProxyPort_HTTP { get; set; }
string Domain { get; set; }
bool HasNginxProxy { get; set; }
}
}

View File

@ -25,7 +25,8 @@ namespace FastTunnel.Core.Core
public Dictionary<string, NewRequest> newRequest = new Dictionary<string, NewRequest>();
public Dictionary<string, WebInfo> WebList = new Dictionary<string, WebInfo>();
public Dictionary<int, SSHInfo<SSHHandlerArg>> SSHList = new Dictionary<int, SSHInfo<SSHHandlerArg>>();
public ServerConfig _serverSettings;
public IServerConfig _serverSettings { get; set; }
ILogger<FastTunnelServer> _logger;
@ -41,7 +42,7 @@ namespace FastTunnel.Core.Core
_swapMsgHandler = swapMsgHandler;
}
public void Run(ServerConfig settings)
public void Run(IServerConfig settings)
{
_serverSettings = settings;
_logger.LogDebug("FastTunnel Server Start");

View File

@ -18,6 +18,7 @@
<RepositoryType>git</RepositoryType>
<PackageTags>FastTunnel.Core</PackageTags>
<PackageReleaseNotes>FastTunnel.Core</PackageReleaseNotes>
<Version>1.0.1</Version>
</PropertyGroup>
<ItemGroup>

View File

@ -8,6 +8,6 @@ namespace FastTunnel.Server
{
public class Appsettings
{
public ServerConfig ServerSettings { get; set; }
public DefaultServerConfig ServerSettings { get; set; }
}
}

View File

@ -1,4 +1,5 @@
using FastTunnel.Core.Core;
using FastTunnel.Core.Config;
using FastTunnel.Core.Core;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
@ -29,7 +30,14 @@ namespace FastTunnel.Server.Service
try
{
Run();
var setting = _configuration.Get<Appsettings>().ServerSettings;
_fastTunnelServer.Run(
new DefaultServerConfigBuilder()
.WithBindInfo(setting.BindAddr, setting.BindPort)
.WithHasNginxProxy(setting.HasNginxProxy)
.WithWebDomain(setting.Domain)
.WithHTTPPort(setting.ProxyPort_HTTP).Build());
}
catch (Exception ex)
{
@ -41,11 +49,6 @@ namespace FastTunnel.Server.Service
return Task.CompletedTask;
}
private void Run()
{
_fastTunnelServer.Run(_configuration.Get<Appsettings>().ServerSettings);
}
public Task StopAsync(CancellationToken cancellationToken)
{
_logger.LogInformation("===== FastTunnel Server Stoping =====");