Skip to content

config: Refactor lexer, parser, controller, dispenser types #4

Closed

Description

@mholt

In preparation to move the New() (generator) functions out of middlewares (#3), the config needs to be refactored. Right now, the config package uses different parsing facilities than the generators do, but the ones provided to the generators are more convenient and powerful. It would be nice if all the parsing could be done by the same types.

The generator functions will need access to the entire config, I think, to access things like hostname, port, and actual address that the server is listening on. Without refactoring, this change would result in circular imports.

Finally, this refactoring should allow the middleware packages to share a test harness to be more conveniently tested. It will also allow the generator functions to be put under test in a consistent, convenient way.

We may have to introduce at least one new package to accomplish this.

These changes are completely internal, and no user-facing behavior will change.

Activity

  1. self-assigned this
    on Apr 27, 2015
  2. added
    in progress 🏃‍♂️
    on May 2, 2015
  3. mholt commented on May 3, 2015

    @mholt
    MemberAuthor

    This is nearly complete on my private branch. Still have to bring everything full circle and add tests (much easier now!) but preliminary results look great. If all goes well, here would be the report. You don't have to read this; it's mostly for historical purposes:

    The middleware that Caddy uses are designed to be completely independent. As such, the New() functions - only ever used by Caddy - were moved into a new "setup" package under the "config" folder. The New() functions were renamed to match their directive name, and may now be dubbed "setup functions" or "directive functions" for future reference.

    All directives are now treated the same, whether they invoke middleware or not. Before, directives like "root" and "tls" (which had nothing to do with HTTP handlers) were processed separately and parsed using a primitive and completely different parsing mechanism. Now, all directives - middleware or not - use the same parsing utilities and are organized together as independent, testable functions.

    Setup functions now have full read+write access to the entire server configuration as well as the parsing mechanisms offered before through the Dispenser type. Some improvements have been made there as well.

    The ordering in which directives are executed and in which HTTP middleware handlers are chained together is very important. Fortunately, both the order of execution and handler chaining is the same. So, the order of the directives is very carefully defined in config/directives.go, where each directive is linked to its setup function. A setup function may or may not return a middleware.Middleware type. If it does, it will be chained into the handler stack for that server.

    These changes completely decouple the middleware packages from Caddy, except for the "middleware" helper package which is now much simpler and may soon contain testing facilities, if needed. Middleware handlers can be more easily tested. And just as importantly, the setup functions - all of them - can be put under test without trouble.

    The Config type actually now lives in the "server" package, not the Config package. This is intentional. The refactoring carefully steps around import cycles. The "server" package does not rely on the "config" package. The "config" package imports "parse" and "setup", but the "parse" package is on its own, and "setup" can import "parse" but not "config". The "main" package imports config and server, bridging the two. Anyway, this was fun to figure out.

    With this revised structure, Caddy has more room to breathe and grow, and a much greater capacity for automated tests. This ultimately results in more readable, maintainable, bug-free code.

  4. mholt commented on May 4, 2015

    @mholt
    MemberAuthor

    Done via e4fdf17

  5. removed
    in progress 🏃‍♂️
    on May 4, 2015
  6. added a commit that references this issue on Jun 13, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions