Skip to content

Commit

Permalink
Allow specifying http port and cors settings in local.settings.json. C…
Browse files Browse the repository at this point in the history
…loses #79 Closes #133
  • Loading branch information
ahmelsayed committed May 8, 2017
1 parent 202a47b commit 653796c
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/Azure.Functions.Cli/Actions/HostActions/StartHostAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
using Microsoft.Azure.WebJobs.Host;
using Microsoft.Azure.WebJobs.Host.Config;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Azure.Functions.Cli.Interfaces;

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

public int Port { get; set; }

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

public DebuggerType Debugger { get; set; }

public StartHostAction(ISecretsManager secretsManager)
{
this._secretsManager = secretsManager;
}

public override ICommandLineParserResult ParseArgs(string[] args)
{
var hostSettings = _secretsManager.GetHostStartSettings();

Parser
.Setup<int>('p', "port")
.WithDescription($"Local port to listen on. Default: {DefaultPort}")
.SetDefault(DefaultPort)
.SetDefault(hostSettings.LocalHttpPort == default(int) ? DefaultPort : hostSettings.LocalHttpPort)
.Callback(p => Port = p);

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

Parser
Expand Down
1 change: 1 addition & 0 deletions src/Azure.Functions.Cli/Azure.Functions.Cli.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -607,6 +607,7 @@
<Compile Include="Common\ExitCodes.cs" />
<Compile Include="Common\FileSystemHelpers.cs" />
<Compile Include="Common\FunctionNotFoundException.cs" />
<Compile Include="Common\HostStartSettings.cs" />
<Compile Include="Common\OutputTheme.cs" />
<Compile Include="Common\PersistentSettings.cs" />
<Compile Include="Common\ProcessInfo.cs" />
Expand Down
13 changes: 13 additions & 0 deletions src/Azure.Functions.Cli/Common/HostStartSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using Newtonsoft.Json;

namespace Azure.Functions.Cli.Common
{
internal class HostStartSettings
{
[JsonProperty(DefaultValueHandling = DefaultValueHandling.Ignore)]
public int LocalHttpPort { get; set; }

[JsonProperty("CORS", DefaultValueHandling = DefaultValueHandling.Ignore)]
public string Cors { get; set; }
}
}
10 changes: 10 additions & 0 deletions src/Azure.Functions.Cli/Common/SecretsManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,12 @@ public void DeleteSecret(string name)
settingsFile.Commit();
}

public HostStartSettings GetHostStartSettings()
{
var settingsFile = new AppSettingsFile(AppSettingsFilePath);
return settingsFile.Host ?? new HostStartSettings();
}

public void DeleteConnectionString(string name)
{
var settingsFile = new AppSettingsFile(AppSettingsFilePath);
Expand All @@ -161,6 +167,7 @@ public AppSettingsFile(string filePath)
IsEncrypted = appSettings.IsEncrypted;
Values = appSettings.Values;
ConnectionStrings = appSettings.ConnectionStrings;
Host = appSettings.Host;
}
catch
{
Expand All @@ -174,6 +181,9 @@ public AppSettingsFile(string filePath)
public Dictionary<string, string> Values { get; set; } = new Dictionary<string, string>();
public Dictionary<string, string> ConnectionStrings { get; set; } = new Dictionary<string, string>();

[JsonProperty(DefaultValueHandling = DefaultValueHandling.Ignore)]
public HostStartSettings Host { get; set; }

public void SetSecret(string name, string value)
{
if (IsEncrypted)
Expand Down
2 changes: 2 additions & 0 deletions src/Azure.Functions.Cli/Interfaces/ISecretsManager.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using Azure.Functions.Cli.Common;

namespace Azure.Functions.Cli.Interfaces
{
Expand All @@ -12,5 +13,6 @@ internal interface ISecretsManager
void EncryptSettings();
void DeleteSecret(string name);
void DeleteConnectionString(string name);
HostStartSettings GetHostStartSettings();
}
}

0 comments on commit 653796c

Please sign in to comment.