82 lines
3.2 KiB
C#
82 lines
3.2 KiB
C#
using _2021_backend.Data;
|
|
using _2021_backend.Models;
|
|
using _2021_backend.Utils;
|
|
using Microsoft.AspNetCore.Hosting;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.Hosting;
|
|
using System;
|
|
using System.Linq;
|
|
|
|
|
|
namespace _2021_backend
|
|
{
|
|
public class Program
|
|
{
|
|
|
|
public static void Main(string[] args)
|
|
{
|
|
AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true);
|
|
|
|
var host = CreateHostBuilder(args).Build();
|
|
using (var scope = host.Services.CreateScope())
|
|
{
|
|
var services = scope.ServiceProvider;
|
|
try
|
|
{
|
|
var context = services.GetRequiredService<BackendContext>();
|
|
context.Database.Migrate();
|
|
Console.WriteLine("Migration succeeded.");
|
|
var config = host.Services.GetRequiredService<IConfiguration>();
|
|
TencentSMS.Init(config["TENCENT_ID"], config["TENCENT_KEY"], config["SMS_APPID"], config["SMS_ID_ACCEPT"], config["SMS_ID_REJECT"], config["SMS_ID_TIMESET"], config["SMS_ID_SUBMITTED"], config["SMS_ID_TIMESELECT"], config["SMS_ID_CAPTCHA"]);
|
|
var botname = config["SMSBOT_NAME"];
|
|
var q = context.Users.Where(it => it.Name == botname);
|
|
if (q.Count() == 0)
|
|
{
|
|
var botusr = new User();
|
|
botusr.Name = botname;
|
|
botusr.Id = 0;
|
|
botusr.isManager = true;
|
|
botusr.Secret = config["SMSBOT_SECRET"];
|
|
botusr.stuID = config["SMSBOT_STUID"];
|
|
User.Bot = botusr;
|
|
context.Users.Add(botusr);
|
|
}
|
|
else
|
|
{
|
|
User.Bot = q.FirstOrDefault();
|
|
//do nothing cause I 've already got a bot
|
|
}
|
|
var name = config["ADMIN_USERNAME"];
|
|
while (context.Users.Any(e => e.stuID == name))
|
|
{
|
|
context.Users.Remove(context.Users.FirstOrDefault(e => e.stuID == name));
|
|
context.SaveChanges();
|
|
}
|
|
var usr = new User();
|
|
usr.Name = "administrator";
|
|
usr.isManager = true;
|
|
usr.Secret = EvaCryptoHelper.Password2Secret(config["ADMIN_PASSWORD"]);
|
|
usr.stuID = config["ADMIN_USERNAME"]; ;
|
|
Console.WriteLine(usr.Id.ToString());
|
|
context.Users.Add(usr);
|
|
context.SaveChanges();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine(ex.Message);
|
|
}
|
|
}
|
|
|
|
host.Run();
|
|
}
|
|
public static IHostBuilder CreateHostBuilder(string[] args) =>
|
|
Host.CreateDefaultBuilder(args)
|
|
.ConfigureWebHostDefaults(webBuilder =>
|
|
{
|
|
webBuilder.UseStartup<Startup>();
|
|
});
|
|
}
|
|
}
|