1package soft
 2
 3import (
 4	"fmt"
 5	"log"
 6
 7	"github.com/charmbracelet/soft/config"
 8	"github.com/charmbracelet/soft/git"
 9	"github.com/charmbracelet/soft/tui"
10
11	"github.com/charmbracelet/wish"
12	bm "github.com/charmbracelet/wish/bubbletea"
13	gm "github.com/charmbracelet/wish/git"
14	lm "github.com/charmbracelet/wish/logging"
15	"github.com/gliderlabs/ssh"
16)
17
18// NewServer returns a new *ssh.Server configured to serve Soft Serve. The SSH
19// server key-pair will be created if none exists. An initial admin SSH public
20// key can be provided with authKey. If authKey is provided, access will be
21// restricted to that key. If authKey is not provided, the server will be
22// publicly writable until configured otherwise by cloning the `config` repo.
23func NewServer(host string, port int, serverKeyPath string, repoPath string, authKey string) *ssh.Server {
24	rs := git.NewRepoSource(repoPath)
25	cfg, err := config.NewConfig(host, port, authKey, rs)
26	if err != nil {
27		log.Fatalln(err)
28	}
29	s, err := wish.NewServer(
30		ssh.PublicKeyAuth(cfg.PublicKeyHandler),
31		ssh.PasswordAuth(cfg.PasswordHandler),
32		wish.WithAddress(fmt.Sprintf("%s:%d", host, port)),
33		wish.WithHostKeyPath(serverKeyPath),
34		wish.WithMiddlewares(
35			bm.Middleware(tui.SessionHandler(cfg)),
36			gm.Middleware(repoPath, cfg),
37			lm.Middleware(),
38		),
39	)
40	if err != nil {
41		log.Fatalln(err)
42	}
43	return s
44}