diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json index 02902f39a..f268ad936 100644 --- a/.devcontainer/devcontainer.json +++ b/.devcontainer/devcontainer.json @@ -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. diff --git a/src/DaemonRunner/DaemonRunner/NetDaemonExtensions.cs b/src/DaemonRunner/DaemonRunner/NetDaemonExtensions.cs index 8b04cb099..911bc443c 100644 --- a/src/DaemonRunner/DaemonRunner/NetDaemonExtensions.cs +++ b/src/DaemonRunner/DaemonRunner/NetDaemonExtensions.cs @@ -16,16 +16,15 @@ public static class NetDaemonExtensions { public static IHostBuilder UseNetDaemon(this IHostBuilder hostBuilder) { + return hostBuilder.ConfigureServices((context, services) => { services.Configure(context.Configuration.GetSection("HomeAssistant")); services.Configure(context.Configuration.GetSection("NetDaemon")); - }).ConfigureWebHostDefaults(webbuilder => { webbuilder.UseKestrel(options => { - options.Listen(IPAddress.Loopback, 5000); //HTTP port }); webbuilder.UseStartup(); }); diff --git a/src/DaemonRunner/DaemonRunner/Service/ApiService.cs b/src/DaemonRunner/DaemonRunner/Service/ApiService.cs index 42044c17b..004d2fa97 100644 --- a/src/DaemonRunner/DaemonRunner/Service/ApiService.cs +++ b/src/DaemonRunner/DaemonRunner/Service/ApiService.cs @@ -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; } @@ -35,27 +40,36 @@ public void ConfigureServices(IServiceCollection services) services.AddTransient(); services.AddSingleton(); 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(); + }); + } } } } diff --git a/src/DaemonRunner/DaemonRunner/Service/Configuration/NetDaemonSettings.cs b/src/DaemonRunner/DaemonRunner/Service/Configuration/NetDaemonSettings.cs index ad602a6e2..4d4b421cf 100644 --- a/src/DaemonRunner/DaemonRunner/Service/Configuration/NetDaemonSettings.cs +++ b/src/DaemonRunner/DaemonRunner/Service/Configuration/NetDaemonSettings.cs @@ -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; } diff --git a/src/Service/appsettings.json b/src/Service/appsettings.json index 781aecad8..6d4b48701 100644 --- a/src/Service/appsettings.json +++ b/src/Service/appsettings.json @@ -9,8 +9,9 @@ "Token": "" }, "NetDaemon": { + "Admin": false, "SourceFolder": "", "GenerateEntities": false, - "ProjectFolder": "" + "ProjectFolder": "" } } \ No newline at end of file