parent
0dd65e7f9a
commit
37e5c0796e
@ -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,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…
Reference in new issue