Skip to content

Commit

Permalink
fix: examples et al
Browse files Browse the repository at this point in the history
  • Loading branch information
caarlos0 committed Mar 1, 2024
1 parent 2c341cc commit 7b7605e
Show file tree
Hide file tree
Showing 5 changed files with 182 additions and 55 deletions.
118 changes: 118 additions & 0 deletions _examples/custom-registry/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
// Package main provides an example of how to use the promwish package.
//
// You can test with:
//
// go run main.go
// ssh -o UserKnownHostsFile=/dev/null -p 2222 localhost
// curl -s localhost:9222/metrics | grep wish_
package main

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

tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/log"
"github.com/charmbracelet/promwish"
"github.com/charmbracelet/ssh"
"github.com/charmbracelet/wish"
bm "github.com/charmbracelet/wish/bubbletea"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/client_golang/prometheus/promhttp"
)

var keyTypeCounter = promauto.NewCounterVec(prometheus.CounterOpts{
Name: "wish_auth_by_type_total",
Help: "Custom metric example",
}, []string{"type"})

func main() {
registry := prometheus.NewRegistry()

s, err := wish.NewServer(
wish.WithAddress("localhost:2223"),
wish.WithPublicKeyAuth(func(ssh.Context, ssh.PublicKey) bool { return true }),
wish.WithMiddleware(
bm.Middleware(func(s ssh.Session) (tea.Model, []tea.ProgramOption) {
keyTypeCounter.WithLabelValues(s.PublicKey().Type()).Inc()
pty, _, active := s.Pty()
if !active {
fmt.Println("no active terminal, skipping")
return nil, nil
}
m := model{
term: pty.Term,
user: s.User(),
}
return m, []tea.ProgramOption{}
}),
promwish.MiddlewareRegistry(
registry,
prometheus.Labels{
"app": "test",
},
promwish.DefaultCommandFn,
),
),
)
if err != nil {
log.Fatal("Fail to start SSH server", "error", err)
}

log.Info("Starting server", "metrics", "http://localhost:9222/metrics", "ssh", "localhost:2223")
done := make(chan os.Signal, 1)
metrics := promwish.NewServer(
"localhost:9222",
promhttp.InstrumentMetricHandler(
registry, promhttp.HandlerFor(registry, promhttp.HandlerOpts{}),
),
)
signal.Notify(done, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
go func() {
if err = s.ListenAndServe(); err != nil {
log.Fatal("Fail to start HTTP server", "error", err)
}
}()
go func() {
if err = metrics.ListenAndServe(); err != nil {
log.Fatal("Fail to start metrics server", "error", err)
}
}()
<-done
if err := s.Close(); err != nil {
log.Fatal("Fail to close SSH server", "error", err)
}

ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer func() { cancel() }()
if err := metrics.Shutdown(ctx); err != nil {
log.Fatal("Fail to shut down metrics server", "error", err)
}
}

// Just a generic tea.Model to demo terminal information of ssh.
type model struct {
user, term string
quitting bool
}

func (m model) Init() tea.Cmd {
return nil
}

func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
if !m.quitting {
m.quitting = true
return m, func() tea.Msg { return nil }
}
return m, tea.Quit
}

func (m model) View() string {
return fmt.Sprintf("\n\nHello, %s. Your terminal is %s!\n\n\n", m.user, m.term)
}
24 changes: 13 additions & 11 deletions _examples/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ go 1.17
replace github.com/charmbracelet/promwish => ../

require (
github.com/charmbracelet/bubbletea v0.24.2
github.com/charmbracelet/bubbletea v0.25.0
github.com/charmbracelet/log v0.3.1
github.com/charmbracelet/promwish v0.5.0
github.com/charmbracelet/ssh v0.0.0-20221117183211-483d43d97103
github.com/charmbracelet/wish v1.2.0
github.com/prometheus/client_golang v1.18.0
github.com/charmbracelet/ssh v0.0.0-20240202115812-f4ab1009799a
github.com/charmbracelet/wish v1.3.1
github.com/prometheus/client_golang v1.19.0
)

require (
Expand All @@ -20,26 +20,28 @@ require (
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/charmbracelet/keygen v0.5.0 // indirect
github.com/charmbracelet/lipgloss v0.9.1 // indirect
github.com/charmbracelet/x/errors v0.0.0-20240117030013-d31dba354651 // indirect
github.com/charmbracelet/x/exp/term v0.0.0-20240202113029-6ff29cf0473e // indirect
github.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81 // indirect
github.com/creack/pty v1.1.21 // indirect
github.com/go-logfmt/logfmt v0.6.0 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/mattn/go-isatty v0.0.18 // indirect
github.com/mattn/go-localereader v0.0.1 // indirect
github.com/mattn/go-runewidth v0.0.15 // indirect
github.com/matttproud/golang_protobuf_extensions/v2 v2.0.0 // indirect
github.com/muesli/ansi v0.0.0-20211018074035-2e021307bc4b // indirect
github.com/muesli/cancelreader v0.2.2 // indirect
github.com/muesli/reflow v0.3.0 // indirect
github.com/muesli/termenv v0.15.2 // indirect
github.com/prometheus/client_model v0.5.0 // indirect
github.com/prometheus/common v0.45.0 // indirect
github.com/prometheus/common v0.48.0 // indirect
github.com/prometheus/procfs v0.12.0 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
golang.org/x/crypto v0.17.0 // indirect
golang.org/x/crypto v0.18.0 // indirect
golang.org/x/exp v0.0.0-20231006140011-7918f672742d // indirect
golang.org/x/sync v0.4.0 // indirect
golang.org/x/sys v0.15.0 // indirect
golang.org/x/term v0.15.0 // indirect
golang.org/x/sync v0.6.0 // indirect
golang.org/x/sys v0.16.0 // indirect
golang.org/x/term v0.16.0 // indirect
golang.org/x/text v0.14.0 // indirect
google.golang.org/protobuf v1.31.0 // indirect
google.golang.org/protobuf v1.32.0 // indirect
)
Loading

0 comments on commit 7b7605e

Please sign in to comment.