-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
182 lines (161 loc) · 4.39 KB
/
main.go
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package main
import (
"context"
"database/sql"
"errors"
"net"
"os"
"os/signal"
"syscall"
"time"
_ "modernc.org/sqlite"
"github.com/charmbracelet/bubbles/help"
"github.com/charmbracelet/bubbles/list"
"github.com/charmbracelet/bubbles/textinput"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/charmbracelet/log"
"github.com/charmbracelet/ssh"
"github.com/charmbracelet/wish"
"github.com/charmbracelet/wish/activeterm"
"github.com/charmbracelet/wish/bubbletea"
"github.com/charmbracelet/wish/logging"
"github.com/muesli/termenv"
)
const (
host = "0.0.0.0"
)
func main() {
lipgloss.SetColorProfile(termenv.TrueColor) // fix for fly.io deployments
port, ok := os.LookupEnv("PORT")
if !ok {
port = "23234"
}
db, err := sql.Open("sqlite", "./.ssh/app.db")
if err != nil {
log.Error("Unable to read sqlite database", "error", err)
}
_, err = db.Exec(`
CREATE TABLE IF NOT EXISTS posts (
title TEXT,
rank INTEGER,
username TEXT,
PRIMARY KEY (title, username)
)
`)
if err != nil {
log.Error("Unable to ensure sqlite database schema", "error", err)
}
s, err := wish.NewServer(
wish.WithAddress(net.JoinHostPort(host, port)),
wish.WithHostKeyPath(".ssh/id_ed25519"),
wish.WithMiddleware(
bubbletea.Middleware(SqliteBubbleHandler(db)),
activeterm.Middleware(), // Bubble Tea apps usually require a PTY.
logging.Middleware(),
),
)
if err != nil {
log.Error("Could not start server", "error", err)
}
done := make(chan os.Signal, 1)
signal.Notify(done, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
log.Info("Starting SSH server", "host", host, "port", port)
go func() {
if err = s.ListenAndServe(); err != nil && !errors.Is(err, ssh.ErrServerClosed) {
log.Error("Could not start server", "error", err)
done <- nil
}
}()
<-done
log.Info("Stopping SSH server")
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer func() { cancel() }()
if err := s.Shutdown(ctx); err != nil && !errors.Is(err, ssh.ErrServerClosed) {
log.Error("Could not stop server", "error", err)
}
}
// You can wire any Bubble Tea model up to the middleware with a function that
// handles the incoming ssh.Session. Here we just grab the terminal info and
// pass it to the new model. You can also return tea.ProgramOptions (such as
// tea.WithAltScreen) on a session by session basis.
func SqliteBubbleHandler(db *sql.DB) func(s ssh.Session) (tea.Model, []tea.ProgramOption) {
return func(s ssh.Session) (tea.Model, []tea.ProgramOption) {
// This should never fail, as we are using the activeterm middleware.
pty, _, _ := s.Pty()
listItems := []list.Item{}
rows, err := db.Query(`
SELECT title, rank, username
FROM posts
ORDER BY rowid DESC
`)
if err != nil {
return nil, nil
}
defer rows.Close()
for rows.Next() {
var p Post
if err := rows.Scan(&p.Title, &p.Rank, &p.Username); err != nil {
return nil, nil
}
if p.Rank <= -10 {
continue
}
listItems = append(listItems, Post{
Title: p.Title,
Rank: p.Rank,
RankDelta: 0,
Username: p.Username,
})
}
if err := rows.Err(); err != nil {
return nil, nil
}
l := list.New(listItems, postDelegate{}, 20, 12)
l.Title = "echohacks: @" + s.User()
l.SetShowHelp(false)
inputStyle := lipgloss.NewStyle().
Border(lipgloss.RoundedBorder()).
BorderForeground(lipgloss.Color("#874BFD")).
Padding(1).
BorderTop(true).
BorderLeft(true).
BorderRight(true).
BorderBottom(true)
input := textinput.New()
input.Placeholder = "New Hackathon Idea"
input.Prompt = ""
input.Width = 40
h := help.New()
h.Styles = l.Help.Styles
m := model{
currentUsername: s.User(),
width: pty.Window.Width,
height: pty.Window.Height,
list: l,
input: input,
inputStyle: inputStyle,
help: h,
}
m.OnNew = func(p Post) {
_, err := db.Exec(`
INSERT INTO posts (title, rank, username)
VALUES (?, ?, ?)
`, p.Title, p.Rank, p.Username)
if err != nil {
log.Printf("Error inserting new post: %v", err)
}
}
m.OnUpdate = func(p Post) {
_, err := db.Exec(`
UPDATE posts
SET rank = ?
WHERE title = ? AND username = ?
`, p.Rank+p.RankDelta, p.Title, p.Username)
if err != nil {
log.Printf("Error updating post: %v", err)
}
}
return m, []tea.ProgramOption{tea.WithAltScreen()}
}
}