Esempio n. 1
0
        public static WebApiService Instance(ApplicationManager manager)
        {
            if (instance == null)
            {
                instance = new WebApiService(manager);
            }

            return(instance);
        }
Esempio n. 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('/')),
            });
        }