コード例 #1
0
ファイル: Startup.cs プロジェクト: zhangz/superdump
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            // setup path
            IConfigurationSection configurationSection = config.GetSection(nameof(SuperDumpSettings));
            IConfigurationSection binPathSection       = configurationSection.GetSection(nameof(SuperDumpSettings.BinPath));
            IEnumerable <string>  binPath = binPathSection.GetChildren().Select(s => s.Value);
            string path           = Environment.GetEnvironmentVariable("PATH");
            string additionalPath = string.Join(";", binPath);

            Environment.SetEnvironmentVariable("PATH", path + ";" + additionalPath);

            services.AddOptions();
            services.Configure <SuperDumpSettings>(config.GetSection(nameof(SuperDumpSettings)));

            var pathHelper = new PathHelper(
                configurationSection.GetValue <string>(nameof(SuperDumpSettings.DumpsDir)) ?? Path.Combine(Directory.GetCurrentDirectory(), @"../../data/dumps/"),
                configurationSection.GetValue <string>(nameof(SuperDumpSettings.UploadDir)) ?? Path.Combine(Directory.GetCurrentDirectory(), @"../../data/uploads/"),
                configurationSection.GetValue <string>(nameof(SuperDumpSettings.HangfireLocalDbDir)) ?? Path.Combine(Directory.GetCurrentDirectory(), @"../../data/hangfire/")
                );

            services.AddSingleton(pathHelper);

            var superDumpSettings = new SuperDumpSettings();

            config.GetSection(nameof(SuperDumpSettings)).Bind(superDumpSettings);

            // add ldap authentication
            if (superDumpSettings.UseLdapAuthentication)
            {
                services.AddLdapCookieAuthentication(superDumpSettings.LdapAuthenticationSettings, new LdapAuthenticationPathOptions {
                    LoginPath        = "/Login/Index/",
                    LogoutPath       = "/Login/Logout/",
                    AccessDeniedPath = "/Login/AccessDenied/"
                });
                services.AddSingleton(typeof(IAuthorizationHelper), typeof(AuthorizationHelper));
            }
            else
            {
                services.AddPoliciesForNoAuthentication();
                services.AddSingleton(typeof(IAuthorizationHelper), typeof(NoAuthorizationHelper));
            }

            services.AddAntiforgery();

            // asp.net core health checks
            services.AddHealthChecks();

            //configure DB
            if (config.GetValue <bool>("UseInMemoryHangfireStorage"))
            {
                services.AddHangfire(x => x.UseStorage(new Hangfire.MemoryStorage.MemoryStorage()));
            }
            else
            {
                string connString;
                Console.WriteLine(Directory.GetCurrentDirectory());
                using (SqlConnection conn = LocalDBAccess.GetLocalDB(config, "HangfireDB", pathHelper)) {
                    connString = conn.ConnectionString;
                }
                if (string.IsNullOrEmpty(connString))
                {
                    throw new Exception("DB could not be created, please check if LocalDB is installed.");
                }
                services.AddHangfire(x => x.UseSqlServerStorage(connString));
            }

            // set upload limit
            int maxUploadSizeMB = config.GetSection(nameof(SuperDumpSettings)).GetValue <int>(nameof(SuperDumpSettings.MaxUploadSizeMB));

            if (maxUploadSizeMB == 0)
            {
                maxUploadSizeMB = 16000;                                   // default
            }
            services.Configure <FormOptions>(opt => opt.MultipartBodyLengthLimit = 1024L * 1024L * maxUploadSizeMB);

            // Add framework services.
            services.AddMvc()
            .AddNewtonsoftJson();
            services.AddSwaggerGen();

            services.AddSwaggerGen(options => {
                options.SwaggerDoc("v1", new OpenApiInfo {
                    Version     = "v1",
                    Title       = "SuperDump API",
                    Description = "REST interface for SuperDump analysis tool",
                    Contact     = new OpenApiContact {
                        Url = new Uri("https://github.com/Dynatrace/superdump")
                    }
                });

                //Determine base path for the application.
                var basePath = PlatformServices.Default.Application.ApplicationBasePath;

                //Set the comments path for the swagger json and ui.
                var xmlDocFile = new FileInfo(Path.Combine(basePath, "SuperDumpService.xml"));
                if (xmlDocFile.Exists)
                {
                    options.IncludeXmlComments(xmlDocFile.FullName);
                }


                options.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme()
                {
                    In          = ParameterLocation.Header,
                    Description = "Please insert JWT Bearer Token into field",
                    Name        = "Authorization",
                    Type        = SecuritySchemeType.ApiKey
                });
            });

            // Add HttpErrorPolicy for ObjectDisposedException when downloading a dump file using the DownloadService
            services.AddHttpClient(DownloadService.HttpClientName, config => config.Timeout = superDumpSettings.DownloadServiceHttpClientTimeout)
            .AddTransientHttpErrorPolicy(builder => builder
                                         .OrInner <ObjectDisposedException>()
                                         .WaitAndRetryAsync(superDumpSettings.DownloadServiceRetry,
                                                            _ => TimeSpan.FromMilliseconds(superDumpSettings.DownloadServiceRetryTimeout)));

            // register repository as singleton
            services.AddSingleton <SuperDumpRepository>();

            services.AddSingleton <BundleRepository>();
            services.AddSingleton <IBundleStorage, BundleStorageFilebased>();
            services.AddSingleton <DumpRepository>();
            services.AddSingleton <IDumpStorage, DumpStorageFilebased>();
            services.AddSingleton <AnalyzerPipeline>();
            services.AddSingleton <AnalysisService>();
            services.AddSingleton <DownloadService>();
            services.AddSingleton <SymStoreService>();
            services.AddSingleton <UnpackService>();
            services.AddSingleton <NotificationService>();
            services.AddSingleton <SlackNotificationService>();
            services.AddSingleton <ElasticSearchService>();
            services.AddSingleton <DumpRetentionService>();
            services.AddSingleton <SimilarityService>();
            services.AddSingleton <RelationshipRepository>();
            services.AddSingleton <IRelationshipStorage, RelationshipStorageFilebased>();
            services.AddSingleton <IIdenticalDumpStorage, IdenticalDumpStorageFilebased>();
            services.AddSingleton <IdenticalDumpRepository>();
            services.AddSingleton <IJiraApiService, JiraApiService>();
            services.AddSingleton <IJiraIssueStorage, JiraIssueStorageFilebased>();
            services.AddSingleton <JiraIssueRepository>();
            services.AddSingleton <SearchService>();
            if (superDumpSettings.UseAmazonSqs)
            {
                services.AddSingleton <AmazonSqsClientService>();
                services.AddSingleton <AmazonSqsPollingService>();
            }

            if (superDumpSettings.UseAmazonSqs)
            {
                services.AddSingleton <IFaultReportSender, AmazonSqsFaultReportingSender>();
            }
            else
            {
                services.AddSingleton <IFaultReportSender, ConsoleFaultReportSender>();
            }
            services.AddSingleton <FaultReportingService>();

            var sdk = OneAgentSdkFactory.CreateInstance();

            sdk.SetLoggingCallback(new DynatraceSdkLogger(services.BuildServiceProvider().GetService <ILogger <DynatraceSdkLogger> >()));
            services.AddSingleton <IOneAgentSdk>(sdk);

            services.AddWebSocketManager();
        }
