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