Example #1
0
 /// <summary>
 ///     Initializes a new instance of the <see cref="NotFoundRedirectionMiddleware"/> class.
 /// </summary>
 /// <param name="next">The middlware component which follows.</param>
 /// <param name="configuration">The active configuration for the <see cref="WebApiService"/>.</param>
 public NotFoundRedirectionMiddleware(OwinMiddleware next, WebApiServiceConfiguration configuration)
     : base(next)
 {
     Configuration = configuration;
 }
Example #2
0
        /// <summary>
        ///     Configures the <see cref="Owin"/> application.
        /// </summary>
        /// <param name="app">The <see cref="IAppBuilder"/> instance to configure.</param>
        public void Configuration(IAppBuilder app)
        {
            WebApiServiceConfiguration configuration = WebApiService.GetConfiguration();
            IRoutes routes = new Routes(configuration);

            app.Use(typeof(LoggingMiddleware), configuration);

            app.UseCors(CorsOptions.AllowAll);

            app.Use(typeof(NotFoundRedirectionMiddleware), configuration);
            app.Use(typeof(AuthenticationMiddleware), configuration);

            app.MapSignalR(routes.SignalR, new HubConfiguration());

            HttpConfiguration config = new HttpConfiguration();

            config.MapHttpAttributeRoutes(new GlobalPrefixProvider(routes.Api));

            config
            .EnableSwagger(routes.Swagger, c =>
            {
                c.RootUrl(req => ComputeHostAsSeenByOriginalClient(req));
                c.SingleApiVersion("v1", Manager.ProductName);
                c.IncludeXmlComments($"{Manager.ProductName}.XML");
                c.DescribeAllEnumsAsStrings();
                c.OperationFilter <MimeTypeOperationFilter>();
            })
            .EnableSwaggerUi(routes.SwaggerUi, c =>
            {
                Assembly containingAssembly = Assembly.GetExecutingAssembly();
                c.CustomAsset("index", containingAssembly, "OpenIIoT.Core.Service.WebApi.Swagger.Content.index.html");
                c.InjectStylesheet(containingAssembly, "OpenIIoT.Core.Service.WebApi.Swagger.Content.style.css");
                c.EnableApiKeySupport(WebApiConstants.ApiKeyHeaderName, "header");
                c.DisableValidator();
            });

            config
            .Routes.MapHttpRoute(
                name: "HelpShortcut",
                routeTemplate: routes.HelpRoot,
                defaults: null,
                constraints: null,
                handler: new RedirectHandler(SwaggerDocsConfig.DefaultRootUrlResolver, routes.Help));

            app.UseWebApi(config);

            // suppress client side caching of files ending in .html/.htm this is necessary for the hacky beta of the front end;
            // once a proper SPA is deployed this can be removed.
            app.UseStaticFiles(new StaticFileOptions()
            {
                FileSystem        = new PhysicalFileSystem(Manager.GetManager <IPlatformManager>().Directories.Web),
                RequestPath       = PathString.FromUriComponent($"/{routes.Root}".TrimEnd('/')),
                OnPrepareResponse = staticFileResponseContext =>
                {
                    string extension = Path.GetExtension(staticFileResponseContext.File.Name);
                    if (WebApiConstants.CacheSuppressedExtensions.Any(e => e == extension))
                    {
                        IHeaderDictionary headers = staticFileResponseContext.OwinContext.Response.Headers;
                        headers.Add("Cache-Control", new[] { "no-cache" });
                        headers.Add("Pragma", new[] { "no-cache" });
                        headers.Add("Expires", new[] { "-1" });
                    }
                },
            });

            app.UseFileServer(new FileServerOptions()
            {
                FileSystem  = new PhysicalFileSystem(Manager.GetManager <IPlatformManager>().Directories.Web),
                RequestPath = PathString.FromUriComponent($"/{routes.Root}".TrimEnd('/')),
            });
        }