|
| 1 | +using System.Reflection; |
| 2 | +using JoySoftware.HomeAssistant.Client; |
| 3 | +using Microsoft.AspNetCore.Builder; |
| 4 | +using Microsoft.AspNetCore.Hosting; |
| 5 | +using Microsoft.AspNetCore.Mvc.ApplicationParts; |
| 6 | +using Microsoft.Extensions.Configuration; |
| 7 | +using Microsoft.Extensions.DependencyInjection; |
| 8 | +using NetDaemon.Daemon; |
| 9 | +using NetDaemon.Daemon.Storage; |
| 10 | +using NetDaemon.Service.Configuration; |
| 11 | +using NetDaemon.Service; |
| 12 | +using Microsoft.Extensions.Options; |
| 13 | +using System.IO; |
| 14 | +using Microsoft.Extensions.Hosting; |
| 15 | +using NetDaemon.Common; |
| 16 | + |
| 17 | +namespace NetDaemon.Service |
| 18 | +{ |
| 19 | + public class ApiStartup |
| 20 | + { |
| 21 | + public ApiStartup(IConfiguration configuration) |
| 22 | + { |
| 23 | + Configuration = configuration; |
| 24 | + } |
| 25 | + |
| 26 | + public IConfiguration Configuration { get; } |
| 27 | + |
| 28 | + public void ConfigureServices(IServiceCollection services) |
| 29 | + { |
| 30 | + // services.Configure<HomeAssistantSettings>(Context.Configuration.GetSection("HomeAssistant")); |
| 31 | + // services.Configure<NetDaemonSettings>(context.Configuration.GetSection("NetDaemon")); |
| 32 | + services.AddHostedService<RunnerService>(); |
| 33 | + services.AddTransient<IHassClient, HassClient>(); |
| 34 | + services.AddTransient<IDataRepository>(n => new DataRepository(Path.Combine(n.GetRequiredService<IOptions<NetDaemonSettings>>().Value.SourceFolder!, ".storage"))); |
| 35 | + services.AddTransient<IHttpHandler, NetDaemon.Daemon.HttpHandler>(); |
| 36 | + services.AddSingleton<NetDaemonHost>(); |
| 37 | + services.AddHttpClient(); |
| 38 | + services.AddControllers().PartManager.ApplicationParts.Add(new AssemblyPart(Assembly.GetExecutingAssembly())); |
| 39 | + services.AddRouting(); |
| 40 | + } |
| 41 | + |
| 42 | + public void Configure(IApplicationBuilder app, IWebHostEnvironment env) |
| 43 | + { |
| 44 | + if (env.IsDevelopment()) |
| 45 | + { |
| 46 | + app.UseDeveloperExceptionPage(); |
| 47 | + } |
| 48 | + |
| 49 | + // app.UseHttpsRedirection(); |
| 50 | + |
| 51 | + app.UseRouting(); |
| 52 | + |
| 53 | + // app.UseAuthorization(); |
| 54 | + |
| 55 | + app.UseEndpoints(endpoints => |
| 56 | + { |
| 57 | + endpoints.MapControllers(); |
| 58 | + }); |
| 59 | + } |
| 60 | + } |
| 61 | +} |
0 commit comments