FIC_Solar/SolarPower/Startup.cs
2021-06-28 15:16:08 +08:00

116 lines
4.1 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using MySql.Data.MySqlClient;
using SolarPower.Helper;
using SolarPower.Models;
using SolarPower.Repository.Implement;
using SolarPower.Repository.Interface;
using SolarPower.Services;
using SolarPower.Services.Interface;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Threading.Tasks;
namespace SolarPower
{
public class Startup
{
public IConfiguration Configuration { get; }
public DBConfig dBConfig = new DBConfig();
public Startup(IConfiguration configuration)
{
Configuration = configuration;
dBConfig.Server = Configuration.GetValue<string>("DBConfig:Server");
dBConfig.Port = Configuration.GetValue<string>("DBConfig:Port");
dBConfig.Database = Configuration.GetValue<string>("DBConfig:Database");
dBConfig.Root = Configuration.GetValue<string>("DBConfig:Root");
dBConfig.Password = Configuration.GetValue<string>("DBConfig:Password");
}
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddLogging(
builder =>
{
builder.AddFilter("Microsoft", LogLevel.Warning)
.AddFilter("System", LogLevel.Warning)
.AddFilter("NToastNotify", LogLevel.Warning)
.AddConsole();
});
#region SMPT°t¸m
services.AddTransient<ISendEmailService, SendEmailService>();
services.Configure<SMTPConfig>(Configuration.GetSection("SMTPConfig"));
#endregion
#region DBHelper ª`¤J
services.AddScoped<IDatabaseHelper>(x => new DatabaseHelper(dBConfig));
#endregion
#region Repository ª`¤J
services.AddScoped<IUserRepository, UserRepository>();
services.AddScoped<ICompanyRepository, CompanyRepository>();
services.AddScoped<IRoleRepository, RoleRepository>();
services.AddScoped<IPowerStationRepository, PowerStationRepository>();
services.AddScoped<IOperatorLogRepository, OperatorLogRepository>();
services.AddScoped<IOperationRepository, OperationRepository>();
#endregion
double loginExpireMinute = this.Configuration.GetValue<double>("LoginExpireMinute");
services.AddSession(options =>
{
options.IdleTimeout = TimeSpan.FromMinutes(loginExpireMinute);
});
services.AddControllersWithViews();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddFile("Logs/log-{Date}.txt");
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseSession();
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Login}/{action=Index}/{id?}");
});
}
}
}