Program.cs 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. using economy.Models;
  2. using Microsoft.EntityFrameworkCore;
  3. using System.Text;
  4. var builder = WebApplication.CreateBuilder(args);
  5. // Add services to the container.
  6. builder.Services.AddControllersWithViews();
  7. // HttpClient »ç¿ë
  8. builder.Services.AddHttpClient<DataGoKR>();
  9. builder.Services.AddHttpClient<FlowerAtOrKR>();
  10. builder.Services.AddHttpClient<KoreaEximGoKR>();
  11. builder.Services.AddHttpClient<DhlotteryCoKR>();
  12. builder.Services.AddHttpClient<FIFA_API>();
  13. builder.Services.AddHttpClient<NTS_API>();
  14. builder.Services.AddHttpClient<Alpha_API>();
  15. builder.Services.AddMemoryCache();
  16. // DB ¿¬°á
  17. builder.Services.AddDbContext<EconomyContext>(options => options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection")));
  18. Encoding.RegisterProvider(CodePagesEncodingProvider.Instance); // ÀÎÄÚµù µî·Ï
  19. var app = builder.Build();
  20. // economy database bootstrap (creates once on first run)
  21. try
  22. {
  23. var masterCsb = new Microsoft.Data.SqlClient.SqlConnectionStringBuilder(builder.Configuration.GetConnectionString("DefaultConnection")) { InitialCatalog = "master" };
  24. using var masterConn = new Microsoft.Data.SqlClient.SqlConnection(masterCsb.ConnectionString);
  25. masterConn.Open();
  26. using var createDbCmd = masterConn.CreateCommand();
  27. createDbCmd.CommandText = "IF DB_ID(N'economy') IS NULL CREATE DATABASE economy;";
  28. createDbCmd.ExecuteNonQuery();
  29. }
  30. catch (Exception e)
  31. {
  32. Console.WriteLine($"Database init error: {e.Message}");
  33. }
  34. // ApiCache table bootstrap (creates once; app still boots if DB is down)
  35. using (var scope = app.Services.CreateScope())
  36. {
  37. try
  38. {
  39. var context = scope.ServiceProvider.GetRequiredService<EconomyContext>();
  40. context.Database.ExecuteSqlRaw("IF OBJECT_ID(N'dbo.ApiCache', N'U') IS NULL CREATE TABLE dbo.ApiCache (CacheKey NVARCHAR(100) NOT NULL PRIMARY KEY, Payload NVARCHAR(MAX) NOT NULL, UpdatedAt DATETIME2 NOT NULL);");
  41. }
  42. catch (Exception e)
  43. {
  44. Console.WriteLine($"ApiCache init error: {e.Message}");
  45. }
  46. }
  47. // Configure the HTTP request pipeline.
  48. if (!app.Environment.IsDevelopment())
  49. {
  50. app.UseExceptionHandler("/Home/Error");
  51. // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
  52. app.UseHsts();
  53. }
  54. app.UseHttpsRedirection();
  55. app.UseStaticFiles();
  56. app.UseRouting();
  57. app.UseAuthorization();
  58. app.MapControllerRoute(
  59. name: "default",
  60. pattern: "{controller=Home}/{action=Index}/{id?}");
  61. app.Run();