-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
162 lines (137 loc) · 5.9 KB
/
Copy pathProgram.cs
File metadata and controls
162 lines (137 loc) · 5.9 KB
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
153
154
155
156
157
158
159
160
161
162
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Options;
using Microsoft.Azure.Cosmos;
using PodcastTranscribe.API.Models;
using PodcastTranscribe.API.Services;
using Microsoft.OpenApi.Models;
using PodcastTranscribe.API.Configuration;
using Newtonsoft.Json.Converters;
using DotNetEnv; // For loading .env files in development
// Dependency Injection (DI) in ASP.NET Core:
// - 3 types of Services lifetimes in ASP.NET Core:
// Singleton: Created at startup, used for the entire application lifetime
// Scoped: Created once per HTTP request
// Transient: Created each time they are requested
// - use DI for complex, dependent, business logic services
// not use DI (instantiate services directly in controllers) for simple, stateless services (helper)
Env.Load(); // Load environment variables from .env
var builder = WebApplication.CreateBuilder(args);
builder.Services
.AddControllers()
.AddNewtonsoftJson(options =>
{
// if you need any special converters, add them here:
options.SerializerSettings.Converters.Add(new StringEnumConverter());
});
// Add services to the container.
// EpisodeController is inherited from ControllerBase, which marks it as a "controller"
// all controllers are registered here.
builder.Services.AddControllers();
// get Cosmos DB credentials from environment variables
builder.Services.Configure<CosmosDbSettings>(options =>
{
options.ConnectionString = Environment.GetEnvironmentVariable("COSMOS_CONNECTION_STRING");
options.DatabaseName = Environment.GetEnvironmentVariable("COSMOS_DB_NAME");
options.ContainerName = Environment.GetEnvironmentVariable("COSMOS_CONTAINER_NAME");
});
// Configure Cosmos DB
builder.Services.AddSingleton<CosmosClient>(sp =>
{
var cosmosSettings = sp.GetRequiredService<IOptions<CosmosDbSettings>>().Value;
var cosmosClientOptions = new CosmosClientOptions
{
SerializerOptions = new CosmosSerializationOptions
{
PropertyNamingPolicy = CosmosPropertyNamingPolicy.CamelCase
}
};
return new CosmosClient(
cosmosSettings.ConnectionString,
cosmosClientOptions
);
});
// CosmosDbService is a singleton, 1 instance created at startup.
// Register Azure Blob Storage service as a singleton. AzureBlobStorageService instantiated.
builder.Services.AddSingleton<CosmosDbService>();
// get Azure Blob Storage credentials from environment variables
builder.Services.Configure<AzureBlobStorageSettings>(options =>
{
options.ConnectionString = Environment.GetEnvironmentVariable("BLOB_CONNECTION_STRING");
options.ContainerName = Environment.GetEnvironmentVariable("BLOB_CONTAINER_NAME");
});
builder.Services.AddSingleton<IAzureBlobStorageService, AzureBlobStorageService>();
// EpisodeService and IEpisodeService are scoped services,
// 1 instance per HTTP request.
builder.Services.AddScoped<IEpisodeService, EpisodeService>();
builder.Services.AddScoped<ITranscriptionSubmissionService, TranscriptionSubmissionService>();
// get Azure Speech credentials from environment variables
builder.Services.Configure<AzureSpeechSettings>(options =>
{
options.SubscriptionKey = Environment.GetEnvironmentVariable("AZURE_SPEECH_KEY");
options.Region = Environment.GetEnvironmentVariable("AZURE_SPEECH_REGION");
options.ApiVersion = Environment.GetEnvironmentVariable("AZURE_SPEECH_API_VERSION");
});
// Register AzureSpeechHandlerService with credentials from environment
builder.Services.AddScoped<IAzureSpeechHandlerService, AzureSpeechHandlerService>(sp =>
new AzureSpeechHandlerService(
sp.GetRequiredService<ILogger<AzureSpeechHandlerService>>(),
sp.GetRequiredService<IAzureBlobStorageService>(),
sp.GetRequiredService<IOptions<AzureSpeechSettings>>(),
sp.GetRequiredService<CosmosDbService>()
)
);
builder.Services.Configure<ListennotesSettings>(options =>
{
options.ApiKey = Environment.GetEnvironmentVariable("LISTENNOTES_API_KEY");
options.EndPoint = Environment.GetEnvironmentVariable("LISTENNOTES_END_POINT");
});
// Register ExternalPodcastSearchService with API key from environment
builder.Services.AddScoped<IExternalPodcastSearchService, ExternalPodcastSearchService>(sp =>
new ExternalPodcastSearchService(
sp.GetRequiredService<ILogger<ExternalPodcastSearchService>>(),
sp.GetRequiredService<IOptions<ListennotesSettings>>(),
sp.GetRequiredService<CosmosDbService>()
)
);
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
//
app.Lifetime.ApplicationStarted.Register(async () =>
{
var logger = app.Services.GetRequiredService<ILogger<Program>>();
try
{
var blobService = app.Services.GetRequiredService<IAzureBlobStorageService>();
if (await blobService.InitializeAsync())
logger.LogInformation("*** Successfully connected to Azure Blob Storage");
else
logger.LogError("*** Failed to connect to Azure Blob Storage");
var cosmosClient = app.Services.GetRequiredService<CosmosClient>();
var cosmosSettings = app.Services.GetRequiredService<IOptions<CosmosDbSettings>>().Value;
var container = cosmosClient.GetContainer(cosmosSettings.DatabaseName, cosmosSettings.ContainerName);
await container.ReadContainerAsync();
logger.LogInformation("*** Successfully connected to Cosmos DB");
}
catch (Exception ex)
{
logger.LogError(ex, "*** Initialization error");
}
});
app.UseHttpsRedirection();
app.UseRouting();
app.UseAuthorization();
app.MapControllers();
app.MapGet("/", () => "API is running");
// app.Urls.Add("http://localhost:5050");
app.Urls.Add("http://0.0.0.0:5050");
// app.Urls.Add("https://localhost:7050");
app.Run();