Skip to content

Commit

Permalink
[server] 크레마 서비스 등록 시 http 포트를 지정할 수 있는 기능 추가
Browse files Browse the repository at this point in the history
사용 방법
cremaserver.exe service install <repo path> --http-port 5104
  • Loading branch information
powerumc committed Aug 26, 2019
1 parent b15ae08 commit 6475f4a
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 7 deletions.
5 changes: 5 additions & 0 deletions common/Ntreev.Crema.ServiceModel/AddressUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,10 @@ public static string GetIPAddress(string address)
var match = Regex.Match(address, "(?<name>[^:]*)[:]*(?<port>\\d*)", RegexOptions.ExplicitCapture);
return match.Groups["name"].Value;
}

public static int GetDefaultHttpPort(int port)
{
return port + 100;
}
}
}
4 changes: 2 additions & 2 deletions server/Ntreev.Crema.ServiceHosts/CremaService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public int Port
public int? HttpPort
{
get => this.httpPort;
set => this.httpPort = value ?? this.port + 100;
set => this.httpPort = value ?? AddressUtility.GetDefaultHttpPort(this.port);
}

public ICremaHost CremaHost
Expand Down Expand Up @@ -233,7 +233,7 @@ private void StartWcfServices()
var port = ports[item.Schema];
var host = item.CreateInstance(port);
host.Open();
this.hosts.Add(host);
this.logService.Info(Resources.ServiceStart_Port, host.GetType().Name, port);
this.serviceInfos.Add(new ServiceInfo()
Expand Down
4 changes: 4 additions & 0 deletions server/Ntreev.Crema.WindowsServiceHost/CremaService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,13 @@ protected override void OnStart(string[] args)
var baseArgs = Environment.GetCommandLineArgs();
var path = baseArgs[1];
var port = int.Parse(baseArgs[2]);
var httpPort = int.Parse(baseArgs[3]);

this.cremaService = new CremaService()
{
BasePath = path,
Port = port,
HttpPort = httpPort
};

CremaLog.Debug("service base path : {0}", path);
Expand All @@ -79,6 +81,7 @@ protected override void OnStart(string[] args)
{
BasePath = this.cremaService.BasePath,
Port = this.cremaService.Port,
HttpPort = this.cremaService.HttpPort.Value,
RepositoryModule = this.cremaService.RepositoryModule,
RepositoryName = this.cremaService.RepositoryName,
};
Expand All @@ -94,6 +97,7 @@ protected override void OnStart(string[] args)
CremaLog.Debug("new settings");
CremaLog.Debug("service base path : {0}", settings.BasePath);
CremaLog.Debug("service port : {0}", settings.Port);
CremaLog.Debug("service http port : {0}", settings.HttpPort);
CremaLog.Debug("service repo name : {0}", settings.RepositoryName);
CremaLog.Debug("service repo module : {0}", settings.RepositoryModule);
CremaLog.Debug("=========================================================");
Expand Down
15 changes: 12 additions & 3 deletions server/Ntreev.Crema.WindowsServiceHost/ServiceCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public ServiceCommand()
}

[CommandMethod]
[CommandMethodProperty(nameof(Port), nameof(ServiceName), nameof(DisplayName), nameof(Comment))]
[CommandMethodProperty(nameof(Port), nameof(ServiceName), nameof(DisplayName), nameof(Comment), nameof(HttpPort))]
public void Install(string path)
{
var basePath = new DirectoryInfo(path).FullName;
Expand Down Expand Up @@ -91,16 +91,24 @@ public string DisplayName
set;
}

[CommandProperty]
public int? HttpPort
{
get;
set;
}

public override bool IsEnabled => Environment.OSVersion.Platform == PlatformID.Win32NT;

private void InstallService(string exeFilename, string basePath)
{
var pathArg = string.Format("/PATH={0}", basePath);
var portArg = string.Format("/PORT={0}", this.Port);
var httpPortArgs = string.Format("/HTTPPORT={0}", this.HttpPort ?? AddressUtility.GetDefaultHttpPort(this.Port));
var displayNameArg = string.Format("/DisplayName={0}", this.DisplayName);
var serviceNameArg = string.Format("/ServiceName={0}", this.ServiceName);
var comment = string.Format("/Comment={0}", this.Comment);
var commandLineOptions = new string[] { pathArg, portArg, displayNameArg, serviceNameArg, comment };
var commandLineOptions = new string[] { pathArg, portArg, displayNameArg, serviceNameArg, comment, httpPortArgs };
var installer = new AssemblyInstaller(exeFilename, commandLineOptions);

installer.Install(null);
Expand All @@ -111,9 +119,10 @@ private void UninstallService(string exeFilename, string basePath)
{
var pathArg = string.Format("/PATH={0}", basePath);
var portArg = string.Format("/PORT={0}", this.Port);
var httpPortArgs = string.Format("/HTTPPORT={0}", this.HttpPort ?? AddressUtility.GetDefaultHttpPort(this.Port));
var displayNameArg = string.Format("/DisplayName={0}", this.DisplayName);
var serviceNameArg = string.Format("/ServiceName={0}", this.ServiceName);
var commandLineOptions = new string[] { pathArg, portArg, displayNameArg, serviceNameArg };
var commandLineOptions = new string[] { pathArg, portArg, displayNameArg, serviceNameArg, httpPortArgs };
var installer = new AssemblyInstaller(exeFilename, commandLineOptions)
{
UseNewContext = true
Expand Down
7 changes: 7 additions & 0 deletions server/Ntreev.Crema.WindowsServiceHost/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,12 @@ public string RepositoryModule
get;
set;
}

[CommandProperty]
public int HttpPort
{
get;
set;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,10 @@ protected override void OnBeforeInstall(System.Collections.IDictionary savedStat

var path = this.Context.Parameters["PATH"];
var port = this.Context.Parameters["PORT"];
var httpPort = this.Context.Parameters["HTTPPORT"];
var filename = this.Context.Parameters["assemblypath"];

this.Context.Parameters["assemblypath"] = $"{filename.WrapQuot()} {path.WrapQuot()} {port}";
this.Context.Parameters["assemblypath"] = $"{filename.WrapQuot()} {path.WrapQuot()} {port} {httpPort}";

this.serviceInstaller.Description = this.Context.Parameters["Comment"];
this.serviceInstaller.DisplayName = this.Context.Parameters["DisplayName"];
Expand Down Expand Up @@ -96,9 +97,10 @@ protected override void OnBeforeUninstall(System.Collections.IDictionary savedSt

var path = this.Context.Parameters["PATH"];
var port = this.Context.Parameters["PORT"];
var httpPort = this.Context.Parameters["HTTPPORT"];
var filename = this.Context.Parameters["assemblypath"];

this.Context.Parameters["assemblypath"] = $"{filename.WrapQuot()} {path.WrapQuot()} {port}";
this.Context.Parameters["assemblypath"] = $"{filename.WrapQuot()} {path.WrapQuot()} {port} {httpPort}";

this.serviceInstaller.Description = this.Context.Parameters["Comment"];
this.serviceInstaller.DisplayName = this.Context.Parameters["DisplayName"];
Expand Down

0 comments on commit 6475f4a

Please sign in to comment.