Restructure Project

master
exsersewo 4 years ago
parent 0dd65e7f9a
commit 37e5c0796e
  1. 46
      src/RestAPI.sln
  2. 0
      src/RestAPI/Clients/FirebirdClient.cs
  3. 0
      src/RestAPI/Clients/MongoDbClient.cs
  4. 0
      src/RestAPI/Clients/MySqlClient.cs
  5. 0
      src/RestAPI/Clients/NpgsqlClient.cs
  6. 0
      src/RestAPI/Clients/SQLiteClient.cs
  7. 0
      src/RestAPI/Controllers/TableController.cs
  8. 0
      src/RestAPI/ExceptionFilters/HttpResponseExceptionFilter.cs
  9. 0
      src/RestAPI/Exceptions/HttpResponseException.cs
  10. 0
      src/RestAPI/Interfaces/IDatabaseClient.cs
  11. 0
      src/RestAPI/OutputFormatters/HtmlOutputFormatter.cs
  12. 54
      src/RestAPI/Program.cs
  13. 0
      src/RestAPI/Properties/launchSettings.json
  14. 23
      src/RestAPI/RestAPI.csproj
  15. 25
      src/RestAPI/RestAPI.sln
  16. 7
      src/RestAPI/RestAPI/.env.default
  17. 27
      src/RestAPI/RestAPI/Program.cs
  18. 18
      src/RestAPI/RestAPI/RestAPI.csproj
  19. 205
      src/RestAPI/RestAPI/Startup.cs
  20. 223
      src/RestAPI/Startup.cs
  21. 0
      src/RestAPI/appsettings.Development.json
  22. 0
      src/RestAPI/appsettings.json

@ -0,0 +1,46 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.30615.102
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RestAPI", "RestAPI\RestAPI.csproj", "{3929B199-3AEB-41BA-993B-DA1AF2A73CA3}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{197287CE-CDEF-4FF8-9846-EFF913496E1A}"
ProjectSection(SolutionItems) = preProject
..\.env.default = ..\.env.default
EndProjectSection
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Docker", "Docker", "{98D984C1-F9F9-4D41-8CDA-4D87F5C0A774}"
ProjectSection(SolutionItems) = preProject
..\docker-compose.yml = ..\docker-compose.yml
RestAPI\Dockerfile = RestAPI\Dockerfile
EndProjectSection
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Github", "Github", "{766907EE-8F38-477E-BD1D-F34EA54AC784}"
ProjectSection(SolutionItems) = preProject
..\.github\LICENSE = ..\.github\LICENSE
..\.github\README = ..\.github\README
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{3929B199-3AEB-41BA-993B-DA1AF2A73CA3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3929B199-3AEB-41BA-993B-DA1AF2A73CA3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3929B199-3AEB-41BA-993B-DA1AF2A73CA3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3929B199-3AEB-41BA-993B-DA1AF2A73CA3}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{98D984C1-F9F9-4D41-8CDA-4D87F5C0A774} = {197287CE-CDEF-4FF8-9846-EFF913496E1A}
{766907EE-8F38-477E-BD1D-F34EA54AC784} = {197287CE-CDEF-4FF8-9846-EFF913496E1A}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {972DCCF9-29C3-427D-82B0-1DA966964D24}
EndGlobalSection
EndGlobal

@ -0,0 +1,54 @@
using dotenv.net;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;
using System;
using System.Collections.Generic;
using System.IO;
namespace RestAPI
{
public class Program
{
public static void Main(string[] args)
{
string envFile = Path.Combine(AppContext.BaseDirectory, ".env");
DotEnv.Config(false, filePath: envFile);
CreateHostBuilder(args).Build().Run();
}
public static string[] GetUrls(IWebHostBuilder webBuilder)
{
List<string> urls = new()
{
"http://0.0.0.0:80"
};
var httpsPort = Environment.GetEnvironmentVariable("HTTPSPORT");
if (!string.IsNullOrEmpty(httpsPort))
{
var environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
if (!string.IsNullOrEmpty(environment) && !environment.ToLowerInvariant().StartsWith("dev"))
{
throw new InvalidOperationException("HTTPS not supported yet, a work around is to use a reverse proxy");
}
if (int.TryParse(httpsPort, out int port))
{
urls.Add($"https://0.0.0.0:{port}");
}
}
return urls.ToArray();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>().UseUrls(GetUrls(webBuilder));
});
}
}

@ -0,0 +1,23 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<LangVersion>preview</LangVersion>
</PropertyGroup>
<ItemGroup>
<None Remove="Dockerfile" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="dotenv.net" Version="2.1.3" />
<PackageReference Include="FirebirdSql.Data.FirebirdClient" Version="7.10.1" />
<PackageReference Include="HtmlAgilityPack" Version="1.11.30" />
<PackageReference Include="Microsoft.Data.Sqlite.Core" Version="5.0.2" />
<PackageReference Include="MongoDB.Driver" Version="2.11.6" />
<PackageReference Include="MySqlConnector" Version="1.2.1" />
<PackageReference Include="Npgsql" Version="5.0.3" />
</ItemGroup>
</Project>

