Add database support

This commit is contained in:
jvyden 2021-10-06 12:33:59 -04:00
parent f7c4f09fc7
commit b3532fa57d
No known key found for this signature in database
GPG key ID: 18BCF2BE0262B278
7 changed files with 77 additions and 4 deletions

View file

@ -0,0 +1,11 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="Development Database" type="docker-deploy" factoryName="docker-compose.yml" server-name="Docker">
<deployment type="docker-compose.yml">
<settings>
<option name="envFilePath" value="" />
<option name="sourceFilePath" value="docker-compose.yml" />
</settings>
</deployment>
<method v="2" />
</configuration>
</component>

View file

@ -0,0 +1,12 @@
using System;
using Microsoft.EntityFrameworkCore;
using ProjectLighthouse.Types;
namespace ProjectLighthouse {
public class Database : DbContext {
protected override void OnConfiguring(DbContextOptionsBuilder options) => options.UseMySql(
ServerSettings.DbConnectionString,
MySqlServerVersion.LatestSupportedServerVersion
);
}
}

View file

@ -6,10 +6,14 @@ using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using ProjectLighthouse.Types;
namespace ProjectLighthouse {
public class Program {
public static class Program {
public static void Main(string[] args) {
Console.WriteLine("Welcome to Project Lighthouse!");
Console.WriteLine(ServerSettings.DbConnected ? "Connected to the database." : "Database unavailable. Starting in stateless mode.");
CreateHostBuilder(args).Build().Run();
}

View file

@ -6,6 +6,8 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="5.0.10" />
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="5.0.2" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="5.6.3" />
</ItemGroup>

View file

@ -20,7 +20,8 @@
"dotnetRunMessages": "true",
"applicationUrl": "http://localhost:10060;https://localhost:10061",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
"ASPNETCORE_ENVIRONMENT": "Development",
"LIGHTHOUSE_DB_CONNECTION_STRING": "server=127.0.0.1;uid=root;pwd=lighthouse;database=lighthouse"
}
}
}

View file

@ -1,10 +1,34 @@
#nullable enable
using System;
namespace ProjectLighthouse.Types {
public static class ServerSettings {
/// <summary>
/// The maximum amount of slots allowed on users' earth
/// </summary>
public static int EntitledSlots = 20;
public const int EntitledSlots = 20;
public static int ListsQuota = 20;
public const int ListsQuota = 20;
private static string? dbConnectionString;
public static string DbConnectionString {
get {
if(dbConnectionString == null) {
return dbConnectionString = Environment.GetEnvironmentVariable("LIGHTHOUSE_DB_CONNECTION_STRING") ?? "";
};
return dbConnectionString;
}
}
public static bool DbConnected {
get {
try {
return new Database().Database.CanConnect();
}
catch {
return false;
}
}
}
}
}

19
docker-compose.yml Normal file
View file

@ -0,0 +1,19 @@
# MySQL Docker compose file for development purposes. DO NOT USE IN PRODUCTION!
version: '3'
services:
lighthouse-db:
image: mysql
environment:
MYSQL_DATABASE: 'lighthouse'
MYSQL_ROOT_PASSWORD: 'lighthouse'
ports:
- '3306:3306'
expose:
- '3306' # Expose port to localhost:3306
volumes:
- lighthouse-db:/var/lib/mysql
volumes:
lighthouse-db: