Example #1
0
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            Kernel = InitializeNinject.Initialize(app, Configuration, RequestScope);
            InitializeDatabase.Initialize(Kernel, Configuration, RequestScope);
            InitializeServices.Initialize(Kernel);

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

            app.UseHttpsRedirection();
            app.UseCors("DefaultCors");
            app.UseMvc();

            app.UseSwagger();
            app.UseSwaggerUI(c =>
            {
                c.SwaggerEndpoint("/swagger/v1/swagger.json", "Magic");
            });
        }
Example #2
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            //启用静态文件中间件
            app.UseStaticFiles();

            //初始化数据
            InitializeDatabase.Initialize(app.ApplicationServices);

            //添加jwt验证
            app.UseAuthentication();

            #region swagger

            // Enable middleware to serve generated Swagger as a JSON endpoint.
            app.UseSwagger(c =>
            {
                c.RouteTemplate = "api-docs/{documentName}/bendan.json";
            });

            // Enable middleware to serve swagger-ui (HTML, JS, CSS, etc.),
            // specifying the Swagger JSON endpoint.
            app.UseSwaggerUI(c =>
            {
                c.DocumentTitle = "BenDan API";
                c.SwaggerEndpoint("/api-docs/v1/bendan.json", "BenDan API V1");
                c.RoutePrefix = string.Empty;
                c.IndexStream = () => GetType().Assembly.GetManifestResourceStream("BenDan.wwwroot.swagger.ui.index.html");
            });

            #endregion

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


            app.UseHttpsRedirection();

            app.UseRouting();

            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
        }
Example #3
0
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc((x) => x.OutputFormatters.Add(new ContentFormatters()))
            .SetCompatibilityVersion(CompatibilityVersion.Version_2_2);

            // Adding some basic configurations.
            services.AddSingleton <IHttpContextAccessor, HttpContextAccessor>();
            services.AddSingleton(Configuration);
            services.Configure <RouteOptions>(options => options.LowercaseUrls = true);

            #region [ -- Magic parts -- ]

            // Giving every module a chance to configure its services.
            Configurator.ConfigureServices(services, Configuration);

            // Initializing database.
            InitializeDatabase.Initialize(services, Configuration);

            // Configuring swagger.
            services.AddSwaggerGen(swag =>
            {
                swag.SwaggerDoc("v1", new Info
                {
                    Title       = "Super DRY Magic",
                    Version     = "v1",
                    Description = "An ASP.NET Core web API Starter Kit",
                    License     = new License
                    {
                        Name = "Affero GPL + Proprietary commercial (Closed Source)",
                        Url  = "https://github.com/polterguy/magic",
                    },
                    Contact = new Contact
                    {
                        Name  = "Thomas Hansen",
                        Email = "*****@*****.**",
                        Url   = "https://github.com/polterguy/magic",
                    },
                });
                foreach (var idxFile in Directory.GetFiles(AppContext.BaseDirectory, "*.xml"))
                {
                    swag.IncludeXmlComments(idxFile);
                }
                swag.OperationFilter <FileUploadOperation>();
            });

            #endregion
        }
Example #4
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            InitializeDatabase.Initialize(app);

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

            app.UseRouting();

            app.UseGraphQL();
            app.UsePlayground();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapGet("/", async context =>
                {
                    await context.Response.WriteAsync("Hello World!");
                });
            });
        }
Example #5
0
        public static void Main(string[] args)
        {
            var host = BuildWebHost(args);

            using (var scope = host.Services.CreateScope())
            {
                var services = scope.ServiceProvider;

                try
                {
                    var context = services.GetRequiredService <MainContext>();
                    context.Database.EnsureCreated();
                    InitializeDatabase.Initialize(context);
                }
                catch (Exception)
                {
                    throw;
                }
            }

            host.Run();
        }
Example #6
0
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            foreach (var ctrlType in app.GetControllerTypes())
            {
                Kernel.Bind(ctrlType).ToSelf().InScope(RequestScope);
            }

            Kernel.Bind <IConfiguration>().ToConstant(Configuration);
            InitializeDatabase.Initialize(Kernel, Configuration, RequestScope);
            Configurator.ConfigureNinject(Kernel, Configuration);

            app.UseExceptionHandler(errorApp => errorApp.Run(async context =>
            {
                context.Response.StatusCode  = 500;
                context.Response.ContentType = "application/json";
                var ex     = context.Features.Get <IExceptionHandlerPathFeature>();
                var logger = LogManager.GetLogger(ex?.Error.GetType() ?? typeof(Startup));
                var msg    = ex?.Error.Message ?? "Unhandled exception";
                logger.Error("At path: " + ex?.Path);
                logger.Error(msg, ex?.Error);
                var response = new JObject
                {
                    ["message"] = msg,
                };
                await context.Response.WriteAsync(response.ToString(Formatting.Indented));
            }));

            app.UseHttpsRedirection();
            app.UseCors(x => x.AllowAnyHeader().AllowAnyOrigin().AllowAnyHeader());

            Configurator.ConfigureApplication(app, Configuration);

            app.UseMvc();
            app.UseSwagger();
            app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "TITLE"));

            Configurator.ExecuteStartups(Kernel, Configuration);
        }