Пример #1
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            NodeHostSettings hostSettings = fullNode.Services.ServiceProvider.GetService <NodeHostSettings>();

            // Make the configuration available to custom features.
            services.AddSingleton(this.Configuration);

            services.AddLogging(loggingBuilder =>
            {
                loggingBuilder.AddConfiguration(this.Configuration.GetSection("Logging"));
                loggingBuilder.AddConsole();
                loggingBuilder.AddDebug();
            });

            services.Configure <BlockcoreSettings>(this.Configuration.GetSection("Blockcore"));

            // Add service and create Policy to allow Cross-Origin Requests
            services.AddCors
            (
                options =>
            {
                options.AddPolicy
                (
                    "CorsPolicy",

                    builder =>
                {
                    var allowedDomains = new[] { "http://localhost", "http://localhost:4200", "http://localhost:8080" };

                    builder
                    .WithOrigins(allowedDomains)
                    .AllowAnyMethod()
                    .AllowAnyHeader()
                    .AllowCredentials();
                }
                );
            });

            if (hostSettings.EnableAuth)
            {
                services.AddAuthentication(options =>
                {
                    options.DefaultAuthenticateScheme = ApiKeyAuthenticationOptions.DefaultScheme;
                    options.DefaultChallengeScheme    = ApiKeyAuthenticationOptions.DefaultScheme;
                })
                .AddApiKeySupport(options => { });

                services.AddSingleton <IAuthorizationHandler, OnlyUsersAuthorizationHandler>();
                services.AddSingleton <IAuthorizationHandler, OnlyAdminsAuthorizationHandler>();

                services.AddSingleton <IGetApiKeyQuery, AppSettingsGetApiKeyQuery>();
            }

            if (hostSettings.EnableUI)
            {
                services.AddRazorPages();

                services.AddServerSideBlazor();

                services.Configure <RazorPagesOptions>(options =>
                {
                    // The UI elements moved under the UI folder
                    options.RootDirectory = "/UI/Pages";
                });

                services.AddBlazorModal();
            }

            if (hostSettings.EnableWS)
            {
                services.AddSignalR().AddNewtonsoftJsonProtocol(options =>
                {
                    var settings = new JsonSerializerSettings();

                    settings.Error = (sender, args) =>
                    {
                        args.ErrorContext.Handled = true;
                    };

                    Serializer.RegisterFrontConverters(settings);
                    options.PayloadSerializerSettings = settings;
                    options.PayloadSerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
                });
            }

            if (hostSettings.EnableAPI)
            {
                services.AddMvc(options =>
                {
                    options.Filters.Add(typeof(LoggingActionFilter));
                })
                // add serializers for NBitcoin objects
                .AddNewtonsoftJson(options =>
                {
                    Serializer.RegisterFrontConverters(options.SerializerSettings);
                    options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
                })
                .AddControllers(this.fullNode.Services.Features, services).AddJsonOptions(x => {
                    x.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter());
                });

                services.AddSwaggerGen(
                    options =>
                {
                    string assemblyVersion = typeof(Startup).Assembly.GetName().Version.ToString();

                    string description = "Access to the Blockcore Node features.";

                    if (hostSettings.EnableAuth)
                    {
                        description += " Authorization is enabled on this API. You must have API key to perform calls that are not public.";

                        options.AddSecurityDefinition(ApiKeyConstants.HeaderName, new OpenApiSecurityScheme
                        {
                            Description = "API key needed to access the endpoints. Node-Api-Key: YOUR_KEY",
                            In          = ParameterLocation.Header,
                            Name        = ApiKeyConstants.HeaderName,
                            Type        = SecuritySchemeType.ApiKey
                        });

                        options.AddSecurityRequirement(new OpenApiSecurityRequirement
                        {
                            {
                                new OpenApiSecurityScheme
                                {
                                    Name      = ApiKeyConstants.HeaderName,
                                    Type      = SecuritySchemeType.ApiKey,
                                    In        = ParameterLocation.Header,
                                    Reference = new OpenApiReference
                                    {
                                        Type = ReferenceType.SecurityScheme,
                                        Id   = ApiKeyConstants.HeaderName
                                    },
                                },
                                new string[] {}
                            }
                        });
                    }

                    options.SwaggerDoc("node",
                                       new OpenApiInfo
                    {
                        Title       = hostSettings.ApiTitle + " Node API",
                        Version     = assemblyVersion,
                        Description = description,
                        Contact     = new OpenApiContact
                        {
                            Name = "Blockcore",
                            Url  = new Uri("https://www.blockcore.net/")
                        }
                    });

                    SwaggerApiDocumentationScaffolder.Scaffold(options);

//#pragma warning disable CS0618 // Type or member is obsolete
//                    options.DescribeAllEnumsAsStrings();
//#pragma warning restore CS0618 // Type or member is obsolete

                    // options.DescribeStringEnumsInCamelCase();
                });

                services.AddSwaggerGenNewtonsoftSupport(); // explicit opt-in - needs to be placed after AddSwaggerGen()
            }
        }
