Skip to content

Commit

Permalink
docs: add a simple wish app example (#46)
Browse files Browse the repository at this point in the history
this adds a simple wish app example, with no bubbletea or anything else,
so it can be easily copied and extended.

Signed-off-by: Carlos A Becker <caarlos0@users.noreply.github.com>
  • Loading branch information
caarlos0 committed May 16, 2022
1 parent 23d67b7 commit b0c88fc
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -1,4 +1,5 @@
examples/*
!examples/simple
!examples/bubbletea
examples/bubbletea/bubbletea
examples/bubbletea/.ssh
Expand Down
56 changes: 56 additions & 0 deletions examples/simple/main.go
@@ -0,0 +1,56 @@
package main

import (
"context"
"fmt"
"log"
"os"
"os/signal"
"syscall"
"time"

"github.com/charmbracelet/wish"
"github.com/charmbracelet/wish/logging"
"github.com/gliderlabs/ssh"
)

const (
host = "localhost"
port = 23234
)

func main() {
s, err := wish.NewServer(
wish.WithAddress(fmt.Sprintf("%s:%d", host, port)),
wish.WithHostKeyPath(".ssh/term_info_ed25519"),
wish.WithMiddleware(
func(h ssh.Handler) ssh.Handler {
return func(s ssh.Session) {
fmt.Fprintln(s, "Hello, world!")
h(s)
}
},
logging.Middleware(),
),
)
if err != nil {
log.Fatalln(err)
}

done := make(chan os.Signal, 1)
signal.Notify(done, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
log.Printf("Starting SSH server on %s:%d", host, port)
go func() {
if err = s.ListenAndServe(); err != nil {
log.Fatalln(err)
}
}()

<-done
log.Println("Stopping SSH server")
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer func() { cancel() }()
if err := s.Shutdown(ctx); err != nil {
log.Fatalln(err)
}
}

0 comments on commit b0c88fc

Please sign in to comment.