using Microsoft.Extensions.FileProviders; using Sonex.Services.WebApi; using Sonex.Services.WebApi.EndPoints; using Sonex.Services.WebApi.Helpers; using DB = Sonex.Data.Database.DB; using System.Net; using System.Net.Sockets; try { // ========================================================================= // Konfiguracja // ========================================================================= int port = Config.GetIntValue("Port", 5000); string webRootDir = AppHelpers.NormalizePath( Config.GetStringValue("WebRootDir", "wwwroot") ); string sonexAppReleasesDir = AppHelpers.NormalizePath( Config.GetStringValue( "SonexAppReleasesDir", Path.Combine(AppContext.BaseDirectory, "sonexapp", "releases") ) ); AppHelpers.EnsureDirectoryExists(sonexAppReleasesDir); InitializeDatabase(); // ========================================================================= // Budowa hosta // ========================================================================= var options = new WebApplicationOptions { WebRootPath = webRootDir }; var builder = WebApplication.CreateBuilder(options); builder.WebHost.UseUrls($"http://*:{port}"); builder.Services.AddDirectoryBrowser(); //builder.Services.AddRazorPages(); builder.Logging.SetMinimumLevel(LogLevel.Warning); var app = builder.Build(); // ========================================================================= // Middleware // ========================================================================= app.UseRouting(); //app.UseAuthorization(); // ========================================================================= // Pliki statyczne Sonex // ========================================================================= const string releasesRequestPath = ""; const string webRootRequestPath = "/static"; var releasesProvider = new PhysicalFileProvider(sonexAppReleasesDir); app.UseStaticFiles(new StaticFileOptions { FileProvider = releasesProvider, RequestPath = releasesRequestPath }); app.UseDirectoryBrowser(new DirectoryBrowserOptions { FileProvider = releasesProvider, RequestPath = releasesRequestPath, Formatter = new BetterDirectoryFormatter() }); app.UseStaticFiles(new StaticFileOptions { FileProvider = app.Environment.WebRootFileProvider, RequestPath = webRootRequestPath }); app.UseDirectoryBrowser(new DirectoryBrowserOptions { FileProvider = app.Environment.WebRootFileProvider, RequestPath = webRootRequestPath, Formatter = new BetterDirectoryFormatter() }); // ========================================================================= // Endpoints // ========================================================================= app.MapSonexEndpoints(sonexAppReleasesDir); app.MapClientErrorLogEndpoints(); app.MapStaticAssets(); //app.MapRazorPages().WithStaticAssets(); // ========================================================================= // Informacje startowe // ========================================================================= app.Lifetime.ApplicationStarted.Register(() => { AppHelpers.ShowStartupInfo(app, port, webRootDir, sonexAppReleasesDir, SonexAppEndpoints._latest); }); app.Run(); } catch (Exception e) { Console.WriteLine(e.Message); Console.WriteLine(); Console.WriteLine(e); Console.ReadLine(); } static void InitializeDatabase() { string host = Config.GetStringValue("DB_HOST", string.Empty); int port = Config.GetIntValue("DB_PORT", 5432); string database = Config.GetStringValue("DB_DATA_BASE", string.Empty); string username = Config.GetStringValue("DB_USER_NAME", string.Empty); string password = Config.GetStringValue("DB_PASSWORD", string.Empty); int timeout = Config.GetIntValue("DB_TIMEOUT", 15); int commandTimeout = Config.GetIntValue("DB_COMMAND_TIMEOUT", 30); bool pooling = Config.GetBoolValue("DB_POOLING", true); int minPoolSize = Config.GetIntValue("DB_MIN_POOL_SIZE", 0); int maxPoolSize = Config.GetIntValue("DB_MAXPOOLSIZE", 100); bool includeErrorDetail = Config.GetBoolValue("DB_INCLUDE_ERROR_DETAIL", false); if (string.IsNullOrWhiteSpace(host)) throw new InvalidOperationException("Config 'DB_HOST' cannot be empty."); if (string.IsNullOrWhiteSpace(database)) throw new InvalidOperationException("Config 'DB_DATA_BASE' cannot be empty."); if (string.IsNullOrWhiteSpace(username)) throw new InvalidOperationException("Config 'DB_USER_NAME' cannot be empty."); if (string.IsNullOrWhiteSpace(password)) throw new InvalidOperationException("Config 'DB_PASSWORD' cannot be empty."); DB.InitPostgreSQL( host: host, port: port, database: database, username: username, password: password, applicationName: "Sonex.Services.WebApi", timeout: timeout, commandTimeout: commandTimeout, pooling: pooling, minPoolSize: minPoolSize, maxPoolSize: maxPoolSize, includeErrorDetail: includeErrorDetail); using var connection = DB.Open(); }