币安量化交易
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

129 lines
5.3 KiB

using Binance.TradeRobot.API.Extensions;
using Binance.TradeRobot.API.Filters;
using Binance.TradeRobot.Business;
using Binance.TradeRobot.Common.Extensions;
using Binance.TradeRobot.Common.Http;
using Binance.TradeRobot.Model.Base;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.IdentityModel.Tokens;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using System.Reflection;
using System.Text;
using Yitter.IdGenerator;
namespace Binance.TradeRobot.API
{
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.AddMemoryCache();
services.AddSingleton<NLogManager>();
services.AddSingleton<RestApiService>();
services.AddSingleton<TaskSchedulerManager>();
var fsql = new FreeSql.FreeSqlBuilder().UseConnectionString(FreeSql.DataType.MySql, Configuration.GetConnectionString("DB")).Build();
services.AddSingleton(typeof(IFreeSql), fsql);
var csredis = new CSRedis.CSRedisClient(Configuration.GetConnectionString("Redis"));
RedisHelper.Initialization(csredis);
var idOption = new IdGeneratorOptions(1);
var idGenerator = new DefaultIdGenerator(idOption);
services.AddSingleton(typeof(IIdGenerator), idGenerator);
services.AddHttpContextAccessor();
services.AddHttpClient();
services.AddCors(options =>
{
options.AddDefaultPolicy(p =>
{
p.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader();
});
});
services.AddControllers(configure =>
{
configure.Filters.Add<ResultFilter>();
}).AddNewtonsoftJson(setupAction =>
{
setupAction.SerializerSettings.ContractResolver = new DefaultContractResolver();
setupAction.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss";
setupAction.SerializerSettings.NullValueHandling = Newtonsoft.Json.NullValueHandling.Include;
setupAction.SerializerSettings.DefaultValueHandling = Newtonsoft.Json.DefaultValueHandling.Include;
});
services.AddSwagger("�Ұ��Զ�����API");
services.BatchRegisterService(new Assembly[] { Assembly.Load("Binance.TradeRobot.Business") });
services.AddMapper(new MappingProfiles());
JsonConvert.DefaultSettings = () => new JsonSerializerSettings()
{
ContractResolver = new DefaultContractResolver(),
DateFormatString = "yyyy-MM-dd HH:mm:ss",
NullValueHandling = NullValueHandling.Include,
DefaultValueHandling = DefaultValueHandling.Include
};
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme).AddJwtBearer(JwtBearerDefaults.AuthenticationScheme, x =>
{
var jwtSecret = Configuration.GetSection("JwtConfig:Secret").Value;
var jwtIssuer = Configuration.GetSection("JwtConfig:Issuer").Value;
var jwtAudience = Configuration.GetSection("JwtConfig:Audience").Value;
x.SaveToken = true;
x.RequireHttpsMetadata = false;
x.TokenValidationParameters = new TokenValidationParameters()
{
ValidateIssuerSigningKey = true,
ValidateIssuer = true,
ValidateAudience = true,
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(jwtSecret)),
ValidIssuer = jwtIssuer,
ValidAudience = jwtAudience,
ValidateLifetime = true
};
});
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, RobotBusiness robotBusiness, GlobalContext globalContext)
{
app.UseSwagger(c => c.SerializeAsV2 = true)
.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "Binance.TradeRobot.API");
c.RoutePrefix = string.Empty;
});
app.UseCustomException();
//app.UseHttpsRedirection();
app.UseRouting();
app.UseCors();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
var robotList = robotBusiness.GetRobotList(robotState: Enums.RobotState.Runing);
foreach (var robot in robotList)
{
globalContext.SubscribeKLine(robot);
globalContext.SubscribeOrderPublish(robot);
}
}
}
}