-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathStartup.cs
43 lines (40 loc) · 1.78 KB
/
Startup.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
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Newtonsoft.Json;
namespace WeatherMicroservice {
public class Startup {
public void ConfigureServices (IServiceCollection services) { }
public void Configure (IApplicationBuilder app, IHostingEnvironment env) {
if (env.IsDevelopment ()) {
app.UseDeveloperExceptionPage ();
}
app.Run (async (context) => {
CultureInfo.CurrentCulture = CultureInfo.InvariantCulture;
var latString = context.Request.Query["lat"].FirstOrDefault ();
var longString = context.Request.Query["long"].FirstOrDefault ();
var latitude = latString.TryParse ();
var longitude = longString.TryParse ();
if (latitude.HasValue && longitude.HasValue) {
var forecast = new List<WeatherReport> ();
for (var days = 1; days <= 5; days++) {
forecast.Add (new WeatherReport (latitude.Value, longitude.Value, days));
}
var json = JsonConvert.SerializeObject (forecast, Formatting.Indented);
context.Response.ContentType = "application/json; charset=utf-8";
await context.Response.WriteAsync (json);
} else {
#region WriteResponse
await context.Response.WriteAsync ($"Retrieving Weather for lat: {latitude}, long: {longitude}");
#endregion
}
});
}
}
}