Tue, 09 Nov 2021 12:26:20 GMT
parent
60c2e6354a
commit
10ac041e45
@ -1,26 +1,54 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace cugoj_ng_server
|
||||
using cugoj_ng_server.Utilities;
|
||||
using StackExchange.Redis;
|
||||
using System.Data;
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
var env = builder.Environment;
|
||||
var services = builder.Services;
|
||||
var configuration = builder.Configuration;
|
||||
|
||||
// Add services to the container.
|
||||
var redisConnstr = configuration.GetConnectionString("redis");
|
||||
var mysqlConnstr = configuration.GetConnectionString("mysql");
|
||||
var sessionTimeout = configuration.GetValue<int>("Config:SessionTimeout");
|
||||
|
||||
Dapper.DefaultTypeMap.MatchNamesWithUnderscores = true;
|
||||
|
||||
services.AddStackExchangeRedisCache(opt =>
|
||||
{
|
||||
public class Program
|
||||
opt.Configuration = redisConnstr;
|
||||
opt.InstanceName = "CUGOJ$";
|
||||
});
|
||||
|
||||
services.AddSession(opt =>
|
||||
{
|
||||
public static void Main(string[] args)
|
||||
opt.Cookie.Name = "_SESSION";
|
||||
opt.Cookie.IsEssential = true;
|
||||
opt.IdleTimeout = TimeSpan.FromMinutes(sessionTimeout);
|
||||
});
|
||||
|
||||
Inject.SetPropertiesByType<cugoj_ng_server.Models.DbConn>(new()
|
||||
{
|
||||
CreateHostBuilder(args).Build().Run();
|
||||
}
|
||||
{ typeof(IConnectionMultiplexer), ConnectionMultiplexer.Connect(redisConnstr) },
|
||||
{ typeof(Func<IDbConnection>), new Func<IDbConnection>(() => new MySql.Data.MySqlClient.MySqlConnection(mysqlConnstr)) },
|
||||
});
|
||||
|
||||
public static IHostBuilder CreateHostBuilder(string[] args) =>
|
||||
Host.CreateDefaultBuilder(args)
|
||||
.ConfigureWebHostDefaults(webBuilder =>
|
||||
services.AddControllers().AddJsonOptions(opt =>
|
||||
{
|
||||
webBuilder.UseStartup<Startup>();
|
||||
opt.JsonSerializerOptions.IncludeFields = true;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// Configure the HTTP request pipeline.
|
||||
var app = builder.Build();
|
||||
|
||||
if (env.IsDevelopment())
|
||||
app.UseDeveloperExceptionPage();
|
||||
|
||||
app.UseRouting();
|
||||
|
||||
app.UseSession();
|
||||
|
||||
app.MapControllers();
|
||||
|
||||
app.Run();
|
||||
|
||||
@ -1,75 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using StackExchange.Redis;
|
||||
|
||||
namespace cugoj_ng_server
|
||||
{
|
||||
public class Startup
|
||||
{
|
||||
public Startup(IConfiguration configuration)
|
||||
{
|
||||
Configuration = configuration;
|
||||
}
|
||||
|
||||
public IConfiguration Configuration { get; }
|
||||
|
||||
// This method gets called by the runtime. Use this method to add services to the container.
|
||||
public void ConfigureServices(IServiceCollection services)
|
||||
{
|
||||
var redisConnstr = Configuration.GetConnectionString("redis");
|
||||
var mysqlConnstr = Configuration.GetConnectionString("mysql");
|
||||
var sessionTimeout = Configuration.GetValue<int>("Config:SessionTimeout");
|
||||
services.AddStackExchangeRedisCache(opt =>
|
||||
{
|
||||
opt.Configuration = redisConnstr;
|
||||
opt.InstanceName = "CUGOJ$";
|
||||
});
|
||||
services.AddSession(opt =>
|
||||
{
|
||||
opt.Cookie.Name = "_SESSION";
|
||||
opt.Cookie.IsEssential = true;
|
||||
opt.IdleTimeout = TimeSpan.FromMinutes(sessionTimeout);
|
||||
});
|
||||
Dapper.DefaultTypeMap.MatchNamesWithUnderscores = true;
|
||||
Utilities.Inject.SetPropertiesByType(typeof(Models.DbConn), null, new()
|
||||
{
|
||||
{ typeof(IConnectionMultiplexer), ConnectionMultiplexer.Connect(redisConnstr) },
|
||||
{ typeof(Func<IDbConnection>), new Func<IDbConnection>(() => new MySql.Data.MySqlClient.MySqlConnection(mysqlConnstr)) },
|
||||
});
|
||||
services.AddControllers().AddJsonOptions(opt =>
|
||||
{
|
||||
opt.JsonSerializerOptions.IncludeFields = true;
|
||||
});
|
||||
}
|
||||
|
||||
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
|
||||
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
|
||||
{
|
||||
if (env.IsDevelopment())
|
||||
{
|
||||
app.UseDeveloperExceptionPage();
|
||||
}
|
||||
|
||||
app.UseRouting();
|
||||
|
||||
app.UseSession();
|
||||
|
||||
app.UseEndpoints(endpoints =>
|
||||
{
|
||||
endpoints.MapControllers();
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in new issue