-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
92 lines (82 loc) · 2.22 KB
/
Copy pathmain.go
File metadata and controls
92 lines (82 loc) · 2.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package main
import (
"context"
"fmt"
"log"
"os"
"os/signal"
"path/filepath"
"syscall"
"time"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/wish"
"github.com/charmbracelet/wish/bubbletea"
"github.com/charmbracelet/ssh"
"github.com/joledev/ssh.joledev/internal/tui"
)
func main() {
host := envOr("SSH_HOST", "0.0.0.0")
port := envOr("SSH_PORT", "2222")
keyPath := envOr("SSH_KEY_PATH", ".ssh/host_key")
songsPath := envOr("SONGS_PATH", "")
postsDir := envOr("POSTS_DIR", "posts")
// Make paths absolute relative to binary location
if !filepath.IsAbs(postsDir) {
if exe, err := os.Executable(); err == nil {
postsDir = filepath.Join(filepath.Dir(exe), postsDir)
}
}
if songsPath != "" && !filepath.IsAbs(songsPath) {
if exe, err := os.Executable(); err == nil {
songsPath = filepath.Join(filepath.Dir(exe), songsPath)
}
}
// Also check relative to CWD as fallback
if _, err := os.Stat(postsDir); os.IsNotExist(err) {
if cwd, err := os.Getwd(); err == nil {
postsDir = filepath.Join(cwd, "posts")
}
}
if songsPath == "" {
if cwd, err := os.Getwd(); err == nil {
candidate := filepath.Join(cwd, "data", "songs.txt")
if _, err := os.Stat(candidate); err == nil {
songsPath = candidate
}
}
}
s, err := wish.NewServer(
wish.WithAddress(fmt.Sprintf("%s:%s", host, port)),
wish.WithHostKeyPath(keyPath),
wish.WithMiddleware(
bubbletea.Middleware(func(s ssh.Session) (tea.Model, []tea.ProgramOption) {
m := tui.NewModel(songsPath, postsDir)
return m, []tea.ProgramOption{tea.WithAltScreen()}
}),
),
)
if err != nil {
log.Fatalf("Could not create SSH server: %v", err)
}
done := make(chan os.Signal, 1)
signal.Notify(done, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
log.Printf("Starting SSH server on %s:%s", host, port)
go func() {
if err := s.ListenAndServe(); err != nil {
log.Fatalf("SSH server error: %v", err)
}
}()
<-done
log.Println("Shutting down...")
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := s.Shutdown(ctx); err != nil {
log.Fatalf("Shutdown error: %v", err)
}
}
func envOr(key, fallback string) string {
if v := os.Getenv(key); v != "" {
return v
}
return fallback
}