Exemplo n.º 1
0
        public async Task Invoke(HttpContext httpContext,
                                 AppDataConnection dataConnection,
                                 IHostApplicationLifetime appLifetime,
                                 ILogger <FirstRunMiddleware> logger)
        {
            var initFlag = AppDomain.CurrentDomain.GetData(Token);

            if (initFlag is not null)
            {
                if ((bool)initFlag)
                {
                    await _next(httpContext);

                    return;
                }
            }

            if (!dataConnection.TestDatabaseConnection(exception =>
            {
                logger?.LogCritical(exception, $"Error {nameof(dataConnection.TestDatabaseConnection)}, connection string: {dataConnection.ConnectionString}");
            }))
            {
                httpContext.Response.StatusCode = StatusCodes.Status500InternalServerError;
                await httpContext.Response.WriteAsync("Database connection failed. Please see error log, fix it and RESTART this application.");

                appLifetime?.StopApplication();
            }
            else
            {
                if (dataConnection.IsFirstRun())
                {
                    try
                    {
                        dataConnection.SetupDatabase();
                    }
                    catch (Exception e)
                    {
                        logger.LogCritical(e, e.Message);
                        httpContext.Response.StatusCode = StatusCodes.Status500InternalServerError;
                        await httpContext.Response.WriteAsync("Error initializing first run, please check error log.");

                        appLifetime?.StopApplication();
                    }
                }

                AppDomain.CurrentDomain.SetData(Token, true);
                await _next(httpContext);
            }
        }