2024-05-02 15:24:13 -07:00

186 lines
7.7 KiB
C#

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Church.Net.DAL.EF;
using Church.Net.DAL.EFCoreDBF.Core;
using Church.Net.DAL.EFCoreDBF.Interface;
using Church.Net.Entity;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Diagnostics;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json.Serialization;
using WebAPI.Handlers;
using WebAPI.Hubs;
using WebAPI.Logics;
using WebAPI.Logics.Core;
using WebAPI.Logics.Interface;
using WebAPI.Services;
using WebAPI.Services.AutoReplyCommands;
using WebAPI.Services.Interfaces;
using WebAPI.Services.ScheduledTask;
using static System.Net.Mime.MediaTypeNames;
namespace WebAPI
{
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)
{
services.AddHttpContextAccessor();
services.AddControllers().AddNewtonsoftJson(options =>
{
// Use the default property (Pascal) casing
options.SerializerSettings.ContractResolver = new DefaultContractResolver()
{
NamingStrategy = new CamelCaseNamingStrategy(),
};
//options.SerializerSettings.DateTimeZoneHandling = Newtonsoft.Json.DateTimeZoneHandling.Unspecified;
options.SerializerSettings.NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore;
options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
});
services.AddSignalR();
services.AddSingleton<GameRoomLogic>();
services.AddSingleton<GameRoomHub>();
//Localted at \\ArkNAS\docker\ChurchAPI\docker-compose.yaml
string databaseConnString = Environment.GetEnvironmentVariable("DB_CONN_STRING");
#if DEBUG
databaseConnString = "Host=192.168.68.55;Port=49154;Database=Church;Username=chris;Password=1124";
#endif
//services.AddSingleton(_ => new DatabaseOptions { ConnectionString = databaseConnString });
services.AddSingleton(_ => new DatabaseOptions { ConnectionString = databaseConnString });
//services.AddSingleton<ChurchNetContext>(new ChurchNetContext());
services.AddDbContext<ChurchNetContext>(options =>
options.UseNpgsql(
//Configuration.GetConnectionString()
//"Host=192.168.68.55;Port=49154;Database=ChurchSandbox;Username=chris;Password=1124"
databaseConnString
));
services.AddScoped<LineAutoBotService>();
services.AddScoped<IAutoReplyCommand, ArChurchInfo>();
services.AddScoped<IAutoReplyCommand, ArArkCellGroupInfo>();
services.AddScoped<IAutoReplyCommand, ArArkCellGroupDinner>();
services.AddScoped<IAutoReplyCommand, ArArkCellGroupPrayer>();
services.AddScoped<IAutoReplyCommand, ArHappinessGroupTask>();
services.AddScoped<IAutoReplyCommand, ArHappinessBEST>();
//services.AddScoped<IScheduledTask, MorningPrayer>();
services.AddScoped<VideoDownloadLogic>();
services.AddScoped<LogicService>();
services.AddScoped<PastoralDomainLogic>();
services.AddScoped(typeof(ICrudLogic<>), typeof(LogicBase<>));
services.AddScoped(typeof(ICrudDAL<>), typeof(CrudDALCBase<>));
services.AddScoped(typeof(ICombinedKeyCrudLogic<>), typeof(CombinedKeyLogicBase<>));
services.AddScoped(typeof(ICombinedKeyCrudDAL<>), typeof(CombinedKeyCrudDALCBase<>));
services.AddScoped<LineMessagingAccountLogic>();
services.AddScoped<ILoggingService, DbLoggingService>();
services.AddScoped<IdentityService>();
services.AddScoped<ICrudLogic<FamilyMember>, MemberLogic>();
services.AddHostedService<WorkerService>();
//services.AddMvc(o=>o.Filters.Add(typeof(HandleExceptionFilter)));
//services.AddMvc(o => o.Filters.Add(new HandleExceptionFilter(services.BuildServiceProvider().GetService<ILoggingService>())));
//services.BuildServiceProvider().GetService<ILoggingService>();
services.AddSingleton<IAuthorizationMiddlewareResultHandler, BasicAuthorizationMiddlewareResultHandler>();
//ObjectFactory.Initialize(x =>
// x.For<HttpContextBase>()
// .HybridHttpOrThreadLocalScoped()
// .Use(() => new HttpContextWrapper(HttpContext.Current));
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, DatabaseOptions databaseOptions)
{
//Reference:https://stackoverflow.com/questions/69961449/net6-and-datetime-problem-cannot-write-datetime-with-kind-utc-to-postgresql-ty
AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true);
AppContext.SetSwitch("Npgsql.DisableDateTimeInfinityConversions", true);
AppDomain.CurrentDomain.SetData("ContentRootPath", env.ContentRootPath);
AppDomain.CurrentDomain.SetData("WebRootPath", env.WebRootPath);
if (env.IsDevelopment())
{
//app.UseDeveloperExceptionPage();
app.UseExceptionHandler("/error-development");
}
else
{
app.UseExceptionHandler("/error");
}
// global cors policy
app.UseCors(x => x
//.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader()
.SetIsOriginAllowed(origin => true) // allow any origin
.AllowCredentials()
); // allow credentials
//app.UseHttpsRedirection();
//app.UsePathBase(new PathString("/api"));
app.UsePathBase(new PathString("/"));
app.UseRouting();
//app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
//endpoints.MapControllerRoute(
// name: "default",
// pattern: "{controller}/{id?}");
endpoints.MapHub<AvalonHub>("/AvalonHub");
endpoints.MapHub<WhoIsSpyHub>("/WhoIsSpyHub");
endpoints.MapHub<BaseHub>("/BaseHub");
endpoints.MapHub<GameRoomHub>("/GameRoomHub");
endpoints.MapControllers();
//endpoints.MapControllerRoute(
// name: "default",
// pattern: "{controller}/{action=Index}/{id?}");
});
if (env.IsDevelopment())
{
databaseOptions.GetDbContext().Database.Migrate();
//new ChurchNetContext(databaseOptions.ConnectionString).Database.Migrate();
//dbContext.Database.EnsureCreated();
//dbContext.Database.Migrate();
}
}
}
}