00195a6764e16394fc093b8fef12ca79cc9d949f

Author
Ayman Bagabas <ayman.bagabas@gmail.com>
Committer
Ayman Bagabas <ayman.bagabas@gmail.com>
Date

Message

Add DefaultConfig and make NewServer take a config

Diff

  1diff --git a/cmd/soft/main.go b/cmd/soft/main.go
  2index f39626de1fb135100d5c0dfbf14fcfcc97daeaa7..d563249507ec8c4e6c9d9113b55e1fb450524269 100644
  3--- a/cmd/soft/main.go
  4+++ b/cmd/soft/main.go
  5@@ -4,33 +4,13 @@ import (
  6 	"log"
  7 
  8 	"github.com/charmbracelet/soft"
  9-
 10-	"github.com/meowgorithm/babyenv"
 11 )
 12 
 13-type serverConfig struct {
 14-	Host            string `env:"SOFT_SERVE_HOST" default:""`
 15-	Port            int    `env:"SOFT_SERVE_PORT" default:"23231"`
 16-	KeyPath         string `env:"SOFT_SERVE_KEY_PATH" default:".ssh/soft_serve_server_ed25519"`
 17-	RepoPath        string `env:"SOFT_SERVE_REPO_PATH" default:".repos"`
 18-	InitialAdminKey string `env:"SOFT_SERVE_INITIAL_ADMIN_KEY" default:""`
 19-}
 20-
 21 func main() {
 22-	var cfg serverConfig
 23-	err := babyenv.Parse(&cfg)
 24-	if err != nil {
 25-		log.Fatalln(err)
 26-	}
 27-	s := soft.NewServer(
 28-		cfg.Host,
 29-		cfg.Port,
 30-		cfg.KeyPath,
 31-		cfg.RepoPath,
 32-		cfg.InitialAdminKey,
 33-	)
 34+	cfg := soft.DefaultConfig()
 35+	s := soft.NewServer(cfg)
 36 	log.Printf("Starting SSH server on %s:%d\n", cfg.Host, cfg.Port)
 37-	err = s.ListenAndServe()
 38+	err := s.ListenAndServe()
 39 	if err != nil {
 40 		log.Fatalln(err)
 41 	}
 42diff --git a/server.go b/server.go
 43index 1205fd70047ab703bf8a3a46b3a51064d2320146..4144896e2fc2842867e73b79252f6fbaf2cf6a2f 100644
 44--- a/server.go
 45+++ b/server.go
 46@@ -4,9 +4,11 @@ import (
 47 	"fmt"
 48 	"log"
 49 
 50-	"github.com/charmbracelet/soft/config"
 51-	"github.com/charmbracelet/soft/git"
 52-	"github.com/charmbracelet/soft/tui"
 53+	"github.com/charmbracelet/soft/internal/config"
 54+	"github.com/charmbracelet/soft/internal/git"
 55+	"github.com/charmbracelet/soft/internal/tui"
 56+	"github.com/charmbracelet/soft/stats"
 57+	"github.com/meowgorithm/babyenv"
 58 
 59 	"github.com/charmbracelet/wish"
 60 	bm "github.com/charmbracelet/wish/bubbletea"
 61@@ -15,27 +17,54 @@ import (
 62 	"github.com/gliderlabs/ssh"
 63 )
 64 
 65+// Config is the configuration for the soft-serve.
 66+type Config struct {
 67+	Host            string `env:"SOFT_SERVE_HOST" default:""`
 68+	Port            int    `env:"SOFT_SERVE_PORT" default:"23231"`
 69+	KeyPath         string `env:"SOFT_SERVE_KEY_PATH" default:".ssh/soft_serve_server_ed25519"`
 70+	RepoPath        string `env:"SOFT_SERVE_REPO_PATH" default:".repos"`
 71+	InitialAdminKey string `env:"SOFT_SERVE_INITIAL_ADMIN_KEY" default:""`
 72+	Cfg             *config.Config
 73+	Stats           stats.Stats
 74+}
 75+
 76+// DefaultConfig returns a Config with the values populated with the defaults
 77+// or specified environment variables.
 78+func DefaultConfig() *Config {
 79+	var scfg Config
 80+	err := babyenv.Parse(&scfg)
 81+	if err != nil {
 82+		log.Fatalln(err)
 83+	}
 84+	return &scfg
 85+}
 86+
 87 // NewServer returns a new *ssh.Server configured to serve Soft Serve. The SSH
 88 // server key-pair will be created if none exists. An initial admin SSH public
 89 // key can be provided with authKey. If authKey is provided, access will be
 90 // restricted to that key. If authKey is not provided, the server will be
 91 // publicly writable until configured otherwise by cloning the `config` repo.
 92-func NewServer(host string, port int, serverKeyPath string, repoPath string, authKey string) *ssh.Server {
 93-	rs := git.NewRepoSource(repoPath)
 94-	cfg, err := config.NewConfig(host, port, authKey, rs)
 95+func NewServer(scfg *Config) *ssh.Server {
 96+	rs := git.NewRepoSource(scfg.RepoPath)
 97+	cfg, err := config.NewConfig(scfg.Host, scfg.Port, scfg.InitialAdminKey, rs)
 98 	if err != nil {
 99 		log.Fatalln(err)
100 	}
101+	scfg.Cfg = cfg
102+	mw := []wish.Middleware{
103+		bm.Middleware(tui.SessionHandler(cfg)),
104+		gm.Middleware(scfg.RepoPath, cfg),
105+		lm.Middleware(),
106+	}
107+	if scfg.Stats != nil {
108+		mw = append(mw, stats.Middleware(scfg.Stats))
109+	}
110 	s, err := wish.NewServer(
111 		ssh.PublicKeyAuth(cfg.PublicKeyHandler),
112 		ssh.PasswordAuth(cfg.PasswordHandler),
113-		wish.WithAddress(fmt.Sprintf("%s:%d", host, port)),
114-		wish.WithHostKeyPath(serverKeyPath),
115-		wish.WithMiddleware(
116-			bm.Middleware(tui.SessionHandler(cfg)),
117-			gm.Middleware(repoPath, cfg),
118-			lm.Middleware(),
119-		),
120+		wish.WithAddress(fmt.Sprintf("%s:%d", scfg.Host, scfg.Port)),
121+		wish.WithHostKeyPath(scfg.KeyPath),
122+		wish.WithMiddleware(mw...),
123 	)
124 	if err != nil {
125 		log.Fatalln(err)