|
| 1 | +// Copyright 2019 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | +using System.Collections.Generic; |
| 15 | +using Amathus.Common.FeedStore; |
| 16 | +using Amathus.Common.Sources; |
| 17 | +using Microsoft.AspNetCore.Builder; |
| 18 | +using Microsoft.AspNetCore.Hosting; |
| 19 | +using Microsoft.Extensions.Configuration; |
| 20 | +using Microsoft.Extensions.DependencyInjection; |
| 21 | +using Microsoft.Extensions.Hosting; |
| 22 | +using Microsoft.Extensions.Logging; |
| 23 | +using Amathus.Common.Converter; |
| 24 | +using System; |
| 25 | + |
| 26 | +namespace Amathus.Converter |
| 27 | +{ |
| 28 | + public class Startup |
| 29 | + { |
| 30 | + private FeedStoreBackend _backend; |
| 31 | + private List<Source> _sources; |
| 32 | + |
| 33 | + public Startup(IConfiguration configuration) |
| 34 | + { |
| 35 | + Configuration = configuration; |
| 36 | + } |
| 37 | + |
| 38 | + public IConfiguration Configuration { get; } |
| 39 | + |
| 40 | + public void ConfigureServices(IServiceCollection services) |
| 41 | + { |
| 42 | + services.AddControllers(); |
| 43 | + |
| 44 | + |
| 45 | + services.AddSingleton<ISyndFeedStore>(container => |
| 46 | + { |
| 47 | + var projectId = Configuration["Amathus:ProjectId"]; |
| 48 | + var bucketId = Configuration["Amathus:BucketId"]; |
| 49 | + var logger = container.GetRequiredService<ILogger<ISyndFeedStore>>(); |
| 50 | + return new CloudStorageSyndFeedStore(projectId, bucketId, logger); |
| 51 | + }); |
| 52 | + |
| 53 | + _sources = Configuration.GetSection("Amathus:Sources").Get<List<Source>>(); |
| 54 | + |
| 55 | + services.AddSingleton<IFeedConverter>(container => |
| 56 | + { |
| 57 | + var logger = container.GetRequiredService<ILogger<IFeedConverter>>(); |
| 58 | + return new FeedConverter(_sources, logger); |
| 59 | + }); |
| 60 | + |
| 61 | + _backend = Enum.Parse<FeedStoreBackend>(Configuration["Amathus:FeedStore"], ignoreCase: true); |
| 62 | + |
| 63 | + switch (_backend) |
| 64 | + { |
| 65 | + case FeedStoreBackend.Firestore: |
| 66 | + services.AddSingleton<IFeedStore>(provider => |
| 67 | + new FirestoreFeedStore( |
| 68 | + Configuration["Amathus:ProjectId"])); |
| 69 | + break; |
| 70 | + default: |
| 71 | + throw new ArgumentException("Backend cannot be initialized"); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILogger<Startup> logger) |
| 76 | + { |
| 77 | + if (env.IsDevelopment()) |
| 78 | + { |
| 79 | + app.UseDeveloperExceptionPage(); |
| 80 | + } |
| 81 | + |
| 82 | + logger.LogInformation("Starting..."); |
| 83 | + logger.LogInformation("Sources: " + _sources?.Count); |
| 84 | + logger.LogInformation("Backend: " + _backend); |
| 85 | + |
| 86 | + app.UseRouting(); |
| 87 | + |
| 88 | + app.UseEndpoints(endpoints => |
| 89 | + { |
| 90 | + endpoints.MapControllers(); |
| 91 | + }); |
| 92 | + } |
| 93 | + } |
| 94 | +} |
0 commit comments