Port Program to net6.0

Signed-off-by: GitHub <noreply@github.com>
This commit is contained in:
Esteban Solano Granados 2022-01-20 04:10:42 +00:00 committed by GitHub
parent 6803d84678
commit 73dbebdeb2

View File

@ -1,30 +1,15 @@
using System; using MySqlConnector;
using System.IO;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.Primitives;
using MySql.Data;
using MySql.Data.MySqlClient;
class Program var builder = WebApplication.CreateBuilder(args);
{
public static void Main(string[] args) => WebHost.CreateDefaultBuilder(args)
.Configure(async app =>
{
app.UseRouting();
string password = File.ReadAllText("/run/secrets/db-password"); string password = File.ReadAllText("/run/secrets/db-password");
string connectionString = $"server=db;user=root;database=example;port=3306;password={password}"; string connectionString = $"server=db;user=root;database=example;port=3306;password={password}";
app.UseEndpoints(e => builder.Services.AddTransient<MySqlConnection>((_provider) => new MySqlConnection(connectionString));
{
e.MapGet("/", context => { var app = builder.Build();
using MySqlConnection connection = new MySqlConnection(connectionString);
app.MapGet("/", (MySqlConnection connection) => {
var titles = new List<string>(); var titles = new List<string>();
try try
@ -44,23 +29,17 @@ class Program
} }
catch (Exception ex) catch (Exception ex)
{ {
Console.WriteLine(ex.ToString()); return Results.Problem(detail: ex.ToString());
context.Response.StatusCode = 500;
return Task.CompletedTask;
} }
connection.Close(); connection.Close();
context.Response.StatusCode = 200; return Results.Ok(titles);
context.Response.WriteAsJsonAsync(titles);
return Task.CompletedTask;
});
}); });
Prepare(connectionString); Prepare(connectionString);
}).Build().Run(); app.Run();
private static void Prepare(string connectionString) void Prepare(string connectionString)
{ {
using MySqlConnection connection = new MySqlConnection(connectionString); using MySqlConnection connection = new MySqlConnection(connectionString);
@ -81,4 +60,3 @@ class Program
transation.Commit(); transation.Commit();
connection.Close(); connection.Close();
} }
}