Пример #2
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            NodeHostSettings hostSettings = fullNode.Services.ServiceProvider.GetService <NodeHostSettings>();

            services.AddLogging(loggingBuilder =>
            {
                loggingBuilder.AddConfiguration(this.Configuration.GetSection("Logging"));
                loggingBuilder.AddConsole();
                loggingBuilder.AddDebug();
            });

            // Add service and create Policy to allow Cross-Origin Requests
            services.AddCors
            (
                options =>
            {
                options.AddPolicy
                (
                    "CorsPolicy",

                    builder =>
                {
                    var allowedDomains = new[] { "http://localhost", "http://localhost:4200", "http://localhost:8080" };

                    builder
                    .WithOrigins(allowedDomains)
                    .AllowAnyMethod()
                    .AllowAnyHeader()
                    .AllowCredentials();
                }
                );
            });

            if (hostSettings.EnableUI)
            {
                services.AddRazorPages();

                services.AddServerSideBlazor();

                services.Configure <RazorPagesOptions>(options =>
                {
                    // The UI elements moved under the UI folder
                    options.RootDirectory = "/UI/Pages";
                });

                services.AddBlazorModal();
            }

            if (hostSettings.EnableWS)
            {
                services.AddSignalR().AddNewtonsoftJsonProtocol(options =>
                {
                    var settings = new JsonSerializerSettings();

                    settings.Error = (sender, args) =>
                    {
                        args.ErrorContext.Handled = true;
                    };

                    Serializer.RegisterFrontConverters(settings);
                    options.PayloadSerializerSettings = settings;
                    options.PayloadSerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
                });
            }

            if (hostSettings.EnableAPI)
            {
                services.AddMvc(options =>
                {
                    options.Filters.Add(typeof(LoggingActionFilter));
                })
                // add serializers for NBitcoin objects
                .AddNewtonsoftJson(options => {
                    Serializer.RegisterFrontConverters(options.SerializerSettings);
                    options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
                })
                .AddControllers(this.fullNode.Services.Features, services);

                services.AddSwaggerGen(
                    options =>
                {
                    string assemblyVersion = typeof(Startup).Assembly.GetName().Version.ToString();

                    options.SwaggerDoc("node",
                                       new OpenApiInfo
                    {
                        Title       = "Blockcore Node API",
                        Version     = assemblyVersion,
                        Description = "Access to the Blockcore Node features.",
                        Contact     = new OpenApiContact
                        {
                            Name = "Blockcore",
                            Url  = new Uri("https://www.blockcore.net/")
                        }
                    });

                    SwaggerApiDocumentationScaffolder.Scaffold(options);

#pragma warning disable CS0618 // Type or member is obsolete
                    options.DescribeAllEnumsAsStrings();
#pragma warning restore CS0618 // Type or member is obsolete

                    // options.DescribeStringEnumsInCamelCase();
                });

                services.AddSwaggerGenNewtonsoftSupport(); // explicit opt-in - needs to be placed after AddSwaggerGen()
            }
        }