server

package
v0.0.0-...-c97f954 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 25, 2025 License: AGPL-3.0 Imports: 13 Imported by: 0

README

Expresso Server Configuration System Overview

This document explains how the configuration system works for the Expresso server. It follows a flexible pattern that allows easy extension when adding new features.

Core Configuration Structure

The core configuration lives in internal/server/config.go as a struct:

type Config struct {
    host, port          string
    developmentMode     bool
    siweStatement       string
    siweMaxValidity     time.Duration
    // more ...
}

Configuration System Components

1. Factory Pattern with ConfigOptions

NewConfig() creates a config instance using:

  • Base host/port parameters
  • A list of ConfigOption functions that modify the config

Example:

config, err := NewConfig("localhost", 8080,
    WithDevelopmentMode(),
    WithSignInWithEthereum("statement", 15*time.Minute))
2. CLI Integration

internal/server/cli.go defines command-line flags that map to config options:

// Flags
HostFlag             // -> NewConfig host parameter
PortFlag             // -> NewConfig port parameter
DevelopmentModeFlag  // -> WithDevelopmentMode()
SignInWithEthereum...// -> WithSignInWithEthereum()
3. Configuration Usage

Config is passed to components that need it:

  • server.Run() uses config for server settings
  • auth.Register() uses config via the AuthConfig interface
  • Features access config values through getters

Adding New Configuration Options

To add a new configuration option (e.g., key):

Step 1: Update Config Struct
// internal/server/config.go
type Config struct {
    ...
    key string
}
Step 2: Create ConfigOption Function and Getter
// internal/server/config.go
func WithKey(key string) ConfigOption {
    return func(c *Config) error {
        c.key = key
        return nil
    }
}

func (c Config) GetKey() string {
    return c.key
}
Step 3: Add CLI Flag Support
// internal/server/cli.go
KeyFlag = &cli.StringFlag{
    Name:  "key",
    Usage: "set custom key value for feature X",
}
Step 4: Use in Command Handling

Note: Validation of value should be done inside of the ConfigOption not when processing the CLI values. CLI processing should be used for making sure mutual exclusion occurs, or other CLI related features.

// internal/server/cli.go
options = append(options, WithKey(cmd.String("key")))
Step 5: Use the Config Value

In your feature code:

// internal/myNewFeature/feature.go
package feature

type MyNewFeatureConfig interface {
    GetKey() string
}

func MyFunction(config *MyNewFeatureConfig) {
    // example in a middleware or handler
    configKey := config.GetKey()
}
// internal/server/server.go

import "github.com/edablez/go-expresso/internal/myNewFeature"

func Run(config *Config) error {
    // code above...

    // your code here
    // the config struct will adhere since you added the Getter required in config.go
    myNewFeature.MyFunction(config)

    // code below...
}

## Configuration Flow

1. CLI flags are parsed into command-line values
2. Values are converted to ConfigOption functions
3. `NewConfig()` builds the config struct
4. Config is passed to components that need it
5. Features access config values through getters

## Best Practices

- Use `ConfigOption` functions for validation
- Keep config struct fields lowercase (private)
- Add getters for external access
- Document all options in the CLI help text

The system is designed to be extensible while maintaining strong validation and clear separation between configuration creation, command-line parsing, and feature implementation.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	HostFlag = &cli.StringFlag{
		Name:     "host",
		Usage:    "specify the host address for the server to listen on",
		Sources:  cli.EnvVars("EXPRESSO_HOST"),
		Value:    defaultHost,
		OnlyOnce: true,
	}
	PortFlag = &cli.Uint16Flag{
		Name:     "port",
		Usage:    "specify the port number for the server to listen on",
		Sources:  cli.EnvVars("EXPRESSO_PORT"),
		Value:    defaultPort,
		OnlyOnce: true,
	}
	DevelopmentModeFlag = &cli.BoolFlag{
		Name:     "dev",
		Usage:    "enable development mode for the server",
		Sources:  cli.EnvVars("EXPRESSO_DEV"),
		OnlyOnce: true,
	}
	SignInWithEthereumStatementFlag = &cli.StringFlag{
		Name:     "siwe-statement",
		Usage:    "specify the statement clients must use when generating their SIWE signature",
		Sources:  cli.EnvVars("EXPRESSO_SIWE_STATEMENT"),
		OnlyOnce: true,
	}
	SignInWithEthereumMaxValidityFlag = &cli.DurationFlag{
		Name:     "siwe-max-validity",
		Usage:    "specify the maximum duration accepted SIWE signatures may be valid for",
		Sources:  cli.EnvVars("EXPRESSO_SIWE_MAX_VALIDITY"),
		OnlyOnce: true,
	}
)

Functions

func NewCommand

func NewCommand() *cli.Command

func Run

func Run(config *Config) error

Run starts the HTTP server using the provided host and port. It returns an error if the server fails to start.

Types

type Config

type Config struct {
	// contains filtered or unexported fields
}

Config holds configuration settings for the server.

func NewConfig

func NewConfig(host string, port uint16, options ...ConfigOption) (*Config, error)

NewConfig creates a new Config instance with the provided host and port, and applies any configuration options.

func (Config) GetDevelopmentMode

func (c Config) GetDevelopmentMode() bool

GetDevelopmentMode returns whether the server is running in development mode.

func (Config) GetHost

func (c Config) GetHost() string

GetHost returns the host configuration for the server.

func (Config) GetPort

func (c Config) GetPort() string

GetPort returns the port configuration for the server as a string.

func (Config) GetSIWE

func (c Config) GetSIWE() (string, time.Duration)

GetSIWE returns the SIWE statement and maximum validity duration for Ethereum-based sign-in.

type ConfigOption

type ConfigOption func(*Config) error

ConfigOption is a functional option pattern type used to configure various aspects of the server.

func WithDevelopmentMode

func WithDevelopmentMode() ConfigOption

WithDevelopmentMode enables development mode for the server configuration.

func WithSignInWithEthereum

func WithSignInWithEthereum(statement string, maxValidity time.Duration) ConfigOption

WithSignInWithEthereum configures the server to use Ethereum-based sign-in with a given statement and maximum signature validity.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL