Exemple #1
0
 public Seeder(
     WikiDbContext dbContext,
     IConfiguration configuration,
     HTTPService http,
     MarkDownDocGenerator markDownGenerator,
     EventService eventService,
     AppsContainer appsContainer)
 {
     _dbContext         = dbContext;
     _configuration     = configuration;
     _http              = http;
     _markDownGenerator = markDownGenerator;
     _eventService      = eventService;
     _appsContainer     = appsContainer;
 }
        public async Task Invoke(HttpContext context)
        {
            if (_isAPIAction == null || _judgeAuthorized == null)
            {
                throw new ArgumentNullException();
            }
            if (context.Request.Path.ToString().Trim().Trim('/').ToLower() != _docAddress)
            {
                await _next.Invoke(context);

                return;
            }
            switch (_format)
            {
            case DocFormat.Json:
                context.Response.ContentType = "application/json";
                break;

            case DocFormat.Markdown:
                context.Response.ContentType = "text/markdown";
                break;

            default:
                throw new InvalidDataException($"Invalid format: '{_format}'!");
            }
            context.Response.StatusCode = 200;
            var actionsMatches      = new List <API>();
            var possibleControllers = Assembly.GetEntryAssembly()?.GetTypes().Where(type => typeof(ControllerBase).IsAssignableFrom(type));

            foreach (var controller in possibleControllers ?? new List <Type>())
            {
                if (!IsController(controller))
                {
                    continue;
                }
                var controllerRoute = controller.GetCustomAttributes(typeof(RouteAttribute), true)
                                      .Select(t => t as RouteAttribute)
                                      .Select(t => t?.Template)
                                      .FirstOrDefault();
                foreach (var method in controller.GetMethods(BindingFlags.Instance | BindingFlags.DeclaredOnly | BindingFlags.Public))
                {
                    if (!IsAction(method) || !_isAPIAction(method, controller))
                    {
                        continue;
                    }
                    var args = GenerateArguments(method);
                    var possibleResponses = GetPossibleResponses(method);
                    var api = new API
                    {
                        ControllerName = controller.Name,
                        ActionName     = method.Name,
                        IsPost         = method.CustomAttributes.Any(t => t.AttributeType == typeof(HttpPostAttribute)),
                        Routes         = method.GetCustomAttributes(typeof(RouteAttribute), true)
                                         .Select(t => t as RouteAttribute)
                                         .Select(t => t?.Template)
                                         .Select(t => $"{controllerRoute}/{t}")
                                         .ToList(),
                        Arguments         = args,
                        AuthRequired      = _judgeAuthorized(method, controller),
                        PossibleResponses = possibleResponses
                    };
                    if (!api.Routes.Any())
                    {
                        api.Routes.Add($"{api.ControllerName.TrimController()}/{api.ActionName}");
                    }
                    actionsMatches.Add(api);
                }
            }
            var generatedJsonDoc = await Task.Factory.StartNew(() => JsonConvert.SerializeObject(actionsMatches));

            if (_format == DocFormat.Json)
            {
                await context.Response.WriteAsync(generatedJsonDoc);
            }
            else if (_format == DocFormat.Markdown)
            {
                var    generator          = new MarkDownDocGenerator();
                var    groupedControllers = actionsMatches.GroupBy(t => t.ControllerName);
                string finalMarkDown      = string.Empty;
                foreach (var controllerDoc in groupedControllers)
                {
                    finalMarkDown += generator.GenerateMarkDownForAPI(controllerDoc, $"{context.Request.Scheme}://{context.Request.Host}") + "\r\n--------\r\n";
                }
                await context.Response.WriteAsync(finalMarkDown);
            }
        }