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 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;
using MySqlConnector;
class Program
{
public static void Main(string[] args) => WebHost.CreateDefaultBuilder(args)
.Configure(async app =>
{
app.UseRouting();
var builder = WebApplication.CreateBuilder(args);
string password = File.ReadAllText("/run/secrets/db-password");
string connectionString = $"server=db;user=root;database=example;port=3306;password={password}";
app.UseEndpoints(e =>
{
e.MapGet("/", context => {
using MySqlConnection connection = new MySqlConnection(connectionString);
builder.Services.AddTransient<MySqlConnection>((_provider) => new MySqlConnection(connectionString));
var app = builder.Build();
app.MapGet("/", (MySqlConnection connection) => {
var titles = new List<string>();
try
@ -44,23 +29,17 @@ class Program
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
context.Response.StatusCode = 500;
return Task.CompletedTask;
return Results.Problem(detail: ex.ToString());
}
connection.Close();
context.Response.StatusCode = 200;
context.Response.WriteAsJsonAsync(titles);
return Task.CompletedTask;
});
return Results.Ok(titles);
});
Prepare(connectionString);
}).Build().Run();
app.Run();
private static void Prepare(string connectionString)
void Prepare(string connectionString)
{
using MySqlConnection connection = new MySqlConnection(connectionString);
@ -81,4 +60,3 @@ class Program
transation.Commit();
connection.Close();
}
}