feat: add NewServerFromConfig function for embedded server config processing#7364
Merged
neilalexander merged 1 commit intonats-io:mainfrom Oct 1, 2025
Merged
Conversation
…cessing This commit introduces NewServerFromConfig() function that automatically processes configuration files for embedded NATS servers, eliminating the need for manual LoadConfig() calls. Key changes: - Add NewServerFromConfig() function in server/server.go - Function automatically calls opts.ProcessConfigFile() when ConfigFile is set and configDigest is empty - Add comprehensive test coverage in server/opts_test.go: * TestNewServerFromConfigFunctionality: tests basic functionality and error handling * TestNewServerFromConfigVsLoadConfig: validates equivalence with LoadConfig approach The new function provides a cleaner API for embedded servers while maintaining full backward compatibility with existing LoadConfig workflows. Signed-off-by: orician <lzjzxOYX201905@gmail.com>
| return s | ||
| } | ||
|
|
||
| func NewServerFromConfig(opts *Options) (*Server, error) { |
Member
There was a problem hiding this comment.
It's an odd API shape here to take both an *Options and expect a filename, do we want the function instead to just take a filename string or is there a specific reason for this?
Contributor
Author
There was a problem hiding this comment.
It's an odd API shape here to take both an
*Optionsand expect a filename, do we want the function instead to just take afilename stringor is there a specific reason for this?
Because I want the NATS server, when embedded in a Go program, to accept both configuration file input and programmatic input via the *Options struct.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Add NewServerFromConfig function for embedded server config processing
Overview
Resolves #7092
This PR introduces a new
NewServerFromConfig()function that automatically processes configuration files for embedded NATS servers.Problem
When using NATS embedded in Go applications, developers (such as me) often pass the
ConfigFilefield in theOptionsstruct, butNewServer(opts *Options)doesn't automatically process this configuration file. This leads to confusion and requires additional manual steps to load the configuration.Solution
Approach Comparison
In pr #7333, a solution was proposed to directly modify
NewServer():However, directly modifying
NewServer()might raise concerns about backward compatibility. Therefore, this PR introduces a dedicatedNewServerFromConfig()function specifically to handle theConfigFilefield of*Options.Implementation
This PR adds:
Testing
Added comprehensive test coverage:
TestNewServerFromConfigFunctionality: Tests basic functionality and error handling scenariosTestNewServerFromConfigVsLoadConfig: Validates equivalence with traditionalLoadConfig()approach using deep equality checksAcknowledgments
Thanks to @ripienaar for the suggestion to create a separate function instead of modifying
NewServer()directly.Signing Off
Signed-off-by: orician lzjzxOYX201905@gmail.com