Setup!
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using MySql.Data.MySqlClient;
|
||||
|
||||
namespace Aberwyn.Controllers
|
||||
{
|
||||
[ApiController]
|
||||
[Route("api/setup")]
|
||||
public class SetupApiController : ControllerBase
|
||||
{
|
||||
[HttpPost("testdb")]
|
||||
public IActionResult TestDbConnection([FromBody] DbTestRequest request)
|
||||
{
|
||||
try
|
||||
{
|
||||
var baseConnStr = $"server={request.Host};port={request.Port};user={request.User};password={request.Pass};";
|
||||
|
||||
var testDbName = $"testcheck_{Guid.NewGuid():N}".Substring(0, 12); // säker tillfällig databas
|
||||
|
||||
using (var conn = new MySqlConnection(baseConnStr + "database=information_schema;"))
|
||||
{
|
||||
conn.Open();
|
||||
|
||||
// Försök skapa en temporär databas
|
||||
var createCmd = new MySqlCommand($"CREATE DATABASE `{testDbName}`", conn);
|
||||
createCmd.ExecuteNonQuery();
|
||||
|
||||
// Och radera den direkt
|
||||
var dropCmd = new MySqlCommand($"DROP DATABASE `{testDbName}`", conn);
|
||||
dropCmd.ExecuteNonQuery();
|
||||
}
|
||||
|
||||
return Ok(new { success = true, message = "Anslutning OK och CREATE DATABASE tillåten." });
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Ok(new { success = false, message = ex.Message });
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public class DbTestRequest
|
||||
{
|
||||
public string Host { get; set; }
|
||||
public string Port { get; set; }
|
||||
public string Db { get; set; }
|
||||
public string User { get; set; }
|
||||
public string Pass { get; set; }
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user