WeatherDashboard/WeatherDashboard.Console/Program.cs

40 lines
1.4 KiB
C#

using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using System.Net.Http.Json;
using WeatherDashboard;
var builder = Host.CreateDefaultBuilder(args);
builder.ConfigureAppConfiguration((context, config) =>
{
var environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
config.AddEnvironmentVariables();
config.AddJsonFile("appsettings.json");
config.AddJsonFile($"appsettings.{environment}.json", true);
});
builder.ConfigureServices(services =>
{
services.AddHttpClient();
});
var app = builder.Build();
var configuration = app.Services.GetService<IConfiguration>();
var httpClientFactory = app.Services.GetService<IHttpClientFactory>();
var httpClient = httpClientFactory.CreateClient();
var apiUrl = configuration["ApiUrl"];
var gotifyUrl = configuration["GotifyUrl"];
var weatherSet = await httpClient.GetFromJsonAsync<WeatherSet>(apiUrl);
var todaysForecast = weatherSet.Forecast.FirstOrDefault().Summary;
var notificationResponse = await httpClient.PostAsJsonAsync(gotifyUrl, new
{
title = "Weather Conditions",
message = $"Today: {todaysForecast.ConditionName} with a high of {todaysForecast.HighTemp} and a low of {todaysForecast.LowTemp}. Currently: {weatherSet.Current.ConditionName} and {weatherSet.Current.Temperature} degrees.",
priority = 5
});