// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, ICodeGeneratorService codeGenerator, ICodeAccessService accessService, IOptions <ProjectOptions> options)
        {
            _codeGenerator = codeGenerator;
            _accessService = accessService;
            _options       = options.Value;

            loggerFactory.AddConsole();

            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.Use(async(context, next) =>
            {
                if (!_accessService.IsGeneratedCodeExists())
                {
                    var codes = _codeGenerator.GenerateMany(_options.CodesCount);
                    _accessService.Write(codes);
                }
                await next.Invoke();
            });

            //app.UseCodeGeneratorMiddleware();

            //app.UseMvc();

            app.Run(async(context) =>
            {
                string path = context.Request.Path;
                if (path.Contains("/api/code/"))
                {
                    int id;
                    if (int.TryParse(path.Substring(path.LastIndexOf('/') + 1), out id))
                    {
                        await context.Response.WriteAsync(_accessService.Get(id));
                    }
                }
                else
                {
                    await context.Response.WriteAsync("Error");
                }
            });
            //app.Run(async (context) =>
            //{
            //    await context.Response.WriteAsync("Hey hey/!");
            //});
        }
예제 #2
0
 public string Get(int id)
 {
     return(_accessService.Get(id));
 }