@ -1,25 +0,0 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.30615.102
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RestAPI", "RestAPI\RestAPI.csproj", "{3929B199-3AEB-41BA-993B-DA1AF2A73CA3}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{3929B199-3AEB-41BA-993B-DA1AF2A73CA3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3929B199-3AEB-41BA-993B-DA1AF2A73CA3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3929B199-3AEB-41BA-993B-DA1AF2A73CA3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3929B199-3AEB-41BA-993B-DA1AF2A73CA3}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {972DCCF9-29C3-427D-82B0-1DA966964D24}
EndGlobalSection
EndGlobal

@ -1,7 +0,0 @@
DATASOURCE=
HOST=
HOSTPORT=
USERNAME=
PASSWORD=
DATABASE=
BLACKLISTEDFIELDS=

@ -1,27 +0,0 @@
using dotenv.net;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;
using System;
using System.IO;
namespace RestAPI
{
public class Program
{
public static void Main(string[] args)
{
string envFile = Path.Combine(AppContext.BaseDirectory, ".env");
DotEnv.Config(false, filePath: envFile);
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
}
}

@ -1,18 +0,0 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="dotenv.net" Version="2.1.1" />
<PackageReference Include="FirebirdSql.Data.FirebirdClient" Version="7.10.1" />
<PackageReference Include="HtmlAgilityPack" Version="1.11.29" />
<PackageReference Include="Microsoft.Data.Sqlite.Core" Version="5.0.1" />
<PackageReference Include="MongoDB.Driver" Version="2.11.5" />
<PackageReference Include="MySqlConnector" Version="1.2.1" />
<PackageReference Include="Npgsql" Version="5.0.1.1" />
</ItemGroup>
</Project>

@ -1,205 +0,0 @@
using FirebirdSql.Data.FirebirdClient;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Data.Sqlite;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using MongoDB.Driver;
using MySqlConnector;
using Npgsql;
using RestAPI.Clients;
using RestAPI.ExceptionFilters;
using RestAPI.Interfaces;
using RestAPI.OutputFormatters;
using System;
using System.IO;
using System.Linq;
namespace RestAPI
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
string dataSource = Environment.GetEnvironmentVariable("DATASOURCE");
if (dataSource != null)
{
string serverHost = Environment.GetEnvironmentVariable("HOST");
string serverHostPort = Environment.GetEnvironmentVariable("HOSTPORT");
string username = Environment.GetEnvironmentVariable("USERNAME");
string password = Environment.GetEnvironmentVariable("PASSWORD");
string database = Environment.GetEnvironmentVariable("DATABASE");
ushort hostPort = 0;
if (!ushort.TryParse(serverHostPort, out hostPort))
{
}
switch (dataSource.ToLowerInvariant())
{
case "mysql":
case "mariadb":
{
if (hostPort == 0)
{
hostPort = 3306;
}
services.AddSingleton<IDatabaseClient>(new MySqlClient(new MySqlConnectionStringBuilder
{
Server = serverHost,
Port = hostPort,
UserID = username,
Password = password,
Database = database,
AllowUserVariables = true
}.ConnectionString));
}
break;
case "mongo":
case "mongodb":
{
if (hostPort == 0)
{
hostPort = 27017;
}
MongoClientSettings builder = new MongoClientSettings();
string prefix = "mongodb";
if (!System.Net.IPAddress.TryParse(serverHost, out _))
{
prefix += "+srv";
}
string connString;
if (!string.IsNullOrEmpty(username))
{
connString = string.Format(
"{0}://{1}:{2}@{3}:{4}/",
prefix,
username,
password,
serverHost,
hostPort
);
}
else
{
connString = string.Format(
"{0}://{1}:{2}/",
prefix,
serverHost,
hostPort
);
}
services.AddSingleton<IDatabaseClient>(new MongoDbClient(connString, database));
}
break;
case "firebird":
case "interbase":
{
//TODO: TEST FIREBASE
throw new NotImplementedException();
if (hostPort == 0)
{
hostPort = 3050;
}
services.AddSingleton<IDatabaseClient>(new FirebirdClient(new FbConnectionStringBuilder
{
DataSource = serverHost,
Port = hostPort,
UserID = username,
Password = password,
ServerType = FbServerType.Default
}.ConnectionString));
}
break;
case "pgsql":
case "postgresql":
{
if (hostPort == 0)
{
hostPort = 5432;
}
services.AddSingleton<IDatabaseClient>(new NpgsqlClient(new NpgsqlConnectionStringBuilder
{
Host = serverHost,
Port = hostPort,
Database = database,
Username = username,
Password = password
}.ConnectionString));
}
break;
case "sqlite":
{
//TODO: TEST SQLite
throw new NotImplementedException();
SqliteConnectionStringBuilder builder = new SqliteConnectionStringBuilder();
var sqliteFiles = Directory.GetFiles(AppContext.BaseDirectory, "*.sqlite", SearchOption.AllDirectories);
if (File.Exists(database))
{
builder.DataSource = database;
}
else if (!database.Contains(".sqlite"))
{
builder.DataSource = sqliteFiles.FirstOrDefault(x => x.Contains($"{database}.sqlite"));
}
services.AddSingleton<IDatabaseClient>(new SQLiteClient(builder.ConnectionString));
}
break;
}
}
services.AddControllers(options =>
{
options.Filters.Add(new HttpResponseExceptionFilter());
options.OutputFormatters.Add(new HtmlOutputFormatter());
})
.AddXmlSerializerFormatters();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseHttpsRedirection();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
}
}

