Advertisement
Guest User

Startup.cs

a guest
Dec 27th, 2017
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.65 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using Microsoft.AspNetCore.Builder;
  6. using Microsoft.AspNetCore.Hosting;
  7. using Microsoft.Extensions.Configuration;
  8. using Microsoft.Extensions.DependencyInjection;
  9. using FindMyPlayer.Models;
  10. using FindMyPlayer.Controllers.Utils;
  11. using RiotGamesApi;
  12. using RiotGamesApi.AspNetCore;
  13. using RiotGamesApi.RateLimit;
  14. using RiotGamesApi.Enums;
  15. using RiotGamesApi.Libraries.Lol.Enums;
  16. using Microsoft.Extensions.Logging;
  17.  
  18. namespace FindMyPlayer {
  19.     public class Startup {
  20.  
  21.         public Startup(IHostingEnvironment env) {
  22.  
  23.             var builder = new ConfigurationBuilder()
  24.  
  25.                 .SetBasePath( env.ContentRootPath )
  26.  
  27.                 .AddJsonFile( "appsettings.json", optional: false, reloadOnChange: true )
  28.  
  29.                 .AddJsonFile( $"appsettings.{env.EnvironmentName}.json", optional: true )
  30.  
  31.                 .AddEnvironmentVariables();
  32.  
  33.             Configuration = builder.Build();
  34.  
  35.         }
  36.  
  37.  
  38.  
  39.         public IConfigurationRoot Configuration { get; }
  40.  
  41.  
  42.  
  43.         // This method gets called by the runtime. Use this method to add services to the container.
  44.  
  45.         public void ConfigureServices(IServiceCollection services) {
  46.  
  47.             // Add framework services.
  48.  
  49.             services.AddMvc();
  50.  
  51.  
  52.  
  53.             //necessary
  54.  
  55.             services.AddLeagueOfLegendsApi( "RGAPI-440fcd4b-a43f-4d80-92b5-bd50c0df1893",
  56.  
  57.             (cache) => {
  58.  
  59.                 //overrides default values
  60.  
  61.                 cache.EnableStaticApiCaching = true;
  62.  
  63.                 cache.StaticApiCacheExpiry = new TimeSpan( 1, 0, 0 );
  64.  
  65.  
  66.  
  67.                 //custom caching is activated
  68.  
  69.                 //working for any api except static-api
  70.  
  71.                 cache.EnableCustomApiCaching = true;
  72.  
  73.                 //summoner-profiles are cached for 5sec
  74.  
  75.                 cache.AddCacheRule( LolUrlType.NonStatic, LolApiName.Summoner, new TimeSpan( 0, 0, 5 ) );
  76.  
  77.                 return cache;
  78.  
  79.             },
  80.  
  81.             (limits) => {
  82.  
  83.                 limits.AddRateLimitFor( LolUrlType.Static, LolApiName.StaticData,
  84.  
  85.                     new List<ApiLimit>()
  86.  
  87.                     {
  88.  
  89.                         new ApiLimit(new TimeSpan(1, 0, 0),10,RateLimitType.MethodRate)
  90.  
  91.                     }, LolApiMethodName.Champions,
  92.  
  93.                     LolApiMethodName.Items,
  94.  
  95.                     LolApiMethodName.LanguageStrings,
  96.  
  97.                     LolApiMethodName.LanguageStrings,
  98.  
  99.                     LolApiMethodName.Maps,
  100.  
  101.                     LolApiMethodName.Masteries,
  102.  
  103.                     LolApiMethodName.ProfileIcons,
  104.  
  105.                     LolApiMethodName.Realms,
  106.  
  107.                     LolApiMethodName.Runes,
  108.  
  109.                     LolApiMethodName.SummonerSpells,
  110.  
  111.                     LolApiMethodName.Versions );
  112.  
  113.  
  114.  
  115.                 limits.AddRateLimitFor( LolUrlType.NonStatic, LolApiName.Match,
  116.  
  117.                     new List<ApiLimit>()
  118.  
  119.                     {
  120.  
  121.                         new ApiLimit(new TimeSpan(0, 0, 10),500, RateLimitType.MethodRate)
  122.  
  123.                     }, LolApiMethodName.Matches, LolApiMethodName.Timelines );
  124.  
  125.  
  126.  
  127.                 limits.AddRateLimitFor( LolUrlType.NonStatic, LolApiName.Match,
  128.  
  129.                     new List<ApiLimit>()
  130.  
  131.                     {
  132.  
  133.                         new ApiLimit(new TimeSpan(0, 0, 10),1000, RateLimitType.MethodRate)
  134.  
  135.                     }, LolApiMethodName.MatchLists );
  136.  
  137.  
  138.  
  139.                 limits.AddRateLimitFor( LolUrlType.Status, LolApiName.Status, new List<ApiLimit>()
  140.  
  141.                 {
  142.  
  143.                     new ApiLimit(new TimeSpan(0, 0, 10), 20000, RateLimitType.MethodRate),
  144.  
  145.                     new ApiLimit(new TimeSpan(0, 2, 0), 100, RateLimitType.AppRate),
  146.  
  147.                     new ApiLimit(new TimeSpan(0, 0, 1), 20, RateLimitType.AppRate)
  148.  
  149.                 }, LolApiMethodName.ShardData );
  150.  
  151.  
  152.  
  153.                 limits.AddRateLimitFor( LolUrlType.Tournament, new List<LolApiName>()
  154.  
  155.                     {
  156.  
  157.                         LolApiName.Tournament,
  158.  
  159.                         LolApiName.TournamentStub
  160.  
  161.                     }, new List<ApiLimit>()
  162.  
  163.                     {
  164.  
  165.                         new ApiLimit(new TimeSpan(0, 0, 10), 20000, RateLimitType.MethodRate),
  166.  
  167.                         new ApiLimit(new TimeSpan(0, 2, 0), 100, RateLimitType.AppRate),
  168.  
  169.                         new ApiLimit(new TimeSpan(0, 0, 1), 20, RateLimitType.AppRate)
  170.  
  171.                     },
  172.  
  173.                     LolApiMethodName.Codes,
  174.  
  175.                     LolApiMethodName.LobbyEvents,
  176.  
  177.                     LolApiMethodName.Providers,
  178.  
  179.                     LolApiMethodName.Tournaments );
  180.  
  181.  
  182.  
  183.                 limits.AddRateLimitFor( LolUrlType.NonStatic, new List<LolApiName>()
  184.  
  185.                     {
  186.  
  187.                         LolApiName.ChampionMastery,
  188.  
  189.                         LolApiName.Spectator,
  190.  
  191.                         LolApiName.Summoner,
  192.  
  193.                         LolApiName.Platform,
  194.  
  195.                     }, new List<ApiLimit>()
  196.  
  197.                     {
  198.  
  199.                         new ApiLimit(new TimeSpan(0, 0, 10), 20000, RateLimitType.MethodRate),
  200.  
  201.                         new ApiLimit(new TimeSpan(0, 2, 0), 100, RateLimitType.AppRate),
  202.  
  203.                         new ApiLimit(new TimeSpan(0, 0, 1), 20, RateLimitType.AppRate)
  204.  
  205.                     },
  206.  
  207.                     LolApiMethodName.ChampionMasteries,
  208.  
  209.                     LolApiMethodName.Scores,
  210.  
  211.                     LolApiMethodName.ActiveGames,
  212.  
  213.                     LolApiMethodName.FeaturedGames,
  214.  
  215.                     LolApiMethodName.Summoners
  216.  
  217.                 );
  218.  
  219.  
  220.  
  221.                 //lol/platform/v3/champions
  222.  
  223.                 //lol/platform/v3/masteries/by-summoner/{summonerId}
  224.  
  225.                 //lol/platform/v3/runes/by-summoner/{summonerId}
  226.  
  227.                 limits.AddRateLimitFor( LolUrlType.NonStatic, new List<LolApiName>()
  228.  
  229.                     {
  230.  
  231.                         LolApiName.Platform
  232.  
  233.                     }, new List<ApiLimit>()
  234.  
  235.                     {
  236.  
  237.                         new ApiLimit(new TimeSpan(0,1,0),400, RateLimitType.MethodRate),
  238.  
  239.                         new ApiLimit(new TimeSpan(0, 2, 0), 100, RateLimitType.AppRate),
  240.  
  241.                         new ApiLimit(new TimeSpan(0, 0, 1), 20, RateLimitType.AppRate)
  242.  
  243.                     }, LolApiMethodName.Champions,
  244.  
  245.                     LolApiMethodName.Masteries,
  246.  
  247.                     LolApiMethodName.Runes );
  248.  
  249.  
  250.  
  251.                 limits.LeaguesApiLimitsInMinute();
  252.  
  253.                 return limits;
  254.  
  255.             } );
  256.  
  257.         }
  258.  
  259.  
  260.  
  261.         // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
  262.  
  263.         public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) {
  264.  
  265.             loggerFactory.AddConsole( Configuration.GetSection( "Logging" ) );
  266.  
  267.             loggerFactory.AddDebug();
  268.  
  269.  
  270.  
  271.             if (env.IsDevelopment()) {
  272.  
  273.                 app.UseDeveloperExceptionPage();
  274.  
  275.                 app.UseBrowserLink();
  276.  
  277.             } else {
  278.  
  279.                 app.UseExceptionHandler( "/Home/Error" );
  280.  
  281.             }
  282.  
  283.  
  284.  
  285.             app.UseStaticFiles();
  286.  
  287.  
  288.  
  289.             app.UseMvc( routes => {
  290.  
  291.                 routes.MapRoute(
  292.  
  293.                     name: "default",
  294.  
  295.                     template: "{controller=Home}/{action=Index}/{id?}" );
  296.  
  297.             } );
  298.  
  299.             //necessary
  300.  
  301.             app.UseRiotGamesApi();
  302.  
  303.             using (var context = new EntidadesContexto()) {
  304.                 InicializaBD.Initialize( context );
  305.             }
  306.  
  307.         }
  308.  
  309.     }
  310. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement