Skip to content

feat: add NewServerFromConfig function for embedded server config processing#7364

Merged
neilalexander merged 1 commit intonats-io:mainfrom
yixianOu:feature/NewServerFromConfig
Oct 1, 2025
Merged

feat: add NewServerFromConfig function for embedded server config processing#7364
neilalexander merged 1 commit intonats-io:mainfrom
yixianOu:feature/NewServerFromConfig

Conversation

@yixianOu
Copy link
Copy Markdown
Contributor

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 ConfigFile field in the Options struct, but NewServer(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():

func NewServer(opts *Options) (*Server, error) {
    if opts.ConfigFile != _EMPTY_ && opts.configDigest == "" {
        if err := opts.ProcessConfigFile(opts.ConfigFile); err != nil {
            return nil, err
        }
    }
    // ...original code
}

However, directly modifying NewServer() might raise concerns about backward compatibility. Therefore, this PR introduces a dedicated NewServerFromConfig() function specifically to handle the ConfigFile field of *Options.

Implementation

This PR adds:

func NewServerFromConfig(opts *Options) (*Server, error) {
    if opts.ConfigFile != _EMPTY_ && opts.configDigest == "" {
        if err := opts.ProcessConfigFile(opts.ConfigFile); err != nil {
            return nil, err
        }
    }
    return NewServer(opts)
}

Testing

Added comprehensive test coverage:

  • TestNewServerFromConfigFunctionality: Tests basic functionality and error handling scenarios
  • TestNewServerFromConfigVsLoadConfig: Validates equivalence with traditional LoadConfig() approach using deep equality checks

Acknowledgments

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

…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>
@yixianOu yixianOu requested a review from a team as a code owner September 28, 2025 16:42
return s
}

func NewServerFromConfig(opts *Options) (*Server, error) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

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.

Copy link
Copy Markdown
Member

@neilalexander neilalexander left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@neilalexander neilalexander merged commit d2e3de9 into nats-io:main Oct 1, 2025
23 checks passed
@yixianOu yixianOu deleted the feature/NewServerFromConfig branch October 1, 2025 15:43
neilalexander added a commit that referenced this pull request Oct 3, 2025
Includes the following:

- #7374
- #7373
- #7377
- #7380
- #7382
- #7381
- #7364
- #7384
- #7385
- #7388
- #7386
- #7391
- #7242

Signed-off-by: Neil Twigg <neil@nats.io>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Go client sdk built-in nats-server unable to read configuration file by passing ConfigFile Options.

2 participants