-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
66 lines (56 loc) · 2.03 KB
/
Program.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
using StackExchange.Redis;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.IdentityModel.Tokens;
using System.Text;
using bamboo_grpc.Repositories;
using bamboo_grpc.Services;
using bamboo_grpc.MongoDB;
using bamboo_grpc.Interfaces;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddGrpc();
builder.Services.AddGrpcReflection();
builder.Services.AddLogging();
var redisConnect = Environment.GetEnvironmentVariable("REDIS_CONNECT");
if (string.IsNullOrEmpty(redisConnect))
{
redisConnect = "localhost:6379";
}
builder.Services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
}).AddJwtBearer(options =>
{
options.RequireHttpsMetadata = false;
options.SaveToken = true;
options.TokenValidationParameters = new Microsoft.IdentityModel.Tokens.TokenValidationParameters
{
ValidateIssuerSigningKey = true,
IssuerSigningKey = new SymmetricSecurityKey(Encoding.ASCII.GetBytes(JwtAuthentication.JWT_TOKEN_KEY)),
ValidateIssuer = false,
ValidateAudience = false
};
});
builder.Services.AddAuthorization();
builder.Services.AddSingleton<ConnectionMultiplexer>(
ConnectionMultiplexer.Connect(redisConnect)
);
builder.Services.AddSingleton<IMongoDBContext, MongoDBContext>();
builder.Services.AddSingleton<ITodosRepository, TodosRepository>();
builder.Services.AddSingleton<IUsersRepository, UsersRepository>();
var app = builder.Build();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.MapGrpcService<AuthenticationService>();
app.MapGrpcService<TodoService>();
if (app.Environment.IsDevelopment())
{
app.MapGrpcReflectionService();
}
app.MapGet(
"/",
() =>
"Communication with gRPC endpoints must be made through a gRPC client. To learn how to create a client, visit: https://go.microsoft.com/fwlink/?linkid=2086909"
);
app.Run();