149 lines
		
	
	
		
			4.1 KiB
		
	
	
	
		
			C#
		
	
	
	
			
		
		
	
	
			149 lines
		
	
	
		
			4.1 KiB
		
	
	
	
		
			C#
		
	
	
	
using langguanApi.Common.Redis;
 | 
						|
using langguanApi.Common.WebSocket;
 | 
						|
using langguanApi.Extensions;
 | 
						|
using langguanApi.Extensions.AutoDI;
 | 
						|
using langguanApi.Middleware;
 | 
						|
using Microsoft.Extensions.Configuration;
 | 
						|
using Microsoft.Extensions.Options;
 | 
						|
using Microsoft.OpenApi.Models;
 | 
						|
using Newtonsoft.Json.Linq;
 | 
						|
using System.Net;
 | 
						|
using System.Text.Json;
 | 
						|
 | 
						|
var builder = WebApplication.CreateBuilder(args);
 | 
						|
 | 
						|
 | 
						|
// Add services to the container.
 | 
						|
 | 
						|
builder.Services.AddControllers(options =>
 | 
						|
{
 | 
						|
    options.Filters.Add<CustomerExceptionFilter>();
 | 
						|
}).AddNewtonsoftJson(option =>
 | 
						|
{
 | 
						|
 | 
						|
    option.SerializerSettings.ContractResolver = new Newtonsoft.Json.Serialization.DefaultContractResolver();
 | 
						|
    option.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss";
 | 
						|
    //驼峰
 | 
						|
    option.SerializerSettings.ContractResolver = new Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver();
 | 
						|
 | 
						|
}).AddJsonOptions(option =>
 | 
						|
{
 | 
						|
    option.JsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
 | 
						|
});
 | 
						|
//swagger
 | 
						|
builder.Services.AddSwaggerGen(
 | 
						|
    options =>
 | 
						|
    {
 | 
						|
        options.SwaggerDoc("v1", new OpenApiInfo()
 | 
						|
        {
 | 
						|
            Title = "Title",
 | 
						|
            Version = "v1",
 | 
						|
            Description = "Description",
 | 
						|
        });
 | 
						|
        var path = Path.Combine(AppContext.BaseDirectory, "langguanApi.xml");
 | 
						|
        options.IncludeXmlComments(path, true);
 | 
						|
        options.OrderActionsBy(_ => _.RelativePath);
 | 
						|
    });
 | 
						|
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
 | 
						|
builder.Services.AddEndpointsApiExplorer();
 | 
						|
//websocket
 | 
						|
builder.Services.AddSignalR();
 | 
						|
//redis
 | 
						|
var redisoptions = builder.Configuration.GetSection("Redis").Get<RedisOptions>();
 | 
						|
if (redisoptions != null)
 | 
						|
{
 | 
						|
    builder.Services.AddRedis(options =>
 | 
						|
    {
 | 
						|
        options.Port = redisoptions.Port;
 | 
						|
        options.Server = redisoptions.Server;
 | 
						|
        options.Index = redisoptions.Index;
 | 
						|
        options.Password = redisoptions.Password;
 | 
						|
        options.Key = redisoptions.Key;
 | 
						|
    });
 | 
						|
}
 | 
						|
//自动注入
 | 
						|
builder.Services.ServicesAutoInjectionExtension();
 | 
						|
builder.Services.AddSocketService();
 | 
						|
builder.Services.AddHttpClient();
 | 
						|
builder.Services.AddHttpClient("httpreq", m => { }).ConfigurePrimaryHttpMessageHandler(() => new HttpClientHandler
 | 
						|
{
 | 
						|
    ServerCertificateCustomValidationCallback = (m, c, ch, e) => true
 | 
						|
});
 | 
						|
//cross domain
 | 
						|
builder.Services.AddCors(options =>
 | 
						|
{
 | 
						|
    options.AddPolicy("CorsPolicy", builder =>
 | 
						|
    {
 | 
						|
        builder.AllowAnyOrigin();
 | 
						|
        builder.AllowAnyMethod();
 | 
						|
        builder.AllowAnyHeader();
 | 
						|
    });
 | 
						|
});
 | 
						|
 | 
						|
var app = builder.Build();
 | 
						|
ServiceLocator.Instance = app.Services;
 | 
						|
app.UseRouting();
 | 
						|
//执行匹配的端点
 | 
						|
app.UseEndpoints(endpoints =>
 | 
						|
{
 | 
						|
    endpoints.MapHub<SocketHub>("/notification");
 | 
						|
    endpoints.MapControllers();
 | 
						|
});
 | 
						|
 | 
						|
 | 
						|
// Configure the HTTP request pipeline.
 | 
						|
if (app.Environment.IsDevelopment())
 | 
						|
{
 | 
						|
    app.UseSwagger();
 | 
						|
    app.UseSwaggerUI();
 | 
						|
}
 | 
						|
app.UseSwagger();
 | 
						|
app.UseSwaggerUI();
 | 
						|
app.UseCors("CorsPolicy");
 | 
						|
//app.UseAuthorization();
 | 
						|
 | 
						|
 | 
						|
 
 | 
						|
 | 
						|
if (!await GetNowTimeAsync())
 | 
						|
{
 | 
						|
    Console.WriteLine("当前时间不在可运行时间范围内,请联系供应商。");
 | 
						|
    Environment.Exit(0);
 | 
						|
}
 | 
						|
app.Run();
 | 
						|
 | 
						|
 | 
						|
 | 
						|
 | 
						|
//获取当前时间是否在可运行时间范围内
 | 
						|
static async Task<bool> GetNowTimeAsync()
 | 
						|
{
 | 
						|
    try
 | 
						|
    {
 | 
						|
        DateTime startTime = DateTime.Parse("2024-09-01");
 | 
						|
        //从公网上获取当前时间
 | 
						|
        var url = "http://www.worldtimeapi.org/api/ip";
 | 
						|
        var myClient = new HttpClient(new HttpClientHandler() { UseDefaultCredentials = true });
 | 
						|
        var response = await myClient.GetStringAsync(url);
 | 
						|
        var time = JObject.Parse(response)["datetime"].ToString();
 | 
						|
        var now = DateTime.Parse(time);
 | 
						|
        Console.WriteLine($"当前时间:{now},过期时间:{startTime.AddDays(365)},距离过期时间还有:{(startTime.AddDays(365) - now).Days} 天");
 | 
						|
        return startTime.AddDays(365) > now ? true : false;
 | 
						|
    }
 | 
						|
    catch
 | 
						|
    {
 | 
						|
        return false;
 | 
						|
    }
 | 
						|
}
 | 
						|
 | 
						|
/// <summary>
 | 
						|
/// 暂存服务
 | 
						|
/// </summary>
 | 
						|
public static class ServiceLocator
 | 
						|
{
 | 
						|
    /// <summary>
 | 
						|
    /// 服务容器
 | 
						|
    /// </summary>
 | 
						|
    public static IServiceProvider Instance { get; set; }
 | 
						|
}
 |