Skip to content

Commit

Permalink
Fix disable Admin API using setting
Browse files Browse the repository at this point in the history
  • Loading branch information
helto4real committed Jul 23, 2020
1 parent 7acd957 commit ce9715f
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 16 deletions.
4 changes: 3 additions & 1 deletion .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
"HOMEASSISTANT__PORT": "${localEnv:HOMEASSISTANT__PORT}",
"LOGGING__MINIMUMLEVEL": "${localEnv:LOGGING__MINIMUMLEVEL}",
"NETDAEMON__GENERATEENTITIES": "${localEnv:NETDAEMON__GENERATEENTITIES}",
"NETDAEMON__SOURCEFOLDER": "${localEnv:NETDAEMON__SOURCEFOLDER}"
"NETDAEMON__SOURCEFOLDER": "${localEnv:NETDAEMON__SOURCEFOLDER}",
"NETDAEMON__ADMIN": "${localEnv:NETDAEMON__ADMIN}",
"ASPNETCORE_URLS": "${localEnv:ASPNETCORE_URLS}"
},
"postCreateCommand": "dotnet restore && .devcontainer/install_prettyprompt.sh",
// Uncomment the next line if you want to publish or forward any ports.
Expand Down
3 changes: 1 addition & 2 deletions src/DaemonRunner/DaemonRunner/NetDaemonExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,15 @@ public static class NetDaemonExtensions
{
public static IHostBuilder UseNetDaemon(this IHostBuilder hostBuilder)
{

return hostBuilder.ConfigureServices((context, services) =>
{
services.Configure<HomeAssistantSettings>(context.Configuration.GetSection("HomeAssistant"));
services.Configure<NetDaemonSettings>(context.Configuration.GetSection("NetDaemon"));
}).ConfigureWebHostDefaults(webbuilder =>
{
webbuilder.UseKestrel(options =>
{
options.Listen(IPAddress.Loopback, 5000); //HTTP port
});
webbuilder.UseStartup<ApiStartup>();
});
Expand Down
38 changes: 26 additions & 12 deletions src/DaemonRunner/DaemonRunner/Service/ApiService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,14 @@ namespace NetDaemon.Service
{
public class ApiStartup
{
bool _useAdmin = false;

public ApiStartup(IConfiguration configuration)
{
Configuration = configuration;

var enableAdminValue = Configuration.GetSection("NetDaemon").GetSection("Admin").Value;
bool.TryParse(enableAdminValue, out _useAdmin);
}

public IConfiguration Configuration { get; }
Expand All @@ -35,27 +40,36 @@ public void ConfigureServices(IServiceCollection services)
services.AddTransient<IHttpHandler, NetDaemon.Daemon.HttpHandler>();
services.AddSingleton<NetDaemonHost>();
services.AddHttpClient();
services.AddControllers().PartManager.ApplicationParts.Add(new AssemblyPart(Assembly.GetExecutingAssembly()));
services.AddRouting();


if (_useAdmin == true)
{
// Only enable them if
services.AddControllers().PartManager.ApplicationParts.Add(new AssemblyPart(Assembly.GetExecutingAssembly()));
services.AddRouting();
}
}

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
if (_useAdmin == true)
{
app.UseDeveloperExceptionPage();
}
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}

// app.UseHttpsRedirection();
// app.UseHttpsRedirection();

app.UseRouting();
app.UseRouting();

// app.UseAuthorization();
// app.UseAuthorization();

app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
public class NetDaemonSettings
{
public bool? GenerateEntities { get; set; } = false;
public bool? Admin { get; set; } = false;
public string? SourceFolder { get; set; } = null;
public string? ProjectFolder { get; set; } = string.Empty;
}
Expand Down
3 changes: 2 additions & 1 deletion src/Service/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
"Token": ""
},
"NetDaemon": {
"Admin": false,
"SourceFolder": "",
"GenerateEntities": false,
"ProjectFolder": ""
"ProjectFolder": ""
}
}

0 comments on commit ce9715f

Please sign in to comment.