server icon indicating copy to clipboard operation
server copied to clipboard

Implement File based configuration

Open mochi-co opened this issue 1 year ago • 2 comments

Following our discussions and @dgduncan's work in #309, here is a PR which implements a new file-based configuration for the broker.

A runnable example can be found in examples/config.

How it works Rather creating a layer which adds listeners and hooks as they are in the main.go files, I have opted instead to update the hooks and listeners so that they can be applied directly through the server options.

The server options, capabilities, and compatibilities transfer directly from yaml/json into their normal options struct, as though it were being provided as a value created in main.go. Refer to examples/config/config.yaml (or config.json) for all options.

The listeners are defined using a slice Listener.Config, specifying the Type, ID, and Address. TLS can also be specified although I haven't tried it, so it may need a bit of work.

Hooks are defined individually, and split by category. As above, see config.yaml for a full list of options for each hook. This was a bit tricky to implement in a respectable manner, but in the end I decided to simply give hooks their own configuration struct, and allow only one type of each hook, which allows us to provide custom parameters for each (e.g. Auth and Storage.Redis).

I have updated the Dockerfile and added a cmd/docker.main.go as per @dgduncan's original work. I have not tested it.

Caveats

  • If you use file-based configuration, you can only have one of each hook type. However, this should be fine for 99% of users.
  • You can only use built in hooks with file-based configuration, as the type needs to be known in order for it to be applied.
  • You can only use built in listeners, for the reasons above.

Contains breaking changes for the way listeners are initialized (id and address are now in Config).

If you wish to use custom hooks and listeners, you will currently need to modify the code to support these, or just use the standard system of applying them in a custom go file.

I suspect I've missed some edge-cases too, but thought I'd submit it for review :)

mochi-co avatar Dec 25 '23 01:12 mochi-co

Pull Request Test Coverage Report for Build 8320787965

Details

  • 144 of 151 (95.36%) changed or added relevant lines in 9 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage decreased (-0.06%) to 98.614%

Changes Missing Coverage Covered Lines Changed/Added Lines %
config/config.go 71 78 91.03%
<!-- Total: 144 151
Totals Coverage Status
Change from base Build 8319335138: -0.06%
Covered Lines: 5692
Relevant Lines: 5772

💛 - Coveralls

coveralls avatar Dec 25 '23 01:12 coveralls

@mochi-co great job carrying on the work! I'll take a look at this asap.

dgduncan avatar Dec 27 '23 16:12 dgduncan