public DataContext CreateDbContext(string[] args)
    {
        IConfigurationRoot configuration = new ConfigurationBuilder()
                                           .SetBasePath(Directory.GetCurrentDirectory())
                                           .AddJsonFile("appsettings.json")
                                           .AddEnvironmentVariables()
                                           .Build();
        var builder          = new DbContextOptionsBuilder <DataContext>();
        var connectionString = NpgsqlUtil.buildConnectionString(configuration);

        Console.WriteLine(connectionString);
        builder.UseNpgsql(connectionString);

        return(new DataContext(builder.Options));
    }
Beispiel #2
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            var connectionString = NpgsqlUtil.buildConnectionString(_configuration);
            var envVar           = Environment.GetEnvironmentVariable("POSTGRES_DB");

            services.AddDbContext <DataContext>(options => options.UseNpgsql(connectionString));

            services
            .AddCors(options =>
            {
                options.AddPolicy("CorsPolicy",
                                  builder => builder
                                  .AllowAnyOrigin()
                                  .AllowAnyMethod()
                                  .AllowAnyHeader()
                                  );

                options.AddPolicy("signalr",
                                  builder => builder
                                  .AllowAnyMethod()
                                  .AllowAnyHeader()
                                  .AllowCredentials()
                                  .SetIsOriginAllowed(hostName => true));
            });

            services.AddSignalR().AddJsonProtocol(options =>
            {
                options.PayloadSerializerOptions.Converters
                .Add(new System.Text.Json.Serialization.JsonStringEnumConverter());
            });

            services.AddSingleton <IUserIdProvider, NameUserIdProvider>();
            services.AddControllers();

            services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies());
            services.AddControllers().AddJsonOptions(o =>
            {
                o.JsonSerializerOptions.IgnoreNullValues = true;
                o.JsonSerializerOptions.Converters.Add(new System.Text.Json.Serialization.JsonStringEnumConverter());
            });


            // configure strongly typed settings objects
            var appSettingsSection = _configuration.GetSection("AppSettings");

            services.Configure <AppSettings>(appSettingsSection);

            // configure jwt authentication
            var appSettings = appSettingsSection.Get <AppSettings>();
            var key         = Encoding.ASCII.GetBytes(appSettings.Secret);

            services.AddAuthentication(x =>
            {
                x.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
                x.DefaultChallengeScheme    = JwtBearerDefaults.AuthenticationScheme;
                x.DefaultScheme             = JwtBearerDefaults.AuthenticationScheme;
            })
            .AddJwtBearer(option =>
            {
                option.Events = new JwtBearerEvents
                {
                    OnTokenValidated  = OnTokenValidated,
                    OnMessageReceived = OnMessageReceived,
                };
                option.RequireHttpsMetadata      = false;
                option.SaveToken                 = true;
                option.TokenValidationParameters = new TokenValidationParameters
                {
                    ValidateIssuerSigningKey = true,
                    IssuerSigningKey         = new SymmetricSecurityKey(key),
                    ValidateIssuer           = false,
                    ValidateAudience         = false
                };
            });


            services.AddScoped <ITableEventService, TableEventService>();
            services.AddScoped <IUserService, UserService>();
            services.AddScoped <IPlayTableService, PlayTableService>();
            services.AddScoped <ILoggerService, LoggerService>();
        }