Skip to content

Commit

Permalink
fix: rename host to bind address
Browse files Browse the repository at this point in the history
and use env to apply defaults
  • Loading branch information
aymanbagabas committed Feb 1, 2022
1 parent 8e032c2 commit 57b1eff
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 25 deletions.
4 changes: 2 additions & 2 deletions cmd/soft/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func main() {
done := make(chan os.Signal, 1)
signal.Notify(done, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)

log.Printf("Starting SSH server on %s:%d", cfg.Host, cfg.Port)
log.Printf("Starting SSH server on %s:%d", cfg.BindAddr, cfg.Port)
go func() {
if err := s.Start(); err != nil {
log.Fatalln(err)
Expand All @@ -66,7 +66,7 @@ func main() {

<-done

log.Printf("Stopping SSH server on %s:%d", cfg.Host, cfg.Port)
log.Printf("Stopping SSH server on %s:%d", cfg.BindAddr, cfg.Port)
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer func() { cancel() }()
if err := s.Shutdown(ctx); err != nil {
Expand Down
30 changes: 10 additions & 20 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,36 +16,26 @@ type Callbacks interface {

// Config is the configuration for Soft Serve.
type Config struct {
Host string `env:"SOFT_SERVE_HOST"`
Port int `env:"SOFT_SERVE_PORT"`
BindAddr string `env:"SOFT_SERVE_BIND_ADDRESS" envDefault:""`
Port int `env:"SOFT_SERVE_PORT" envDefault:"23231"`
KeyPath string `env:"SOFT_SERVE_KEY_PATH"`
RepoPath string `env:"SOFT_SERVE_REPO_PATH"`
RepoPath string `env:"SOFT_SERVE_REPO_PATH" envDefault:".repos"`
InitialAdminKeys []string `env:"SOFT_SERVE_INITIAL_ADMIN_KEY" envSeparator:"\n"`
Callbacks Callbacks
}

func (c *Config) applyDefaults() {
if c.Port == 0 {
c.Port = 23231
}
if c.KeyPath == "" {
// NB: cross-platform-compatible path
c.KeyPath = filepath.Join(".ssh", "soft_serve_server_ed25519")
}
if c.RepoPath == "" {
c.RepoPath = ".repos"
}
}

// DefaultConfig returns a Config with the values populated with the defaults
// or specified environment variables.
func DefaultConfig() *Config {
var scfg Config
if err := env.Parse(&scfg); err != nil {
var cfg Config
if err := env.Parse(&cfg); err != nil {
log.Fatalln(err)
}
scfg.applyDefaults()
return scfg.WithCallbacks(nil)
if cfg.KeyPath == "" {
// NB: cross-platform-compatible path
cfg.KeyPath = filepath.Join(".ssh", "soft_serve_server_ed25519")
}
return cfg.WithCallbacks(nil)
}

// WithCallbacks applies the given Callbacks to the configuration.
Expand Down
4 changes: 2 additions & 2 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func NewConfig(cfg *config.Config) (*Config, error) {
var anonAccess string
var yamlUsers string
var displayHost string
host := cfg.Host
host := cfg.BindAddr
port := cfg.Port

pks := make([]string, 0, len(cfg.InitialAdminKeys))
Expand All @@ -72,7 +72,7 @@ func NewConfig(cfg *config.Config) (*Config, error) {
c := &Config{
Cfg: cfg,
}
c.Host = cfg.Host
c.Host = cfg.BindAddr
c.Port = port
c.Source = rs
if len(pks) == 0 {
Expand Down
2 changes: 1 addition & 1 deletion server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func NewServer(cfg *config.Config) *Server {
s, err := wish.NewServer(
ssh.PublicKeyAuth(ac.PublicKeyHandler),
ssh.PasswordAuth(ac.PasswordHandler),
wish.WithAddress(fmt.Sprintf("%s:%d", cfg.Host, cfg.Port)),
wish.WithAddress(fmt.Sprintf("%s:%d", cfg.BindAddr, cfg.Port)),
wish.WithHostKeyPath(cfg.KeyPath),
wish.WithMiddleware(mw...),
)
Expand Down

0 comments on commit 57b1eff

Please sign in to comment.