Skip to content

Commit 653796c

Browse files
committed
Allow specifying http port and cors settings in local.settings.json. Closes #79 Closes #133
1 parent 202a47b commit 653796c

File tree

5 files changed

+37
-2
lines changed

5 files changed

+37
-2
lines changed

src/Azure.Functions.Cli/Actions/HostActions/StartHostAction.cs

+11-2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
using Microsoft.Azure.WebJobs.Host;
2626
using Microsoft.Azure.WebJobs.Host.Config;
2727
using Microsoft.Azure.WebJobs.Extensions.Http;
28+
using Azure.Functions.Cli.Interfaces;
2829

2930
namespace Azure.Functions.Cli.Actions.HostActions
3031
{
@@ -35,6 +36,7 @@ internal class StartHostAction : BaseAction, IDisposable
3536
const int DefaultPort = 7071;
3637
const TraceLevel DefaultDebugLevel = TraceLevel.Info;
3738
const int DefaultTimeout = 20;
39+
private readonly ISecretsManager _secretsManager;
3840

3941
public int Port { get; set; }
4042

@@ -50,12 +52,19 @@ internal class StartHostAction : BaseAction, IDisposable
5052

5153
public DebuggerType Debugger { get; set; }
5254

55+
public StartHostAction(ISecretsManager secretsManager)
56+
{
57+
this._secretsManager = secretsManager;
58+
}
59+
5360
public override ICommandLineParserResult ParseArgs(string[] args)
5461
{
62+
var hostSettings = _secretsManager.GetHostStartSettings();
63+
5564
Parser
5665
.Setup<int>('p', "port")
5766
.WithDescription($"Local port to listen on. Default: {DefaultPort}")
58-
.SetDefault(DefaultPort)
67+
.SetDefault(hostSettings.LocalHttpPort == default(int) ? DefaultPort : hostSettings.LocalHttpPort)
5968
.Callback(p => Port = p);
6069

6170
Parser
@@ -73,7 +82,7 @@ public override ICommandLineParserResult ParseArgs(string[] args)
7382
Parser
7483
.Setup<string>("cors")
7584
.WithDescription($"A comma separated list of CORS origins with no spaces. Example: https://functions.azure.com,https://functions-staging.azure.com")
76-
.SetDefault(string.Empty)
85+
.SetDefault(hostSettings.Cors ?? string.Empty)
7786
.Callback(c => CorsOrigins = c);
7887

7988
Parser

src/Azure.Functions.Cli/Azure.Functions.Cli.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -607,6 +607,7 @@
607607
<Compile Include="Common\ExitCodes.cs" />
608608
<Compile Include="Common\FileSystemHelpers.cs" />
609609
<Compile Include="Common\FunctionNotFoundException.cs" />
610+
<Compile Include="Common\HostStartSettings.cs" />
610611
<Compile Include="Common\OutputTheme.cs" />
611612
<Compile Include="Common\PersistentSettings.cs" />
612613
<Compile Include="Common\ProcessInfo.cs" />
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using Newtonsoft.Json;
2+
3+
namespace Azure.Functions.Cli.Common
4+
{
5+
internal class HostStartSettings
6+
{
7+
[JsonProperty(DefaultValueHandling = DefaultValueHandling.Ignore)]
8+
public int LocalHttpPort { get; set; }
9+
10+
[JsonProperty("CORS", DefaultValueHandling = DefaultValueHandling.Ignore)]
11+
public string Cors { get; set; }
12+
}
13+
}

src/Azure.Functions.Cli/Common/SecretsManager.cs

+10
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,12 @@ public void DeleteSecret(string name)
140140
settingsFile.Commit();
141141
}
142142

143+
public HostStartSettings GetHostStartSettings()
144+
{
145+
var settingsFile = new AppSettingsFile(AppSettingsFilePath);
146+
return settingsFile.Host ?? new HostStartSettings();
147+
}
148+
143149
public void DeleteConnectionString(string name)
144150
{
145151
var settingsFile = new AppSettingsFile(AppSettingsFilePath);
@@ -161,6 +167,7 @@ public AppSettingsFile(string filePath)
161167
IsEncrypted = appSettings.IsEncrypted;
162168
Values = appSettings.Values;
163169
ConnectionStrings = appSettings.ConnectionStrings;
170+
Host = appSettings.Host;
164171
}
165172
catch
166173
{
@@ -174,6 +181,9 @@ public AppSettingsFile(string filePath)
174181
public Dictionary<string, string> Values { get; set; } = new Dictionary<string, string>();
175182
public Dictionary<string, string> ConnectionStrings { get; set; } = new Dictionary<string, string>();
176183

184+
[JsonProperty(DefaultValueHandling = DefaultValueHandling.Ignore)]
185+
public HostStartSettings Host { get; set; }
186+
177187
public void SetSecret(string name, string value)
178188
{
179189
if (IsEncrypted)

src/Azure.Functions.Cli/Interfaces/ISecretsManager.cs

+2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Collections.Generic;
2+
using Azure.Functions.Cli.Common;
23

34
namespace Azure.Functions.Cli.Interfaces
45
{
@@ -12,5 +13,6 @@ internal interface ISecretsManager
1213
void EncryptSettings();
1314
void DeleteSecret(string name);
1415
void DeleteConnectionString(string name);
16+
HostStartSettings GetHostStartSettings();
1517
}
1618
}

0 commit comments

Comments
 (0)