public GifService(BotAppDbContext appDbContext) { this.appDbContext = appDbContext; }
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1); // Auth services.AddAuthentication(options => { options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme; options.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme; options.DefaultChallengeScheme = "Discord"; }) .AddCookie() .AddJwtBearer(options => { options.SaveToken = true; }) .AddOAuth("Discord", options => { options.ClientId = Configuration["Auth:Providers:Discord:ClientId"]; options.ClientSecret = Configuration["Auth:Providers:Discord:ClientSecret"]; options.CallbackPath = new PathString("/auth/discord/callback"); options.Scope.Add("identify"); options.Scope.Add("email"); options.Scope.Add("guilds"); options.SaveTokens = true; options.AuthorizationEndpoint = "https://discordapp.com/oauth2/authorize"; options.TokenEndpoint = "https://discordapp.com/api/oauth2/token"; options.UserInformationEndpoint = "https://discordapp.com/api/users/@me"; options.ClaimActions.MapJsonKey(ClaimTypes.NameIdentifier, "id"); options.ClaimActions.MapJsonKey(ClaimTypes.Name, "username"); options.ClaimActions.MapJsonKey(ClaimTypes.Email, "email"); options.Events = new OAuthEvents() { OnCreatingTicket = async context => { var request = new HttpRequestMessage(HttpMethod.Get, context.Options.UserInformationEndpoint); request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", context.AccessToken); var response = await context.Backchannel.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, context.HttpContext.RequestAborted); response.EnsureSuccessStatusCode(); var user = JObject.Parse(await response.Content.ReadAsStringAsync()); context.RunClaimActions(user); } }; }); services.AddAuthorization(config => { config.AddPolicy("Owner", policy => policy.Requirements.Add(new OwnerRequirement())); }); // Database context BotAppDbContext dbContext = new BotAppDbContext(Configuration["Databases:BotDatabaseConnectionString"]); services.AddSingleton(dbContext); // App services services.AddSingleton <IQuotesService, QuotesService>(); // Swagger services.AddSwagger(); // In production, the Angular files will be served from this directory services.AddSpaStaticFiles(configuration => { configuration.RootPath = "ClientApp/dist"; }); }
public QuotesService(BotAppDbContext appDbContext) { _appDbContext = appDbContext; }
private void InitializeServices(ServiceCollection serviceCollection) { Console.WriteLine("Initializing services...", Color.Blue); // Configuration builder var builder = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("appsettings.json", false, true); configuration = builder.Build(); serviceCollection.AddSingleton(configuration); Console.WriteLine("Added configuration"); // Database var botAppDbContext = new BotAppDbContext(); serviceCollection.AddSingleton(botAppDbContext); botAppDbContext.Database.EnsureCreated(); Console.WriteLine("Added database context"); // Storage var storageDb = new LiteDatabase("app.storage.db"); serviceCollection.AddSingleton(storageDb); Console.WriteLine("Added file storage"); // Discord serviceCollection.AddSingleton(discordClient); Console.WriteLine("Added Discord client"); // Reddit var redditWebAgent = new BotWebAgent(configuration["Reddit:Username"], configuration["Reddit:Password"], configuration["Reddit:ClientId"], configuration["Reddit:ClientSecret"], configuration["Reddit:RedirectUri"]); Reddit redditClient = new Reddit(redditWebAgent); serviceCollection.AddSingleton(redditClient); Console.WriteLine("Added Reddit client"); // Giphy Giphy giphy = new Giphy(configuration["GiphyToken"]); serviceCollection.AddSingleton(giphy); Console.WriteLine("Added Giphy client"); // YouTube //YouTubeService youTubeService = new YouTubeService(new BaseClientService.Initializer //{ // ApplicationName = "Useless Bot", // ApiKey = configuration["YouTubeApiKey"] //}); //serviceCollection.AddSingleton(youTubeService); // App services serviceCollection.AddSingleton <IFileStorageService, FileStorageService>(); serviceCollection.AddSingleton <IQuotesService, QuotesService>(); serviceCollection.AddSingleton <IGifService, GifService>(); serviceCollection.AddSingleton <IRedditService, RedditService>(); Console.WriteLine("Added app services"); // Build the service provider Services = serviceCollection.BuildServiceProvider(); Console.WriteLine("Services were initialized", Color.Green); }