예제 #1
0
        public void Configure(IServiceProvider serviceProvider,
                              IApplicationBuilder app,
                              IHostingEnvironment env,
                              ILoggerFactory loggerFactory)
        {
            IDBService dbService = serviceProvider.GetService <IDBService>();

            dbService.SetupAsync().Wait();

#if DEBUG
            loggerFactory.AddConsole(LogLevel.Debug);
            loggerFactory.AddDebug();
            app.UseDeveloperExceptionPage();
#else
            loggerFactory.AddConsole(LogLevel.Information);
#endif

            // Allow certain domains for AJAX / JSON capability
            app.UseCors(builder => builder.WithOrigins(AllowedOrigins)
                        .AllowAnyHeader()
                        .AllowAnyMethod());

#if _ENABLE_CHAT_
            app.Map("/chat", wsapp => {
                wsapp.UseWebSockets(new WebSocketOptions {
                    ReplaceFeature = true
                });

                wsapp.Use(async(context, next) =>
                {
                    if (context.WebSockets.IsWebSocketRequest)
                    {
                        var webSocket = await context.WebSockets.AcceptWebSocketAsync();
                        using (var wsSession = new WebSocketSession(serviceProvider, webSocket))
                        {
                            await wsSession.Run();
                        }
                        return;
                    }
                    await next();
                });
            });
#endif

            // Redirect servers.openrct2.website to /servers
            // servers.openrct2.website
            app.Use(async(context, next) =>
            {
                string host = context.Request.Host.Value;
                if (String.Equals(host, "servers.openrct2.website", StringComparison.OrdinalIgnoreCase) ||
                    String.Equals(host, "beta-servers.openrct2.website", StringComparison.OrdinalIgnoreCase))
                {
                    string accept    = context.Request.Headers[HeaderNames.Accept];
                    string[] accepts = accept.Split(',');
                    if (accepts.Contains(MimeTypes.ApplicationJson))
                    {
                        context.Request.Path = "/servers";
                    }
                    else
                    {
                        context.Response.Redirect(MainWebsite);
                        return;
                    }
                }
                await next();
            });

            app.UseDefaultFiles();
            app.UseStaticFiles();
            app.UseApiAuthentication();
            app.UseMvc();

            // Let index redirect to main website
            app.MapWhen(context => context.Request.Path == "/", app2 =>
            {
                app2.Use((context, next) =>
                {
                    context.Response.Redirect(MainWebsite);
                    return(Task.FromResult(0));
                });
            });

            // Fallback to an empty 404
            app.Run(context =>
            {
                context.Response.StatusCode = StatusCodes.Status404NotFound;
                return(Task.FromResult(0));
            });
        }
예제 #2
0
        public void Configure(
            IServiceProvider serviceProvider,
            IApplicationBuilder app,
            IHostingEnvironment env,
            IOptions <DBOptions> dbOptions)
        {
            // Use X-Forwarded-For header for client IP address
            app.UseForwardedHeaders(
                new ForwardedHeadersOptions
            {
                ForwardedHeaders = ForwardedHeaders.All
            });

            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            // Setup / connect to the database
            if (dbOptions.Value.Host == null)
            {
                Logger.LogWarning("No database has been configured");
            }
            else
            {
                var dbService = serviceProvider.GetService <IDBService>();
                try
                {
#pragma warning disable VSTHRD002
                    dbService.SetupAsync().Wait();
#pragma warning restore VSTHRD002
                }
                catch (Exception ex)
                {
                    Logger.LogError(ex, "An error occured while setting up the database service");
                }
            }

            // Allow certain domains for AJAX / JSON capability
            app.UseCors(builder => builder.WithOrigins(AllowedOrigins)
                        .AllowAnyHeader()
                        .AllowAnyMethod());

#if _ENABLE_CHAT_
            app.Map("/chat", wsapp => {
                wsapp.UseWebSockets(new WebSocketOptions {
                    ReplaceFeature = true
                });

                wsapp.Use(async(context, next) =>
                {
                    if (context.WebSockets.IsWebSocketRequest)
                    {
                        var webSocket = await context.WebSockets.AcceptWebSocketAsync();
                        using (var wsSession = new WebSocketSession(serviceProvider, webSocket))
                        {
                            await wsSession.Run();
                        }
                        return;
                    }
                    await next();
                });
            });
#endif

            // Redirect servers.openrct2.website to /servers
            // servers.openrct2.website
            app.Use(async(context, next) =>
            {
                string host = context.Request.Host.Value;
                if (String.Equals(host, "servers.openrct2.io", StringComparison.OrdinalIgnoreCase) ||
                    String.Equals(host, "servers.openrct2.website", StringComparison.OrdinalIgnoreCase))
                {
#if REDIRECT_SERVERS_TO_HOME
                    string accept    = context.Request.Headers[HeaderNames.Accept];
                    string[] accepts = accept.Split(',');
                    if (accepts.Contains(MimeTypes.ApplicationJson))
                    {
                        context.Request.Path = "/servers";
                    }
                    else
                    {
                        context.Response.Redirect(MainWebsite);
                        return;
                    }
#else
                    context.Request.Path = "/servers";
#endif
                }
                await next();
            });

            app.UseDefaultFiles();
            app.UseStaticFiles();
            app.UseAuthentication();
            app.UseMvc();

            // Fallback to an empty 404
            app.Run(
                context =>
            {
                context.Response.StatusCode = StatusCodes.Status404NotFound;
                return(Task.CompletedTask);
            });
        }