/
Startup.cs
152 lines (132 loc) · 5.92 KB
/
Startup.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
using System;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Dapper;
using Tzkt.Api.Repositories;
using Tzkt.Api.Services;
using Tzkt.Api.Services.Auth;
using Tzkt.Api.Services.Cache;
using Tzkt.Api.Services.Sync;
using Tzkt.Api.Swagger;
using Tzkt.Api.Websocket;
using Tzkt.Api.Websocket.Hubs;
using Tzkt.Api.Websocket.Processors;
using Tzkt.Data;
namespace Tzkt.Api
{
public class Startup
{
public IConfiguration Configuration { get; }
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<TzktContext>(options =>
options.UseNpgsql(Configuration.GetConnectionString("DefaultConnection")));
services.AddSingleton<AccountsCache>();
services.AddSingleton<BigMapsCache>();
services.AddSingleton<AliasesCache>();
services.AddSingleton<ProtocolsCache>();
services.AddSingleton<QuotesCache>();
services.AddSingleton<SoftwareCache>();
services.AddSingleton<StateCache>();
services.AddSingleton<TimeCache>();
services.AddTransient<StateRepository>();
services.AddTransient<AccountRepository>();
services.AddTransient<OperationRepository>();
services.AddTransient<BalanceHistoryRepository>();
services.AddTransient<ReportRepository>();
services.AddTransient<BlockRepository>();
services.AddTransient<VotingRepository>();
services.AddTransient<ProtocolRepository>();
services.AddTransient<BakingRightsRepository>();
services.AddTransient<CyclesRepository>();
services.AddTransient<RewardsRepository>();
services.AddTransient<QuotesRepository>();
services.AddTransient<CommitmentRepository>();
services.AddTransient<StatisticsRepository>();
services.AddTransient<SoftwareRepository>();
services.AddTransient<BigMapsRepository>();
services.AddTransient<MetadataRepository>();
services.AddAuthService(Configuration);
services.AddHomeService();
services.AddStateListener();
services.AddControllers()
.AddJsonOptions(options =>
{
options.JsonSerializerOptions.MaxDepth = 1024;
options.JsonSerializerOptions.IgnoreNullValues = true;
options.JsonSerializerOptions.Converters.Add(new AccountConverter());
options.JsonSerializerOptions.Converters.Add(new DateTimeConverter());
options.JsonSerializerOptions.Converters.Add(new OperationConverter());
options.JsonSerializerOptions.Converters.Add(new OperationErrorConverter());
})
.ConfigureApiBehaviorOptions(options =>
{
options.InvalidModelStateResponseFactory = context => new BadRequest(context);
});
services.AddOpenApiDocument();
#region websocket
if (Configuration.GetWebsocketConfig().Enabled)
{
services.AddTransient<HeadProcessor<DefaultHub>>();
services.AddTransient<IHubProcessor, HeadProcessor<DefaultHub>>();
services.AddTransient<BlocksProcessor<DefaultHub>>();
services.AddTransient<IHubProcessor, BlocksProcessor<DefaultHub>>();
services.AddTransient<OperationsProcessor<DefaultHub>>();
services.AddTransient<IHubProcessor, OperationsProcessor<DefaultHub>>();
services.AddTransient<BigMapsProcessor<DefaultHub>>();
services.AddTransient<IHubProcessor, BigMapsProcessor<DefaultHub>>();
services.AddSignalR(options =>
{
options.EnableDetailedErrors = true;
options.KeepAliveInterval = TimeSpan.FromSeconds(10);
})
.AddJsonProtocol(jsonOptions =>
{
jsonOptions.PayloadSerializerOptions.MaxDepth = 1024;
jsonOptions.PayloadSerializerOptions.IgnoreNullValues = true;
jsonOptions.PayloadSerializerOptions.Converters.Add(new AccountConverter());
jsonOptions.PayloadSerializerOptions.Converters.Add(new DateTimeConverter());
jsonOptions.PayloadSerializerOptions.Converters.Add(new OperationConverter());
jsonOptions.PayloadSerializerOptions.Converters.Add(new OperationErrorConverter());
});
}
#endregion
#region dapper
SqlMapper.AddTypeHandler(new AccountMetadataTypeHandler());
SqlMapper.AddTypeHandler(new JsonElementTypeHandler());
SqlMapper.AddTypeHandler(new RawJsonTypeHandler());
#endregion
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseCors(builder => builder
.AllowAnyHeader()
.AllowAnyMethod()
.SetIsOriginAllowed(_ => true)
.AllowCredentials());
app.UseOpenApi();
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
if (Configuration.GetWebsocketConfig().Enabled)
{
endpoints.MapHub<DefaultHub>("/v1/events");
}
});
}
}
}