1package server
2
3import (
4 "context"
5 "fmt"
6 "log"
7
8 "github.com/charmbracelet/soft-serve/config"
9 appCfg "github.com/charmbracelet/soft-serve/internal/config"
10 "github.com/charmbracelet/soft-serve/internal/tui"
11
12 "github.com/charmbracelet/wish"
13 bm "github.com/charmbracelet/wish/bubbletea"
14 gm "github.com/charmbracelet/wish/git"
15 lm "github.com/charmbracelet/wish/logging"
16 "github.com/gliderlabs/ssh"
17)
18
19// Server is the Soft Serve server.
20type Server struct {
21 SSHServer *ssh.Server
22 Config *config.Config
23 config *appCfg.Config
24}
25
26// NewServer returns a new *ssh.Server configured to serve Soft Serve. The SSH
27// server key-pair will be created if none exists. An initial admin SSH public
28// key can be provided with authKey. If authKey is provided, access will be
29// restricted to that key. If authKey is not provided, the server will be
30// publicly writable until configured otherwise by cloning the `config` repo.
31func NewServer(cfg *config.Config) *Server {
32 ac, err := appCfg.NewConfig(cfg)
33 if err != nil {
34 log.Fatal(err)
35 }
36 mw := []wish.Middleware{
37 bm.Middleware(tui.SessionHandler(ac)),
38 gm.Middleware(cfg.RepoPath, ac),
39 lm.Middleware(),
40 }
41 s, err := wish.NewServer(
42 ssh.PublicKeyAuth(ac.PublicKeyHandler),
43 ssh.PasswordAuth(ac.PasswordHandler),
44 wish.WithAddress(fmt.Sprintf("%s:%d", cfg.BindAddr, cfg.Port)),
45 wish.WithHostKeyPath(cfg.KeyPath),
46 wish.WithMiddleware(mw...),
47 )
48 if err != nil {
49 log.Fatalln(err)
50 }
51 return &Server{
52 SSHServer: s,
53 Config: cfg,
54 config: ac,
55 }
56}
57
58// Reload reloads the server configuration.
59func (srv *Server) Reload() error {
60 return srv.config.Reload()
61}
62
63// Start starts the SSH server.
64func (srv *Server) Start() error {
65 return srv.SSHServer.ListenAndServe()
66}
67
68// Shutdown lets the server gracefully shutdown.
69func (srv *Server) Shutdown(ctx context.Context) error {
70 return srv.SSHServer.Shutdown(ctx)
71}