@ -0,0 +1,223 @@
using FirebirdSql.Data.FirebirdClient;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Data.Sqlite;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using MongoDB.Driver;
using MySqlConnector;
using Npgsql;
using RestAPI.Clients;
using RestAPI.ExceptionFilters;
using RestAPI.Interfaces;
using RestAPI.OutputFormatters;
using System;
using System.IO;
using System.Linq;
namespace RestAPI
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
IDatabaseClient GetDatabase()
{
string dataSource = Environment.GetEnvironmentVariable("DATASOURCE");
string serverHost = Environment.GetEnvironmentVariable("HOST");
string serverHostPort = Environment.GetEnvironmentVariable("HOSTPORT");
string username = Environment.GetEnvironmentVariable("USERNAME");
string password = Environment.GetEnvironmentVariable("PASSWORD");
string database = Environment.GetEnvironmentVariable("DATABASE");
if (!ushort.TryParse(serverHostPort, out ushort hostPort))
{
switch (dataSource.ToLowerInvariant())
{
case "mysql":
case "mariadb":
hostPort = 3306;
break;
case "mongo":
case "mongodb":
hostPort = 27017;
break;
case "firebird":
case "interbase":
hostPort = 3050;
break;
case "pgsql":
case "postgresql":
hostPort = 5432;
break;
}
}
switch (dataSource.ToLowerInvariant())
{
case "mysql":
case "mariadb":
return new MySqlClient(new MySqlConnectionStringBuilder
{
Server = serverHost,
Port = hostPort,
UserID = username,
Password = password,
Database = database,
AllowUserVariables = true
}.ConnectionString);
case "mongo":
case "mongodb":
MongoClientSettings mongoBuilder = new();
string prefix = "mongodb";
if (!System.Net.IPAddress.TryParse(serverHost, out _))
{
prefix += "+srv";
}
string connString;
if (!string.IsNullOrEmpty(username))
{
connString = string.Format(
"{0}://{1}:{2}@{3}:{4}/",
prefix,
username,
password,
serverHost,
(ushort)hostPort
);
}
else
{
connString = string.Format(
"{0}://{1}:{2}/",
prefix,
serverHost,
(ushort)hostPort
);
}
return new MongoDbClient(connString, database);
case "firebird":
case "interbase":
//TODO: TEST FIREBIRD
throw new NotImplementedException();
return new FirebirdClient(new FbConnectionStringBuilder
{
DataSource = serverHost,
Port = hostPort,
UserID = username,
Password = password,
ServerType = FbServerType.Default
}.ConnectionString);
case "pgsql":
case "postgresql":
return new NpgsqlClient(new NpgsqlConnectionStringBuilder
{
Host = serverHost,
Port = hostPort,
Database = database,
Username = username,
Password = password
}.ConnectionString);
case "sqlite":
//TODO: TEST SQLite
throw new NotImplementedException();
SqliteConnectionStringBuilder sqliteBuilder = new();
var sqliteFiles = Directory.GetFiles(AppContext.BaseDirectory, "*.sqlite", SearchOption.AllDirectories);
if (File.Exists(database))
{
sqliteBuilder.DataSource = database;
}
else if (!database.Contains(".sqlite"))
{
sqliteBuilder.DataSource = sqliteFiles.FirstOrDefault(x => x.Contains($"{database}.sqlite"));
}
return new SQLiteClient(sqliteBuilder.ConnectionString);
}
return null;
}
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
string dataSource = Environment.GetEnvironmentVariable("DATASOURCE");
string httpsPort = Environment.GetEnvironmentVariable("HTTPSPORT");
if (!string.IsNullOrEmpty(dataSource))
{
var database = GetDatabase();
services.AddSingleton(database);
}
if (!string.IsNullOrEmpty(httpsPort))
{
throw new InvalidOperationException("HTTPS not supported yet, a work around is to use a reverse proxy");
if (int.TryParse(httpsPort, out int port))
{
services.AddHsts(options =>
{
options.Preload = true;
options.IncludeSubDomains = true;
options.MaxAge = TimeSpan.FromDays(60);
});
services.AddHttpsRedirection(options =>
{
options.RedirectStatusCode = StatusCodes.Status307TemporaryRedirect;
options.HttpsPort = port;
});
}
}
services.AddControllers(options =>
{
options.Filters.Add(new HttpResponseExceptionFilter());
options.OutputFormatters.Add(new HtmlOutputFormatter());
})
.AddXmlSerializerFormatters();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
string httpsPort = Environment.GetEnvironmentVariable("HTTPSPORT");
if (!string.IsNullOrEmpty(httpsPort))
{
app.UseHttpsRedirection();
}
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
}
}
Loading…
Cancel
Save