1- using Microsoft . AspNetCore ;
1+ using APIJSON . NET ;
2+ using APIJSON . NET . Models ;
3+ using APIJSON . NET . Services ;
4+ using Microsoft . AspNetCore ;
5+ using Microsoft . AspNetCore . Builder ;
26using Microsoft . AspNetCore . Hosting ;
7+ using Microsoft . AspNetCore . Http ;
8+ using Microsoft . Extensions . DependencyInjection ;
9+ using Microsoft . Extensions . Hosting ;
10+ using Microsoft . IdentityModel . Tokens ;
11+ using Microsoft . OpenApi . Models ;
12+ using System ;
13+ using System . Collections . Generic ;
314using System . Net ;
15+ using System . Text ;
416
5- namespace APIJSON . NET
17+ const string _defaultCorsPolicyName = "localhost" ;
18+ var builder = WebApplication . CreateBuilder ( args ) ;
19+
20+ // Add services to the container.
21+
22+ builder . Services . AddControllers ( ) ;
23+ // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
24+ builder . Services . AddEndpointsApiExplorer ( ) ;
25+
26+ builder . Services . Configure < List < Role > > ( builder . Configuration . GetSection ( "RoleList" ) ) ;
27+ builder . Services . Configure < Dictionary < string , string > > ( builder . Configuration . GetSection ( "tablempper" ) ) ;
28+ builder . Services . Configure < TokenAuthConfiguration > ( tokenAuthConfig =>
629{
7- public class Program
30+ tokenAuthConfig . SecurityKey = new SymmetricSecurityKey ( Encoding . ASCII . GetBytes ( builder . Configuration [ "Authentication:JwtBearer:SecurityKey" ] ) ) ;
31+ tokenAuthConfig . Issuer = builder . Configuration [ "Authentication:JwtBearer:Issuer" ] ;
32+ tokenAuthConfig . Audience = builder . Configuration [ "Authentication:JwtBearer:Audience" ] ;
33+ tokenAuthConfig . SigningCredentials = new SigningCredentials ( tokenAuthConfig . SecurityKey , SecurityAlgorithms . HmacSha256 ) ;
34+ tokenAuthConfig . Expiration = TimeSpan . FromDays ( 1 ) ;
35+ } ) ;
36+ AuthConfigurer . Configure ( builder . Services , builder . Configuration ) ;
37+
38+ var origins = builder . Configuration . GetSection ( "CorsUrls" ) . Value . Split ( "," ) ;
39+ builder . Services . AddCors ( options => options . AddPolicy ( _defaultCorsPolicyName ,
40+ builder =>
41+ builder . WithOrigins ( origins )
42+ . AllowAnyHeader ( )
43+ . AllowAnyMethod ( ) . AllowCredentials ( )
44+ ) ) ;
45+ builder . Services . AddControllers ( )
46+ . AddNewtonsoftJson ( options =>
847 {
9- public static void Main ( string [ ] args )
10- {
11- CreateWebHostBuilder ( args ) . Build ( ) . Run ( ) ;
12- }
13-
14- public static IWebHostBuilder CreateWebHostBuilder ( string [ ] args )
15- {
16- return WebHost . CreateDefaultBuilder ( args ) . UseStartup < Startup > ( ) ;
17- }
18- }
48+ options . SerializerSettings . ReferenceLoopHandling = Newtonsoft . Json . ReferenceLoopHandling . Ignore ;
49+ options . SerializerSettings . DateFormatString = "yyyy-MM-dd HH:mm:ss" ;
50+ } ) ; ;
51+ builder . Services . AddSwaggerGen ( c =>
52+ {
53+ c . SwaggerDoc ( "v1" , new OpenApiInfo { Title = "APIJSON.NET" , Version = "v1" } ) ;
54+ } ) ;
55+ builder . Services . AddSingleton < DbContext > ( ) ;
56+ builder . Services . AddSingleton < SelectTable > ( ) ;
57+ builder . Services . AddSingleton < TokenAuthConfiguration > ( ) ;
58+ builder . Services . AddSingleton < IHttpContextAccessor , HttpContextAccessor > ( ) ;
59+ builder . Services . AddTransient < IIdentityService , IdentityService > ( ) ;
60+ builder . Services . AddTransient < ITableMapper , TableMapper > ( ) ;
61+
62+
63+ var app = builder . Build ( ) ;
64+
65+ // Configure the HTTP request pipeline.
66+ if ( app . Environment . IsDevelopment ( ) )
67+ {
68+ app . UseSwagger ( ) ;
69+ app . UseSwaggerUI ( c =>
70+ {
71+ c . SwaggerEndpoint ( "/swagger/v1/swagger.json" , "My API V1" ) ;
72+
73+ } ) ;
1974}
75+
76+ app . UseHttpsRedirection ( ) ;
77+ app . UseStaticFiles ( ) ;
78+ app . UseAuthorization ( ) ;
79+ app . UseCors ( _defaultCorsPolicyName ) ;
80+ app . MapControllers ( ) ;
81+ app . UseJwtTokenMiddleware ( ) ;
82+ DbInit . Initialize ( app ) ;
83+ app . Run ( ) ;
0 commit comments