Skip to content

Commit ab65bb6

Browse files
authored
Merge pull request #8 from JerryMouseLi/master
数据库生命周期更改
2 parents d0c716c + 991d801 commit ab65bb6

File tree

1 file changed

+94
-0
lines changed

1 file changed

+94
-0
lines changed

Startup.cs

+94
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
namespace APIJSON.NET
2+
{
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Text;
6+
using APIJSON.NET.Models;
7+
using APIJSON.NET.Services;
8+
using Microsoft.AspNetCore.Builder;
9+
using Microsoft.AspNetCore.Hosting;
10+
using Microsoft.AspNetCore.Http;
11+
using Microsoft.AspNetCore.Mvc;
12+
using Microsoft.Extensions.Configuration;
13+
using Microsoft.Extensions.DependencyInjection;
14+
using Microsoft.IdentityModel.Tokens;
15+
using SqlKata.Execution;
16+
using Swashbuckle.AspNetCore.Swagger;
17+
using MySql.Data.MySqlClient;
18+
using SqlKata.Compilers;
19+
20+
public class Startup
21+
{
22+
private const string _defaultCorsPolicyName = "localhost";
23+
public Startup(IConfiguration configuration)
24+
{
25+
Configuration = configuration;
26+
}
27+
28+
public IConfiguration Configuration { get; }
29+
30+
// This method gets called by the runtime. Use this method to add services to the container.
31+
public void ConfigureServices(IServiceCollection services)
32+
{
33+
34+
services.Configure<List<Role>>(Configuration.GetSection("RoleList"));
35+
services.Configure<Dictionary<string,string>>(Configuration.GetSection("tablempper"));
36+
services.Configure<TokenAuthConfiguration>(tokenAuthConfig =>
37+
{
38+
tokenAuthConfig.SecurityKey = new SymmetricSecurityKey(Encoding.ASCII.GetBytes(Configuration["Authentication:JwtBearer:SecurityKey"]));
39+
tokenAuthConfig.Issuer = Configuration["Authentication:JwtBearer:Issuer"];
40+
tokenAuthConfig.Audience = Configuration["Authentication:JwtBearer:Audience"];
41+
tokenAuthConfig.SigningCredentials = new SigningCredentials(tokenAuthConfig.SecurityKey, SecurityAlgorithms.HmacSha256);
42+
tokenAuthConfig.Expiration = TimeSpan.FromDays(1);
43+
});
44+
AuthConfigurer.Configure(services, Configuration);
45+
46+
services.AddCors( options => options.AddPolicy( _defaultCorsPolicyName,
47+
builder =>
48+
builder.AllowAnyOrigin()
49+
.AllowAnyHeader()
50+
.AllowAnyMethod().AllowCredentials()
51+
));
52+
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
53+
services.AddSwaggerGen(c =>
54+
{
55+
c.SwaggerDoc("v1", new Info { Title = "APIJSON.NET", Version = "v1" });
56+
});
57+
// services.AddSingleton<DbContext>();
58+
//services.AddSingleton<SelectTable>();
59+
services.AddScoped<DbContext>();
60+
services.AddScoped<SelectTable>();
61+
62+
services.AddSingleton<TokenAuthConfiguration>();
63+
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
64+
services.AddTransient<IIdentityService, IdentityService>();
65+
services.AddTransient<ITableMapper, TableMapper>();
66+
67+
}
68+
69+
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
70+
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
71+
{
72+
73+
app.UseAuthentication();
74+
75+
app.UseMvc(routes =>
76+
{
77+
routes.MapRoute(
78+
name: "default",
79+
template: "{controller=Home}/{action=Index}/{id?}");
80+
});
81+
app.UseStaticFiles();
82+
app.UseCors(_defaultCorsPolicyName);
83+
app.UseSwagger();
84+
app.UseSwaggerUI(c =>
85+
{
86+
c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
87+
88+
});
89+
90+
app.UseJwtTokenMiddleware();
91+
DbInit.Initialize(app);
92+
}
93+
}
94+
}

0 commit comments

Comments
 (0)