12345678910111213141516171819202122232425262728293031323334353637383940414243444546 |
- using System;
- using System.Collections.Generic;
- using Microsoft.AspNetCore.Http;
- using Microsoft.Extensions.Configuration;
- using Microsoft.Extensions.Logging;
- namespace BubbleSocketCore.Handlers
- {
- class HandlerFactory
- {
- private static List<IHandler> handlers = new List<IHandler>();
- private readonly IConfiguration Configuration;
- private readonly ILoggerFactory LoggerFactory;
- private readonly ILogger Logger;
- public HandlerFactory(IConfiguration configuration, ILoggerFactory loggerFactory)
- {
- Configuration = configuration;
- LoggerFactory = loggerFactory;
- Logger = LoggerFactory.CreateLogger("HandlerFactory");
- handlers.Add(new DummyHandler());
- handlers.Add(new EchoHandler());
- handlers.Add(new PlayerConnectionHandler(Configuration, LoggerFactory));
- }
- internal IHandler Get(HttpRequest request)
- {
- foreach (var handler in handlers)
- {
- if (handler.IsListening(request))
- {
- return handler;
- }
- }
- throw new HandlerNotFoundException($"{request.Scheme}://{request.Host}{request.Path}");
- }
- }
- class HandlerNotFoundException : Exception
- {
- public HandlerNotFoundException(string message) : base(message)
- {
- }
- }
- }
|