コード例 #2
0
ファイル: Startup.cs プロジェクト: jeffstokes72/superdump
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            // setup path
            IConfigurationSection configurationSection = Configuration.GetSection(nameof(SuperDumpSettings));
            IConfigurationSection binPathSection       = configurationSection.GetSection(nameof(SuperDumpSettings.BinPath));
            IEnumerable <string>  binPath = binPathSection.GetChildren().Select(s => s.Value);
            string path           = Environment.GetEnvironmentVariable("PATH");
            string additionalPath = string.Join(";", binPath);

            Environment.SetEnvironmentVariable("PATH", path + ";" + additionalPath);

            services.AddOptions();
            services.Configure <SuperDumpSettings>(Configuration.GetSection(nameof(SuperDumpSettings)));

            var pathHelper = new PathHelper(Configuration.GetSection(nameof(SuperDumpSettings)));

            services.AddSingleton(pathHelper);

            var superDumpSettings = new SuperDumpSettings();

            Configuration.GetSection(nameof(SuperDumpSettings)).Bind(superDumpSettings);

            // add ldap authentication
            if (superDumpSettings.UseLdapAuthentication)
            {
                services.AddLdapCookieAuthentication(superDumpSettings.LdapAuthenticationSettings, new LdapAuthenticationPathOptions {
                    LoginPath        = "/Login/Index/",
                    LogoutPath       = "/Login/Logout/",
                    AccessDeniedPath = "/Login/AccessDenied/"
                });
                services.AddSingleton(typeof(IAuthorizationHelper), typeof(AuthorizationHelper));
            }
            else
            {
                services.AddPoliciesForNoAuthentication();
                services.AddSingleton(typeof(IAuthorizationHelper), typeof(NoAuthorizationHelper));
            }

            //configure DB
            if (Configuration.GetValue <bool>("UseInMemoryHangfireStorage"))
            {
                services.AddHangfire(x => x.UseStorage(new Hangfire.MemoryStorage.MemoryStorage()));
            }
            else
            {
                string connString;
                Console.WriteLine(Directory.GetCurrentDirectory());
                using (SqlConnection conn = LocalDBAccess.GetLocalDB(Configuration, "HangfireDB", pathHelper)) {
                    connString = conn.ConnectionString;
                }
                if (string.IsNullOrEmpty(connString))
                {
                    throw new Exception("DB could not be created, please check if LocalDB is installed.");
                }
                services.AddHangfire(x => x.UseSqlServerStorage(connString));
            }

            // set upload limit
            int maxUploadSizeMB = Configuration.GetSection(nameof(SuperDumpSettings)).GetValue <int>(nameof(SuperDumpSettings.MaxUploadSizeMB));

            if (maxUploadSizeMB == 0)
            {
                maxUploadSizeMB = 16000;                                   // default
            }
            services.Configure <FormOptions>(opt => opt.MultipartBodyLengthLimit = 1024L * 1024L * maxUploadSizeMB);

            // Add framework services.
            services.AddMvc();
            //services.AddCors();
            services.AddSwaggerGen();

            services.ConfigureSwaggerGen(options => {
                options.SingleApiVersion(new Info {
                    Version        = "v1",
                    Title          = "SuperDump API",
                    Description    = "REST interface for SuperDump analysis tool",
                    TermsOfService = "None",
                    Contact        = new Contact {
                        Url = "https://github.com/Dynatrace/superdump"
                    }
                });

                //Determine base path for the application.
                var basePath = PlatformServices.Default.Application.ApplicationBasePath;

                //Set the comments path for the swagger json and ui.
                var xmlDocFile = new FileInfo(Path.Combine(basePath, "SuperDumpService.xml"));
                if (xmlDocFile.Exists)
                {
                    options.IncludeXmlComments(xmlDocFile.FullName);
                }
            });

            // for pagination list
            services.AddBootstrapPagerGenerator(options => {
                options.ConfigureDefault();
            });

            // register repository as singleton
            services.AddSingleton <SuperDumpRepository>();

            services.AddSingleton <BundleRepository>();
            services.AddSingleton <BundleStorageFilebased>();
            services.AddSingleton <DumpRepository>();
            services.AddSingleton <DumpStorageFilebased>();
            services.AddSingleton <AnalysisService>();
            services.AddSingleton <DownloadService>();
            services.AddSingleton <SymStoreService>();
            services.AddSingleton <UnpackService>();
            services.AddSingleton <NotificationService>();
            services.AddSingleton <SlackNotificationService>();
            services.AddSingleton <ElasticSearchService>();
            services.AddSingleton <DumpRetentionService>();
            services.AddSingleton <SimilarityService>();
            services.AddSingleton <RelationshipRepository>();
            services.AddSingleton <RelationshipStorageFilebased>();
            services.AddWebSocketManager();
        }