Tue, 09 Nov 2021 12:26:20 GMT
parent
60c2e6354a
commit
10ac041e45
@ -1,26 +1,54 @@
|
|||||||
using System;
|
using cugoj_ng_server.Utilities;
|
||||||
using System.Collections.Generic;
|
using StackExchange.Redis;
|
||||||
using System.Linq;
|
using System.Data;
|
||||||
using System.Threading.Tasks;
|
|
||||||
using Microsoft.AspNetCore.Hosting;
|
var builder = WebApplication.CreateBuilder(args);
|
||||||
using Microsoft.Extensions.Configuration;
|
|
||||||
using Microsoft.Extensions.Hosting;
|
var env = builder.Environment;
|
||||||
using Microsoft.Extensions.Logging;
|
var services = builder.Services;
|
||||||
|
var configuration = builder.Configuration;
|
||||||
namespace cugoj_ng_server
|
|
||||||
|
// 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 =>
|
||||||
|
{
|
||||||
|
opt.Configuration = redisConnstr;
|
||||||
|
opt.InstanceName = "CUGOJ$";
|
||||||
|
});
|
||||||
|
|
||||||
|
services.AddSession(opt =>
|
||||||
|
{
|
||||||
|
opt.Cookie.Name = "_SESSION";
|
||||||
|
opt.Cookie.IsEssential = true;
|
||||||
|
opt.IdleTimeout = TimeSpan.FromMinutes(sessionTimeout);
|
||||||
|
});
|
||||||
|
|
||||||
|
Inject.SetPropertiesByType<cugoj_ng_server.Models.DbConn>(new()
|
||||||
{
|
{
|
||||||
public class Program
|
{ typeof(IConnectionMultiplexer), ConnectionMultiplexer.Connect(redisConnstr) },
|
||||||
{
|
{ typeof(Func<IDbConnection>), new Func<IDbConnection>(() => new MySql.Data.MySqlClient.MySqlConnection(mysqlConnstr)) },
|
||||||
public static void Main(string[] args)
|
});
|
||||||
{
|
|
||||||
CreateHostBuilder(args).Build().Run();
|
services.AddControllers().AddJsonOptions(opt =>
|
||||||
}
|
{
|
||||||
|
opt.JsonSerializerOptions.IncludeFields = true;
|
||||||
public static IHostBuilder CreateHostBuilder(string[] args) =>
|
});
|
||||||
Host.CreateDefaultBuilder(args)
|
|
||||||
.ConfigureWebHostDefaults(webBuilder =>
|
// Configure the HTTP request pipeline.
|
||||||
{
|
var app = builder.Build();
|
||||||
webBuilder.UseStartup<Startup>();
|
|
||||||
});
|
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