-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
57 lines (47 loc) · 1.53 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
using Microsoft.Extensions.FileProviders;
using System.Text.Json;
using WeatherPlugin;
const string WEATHER_API_URL = "https://api.open-meteo.com/v1/forecast?";
var builder = WebApplication.CreateBuilder(args);
// Add services and its settings.
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddCors(options =>
{
options.AddPolicy("AllowAll", policy =>
{
policy.WithOrigins(
"https://chat.openai.com",
"http://localhost:5139")
.AllowAnyHeader()
.AllowAnyMethod();
});
});
var app = builder.Build();
app.UseCors("AllowAll");
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseStaticFiles(new StaticFileOptions
{
FileProvider = new PhysicalFileProvider(
Path.Combine(Directory.GetCurrentDirectory(), ".well-known")),
RequestPath = "/.well-known"
});
// Add weather-forecast endpoint.
app.MapGet("/weather-forecast", async (double latitude, double longitude) =>
{
using (var httpClient = new HttpClient())
{
var queryParams = $"latitude={latitude}&longitude={longitude}¤t_weather=true";
var url = WEATHER_API_URL + queryParams;
var result = await httpClient.GetStringAsync(url);
var jsonDocument = JsonDocument.Parse(result);
var currentWeather = jsonDocument.RootElement.GetProperty("current_weather");
return JsonSerializer.Deserialize<GetWeatherResponse>(currentWeather);
}
})
.WithName("getWeather");
app.Run();