chore: renamed messages to events
This commit is contained in:
parent
c1e06a9dfc
commit
017c177b69
@ -11,9 +11,9 @@ import (
|
|||||||
"termtap.dev/internal/process"
|
"termtap.dev/internal/process"
|
||||||
)
|
)
|
||||||
|
|
||||||
func StartProcess(cmd model.Command, addr string, ch chan<- model.Message) (*model.Process, error) {
|
func StartProcess(cmd model.Command, addr string, ch chan<- model.Event) (*model.Process, error) {
|
||||||
ch <- model.Message{
|
ch <- model.Event{
|
||||||
Type: model.MessageTypeProcessStarting,
|
Type: model.EventTypeProcessStarting,
|
||||||
Body: fmt.Sprintf("spawning process '%s'", process.CommandString(cmd)),
|
Body: fmt.Sprintf("spawning process '%s'", process.CommandString(cmd)),
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -29,13 +29,13 @@ func StartProcess(cmd model.Command, addr string, ch chan<- model.Message) (*mod
|
|||||||
return proc, nil
|
return proc, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func StopProcess(proc *model.Process, ch chan<- model.Message, sig syscall.Signal) {
|
func StopProcess(proc *model.Process, ch chan<- model.Event, sig syscall.Signal) {
|
||||||
if proc == nil || proc.Exec == nil || proc.Exec.Process == nil {
|
if proc == nil || proc.Exec == nil || proc.Exec.Process == nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
ch <- model.Message{
|
ch <- model.Event{
|
||||||
Type: model.MessageTypeProcessSignaled,
|
Type: model.EventTypeProcessSignaled,
|
||||||
Body: fmt.Sprintf("process with pid '%d' is being killed", proc.Exec.Process.Pid),
|
Body: fmt.Sprintf("process with pid '%d' is being killed", proc.Exec.Process.Pid),
|
||||||
PID: proc.Exec.Process.Pid,
|
PID: proc.Exec.Process.Pid,
|
||||||
}
|
}
|
||||||
@ -50,15 +50,15 @@ func StopProcess(proc *model.Process, ch chan<- model.Message, sig syscall.Signa
|
|||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
|
|
||||||
func waitForProcessExit(proc *model.Process, ch chan<- model.Message) {
|
func waitForProcessExit(proc *model.Process, ch chan<- model.Event) {
|
||||||
if proc == nil || proc.Exec == nil {
|
if proc == nil || proc.Exec == nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := proc.Exec.Wait(); err != nil {
|
if err := proc.Exec.Wait(); err != nil {
|
||||||
if exitErr, ok := errors.AsType[*exec.ExitError](err); ok {
|
if exitErr, ok := errors.AsType[*exec.ExitError](err); ok {
|
||||||
ch <- model.Message{
|
ch <- model.Event{
|
||||||
Type: model.MessageTypeProcessExited,
|
Type: model.EventTypeProcessExited,
|
||||||
Body: fmt.Sprintf("process pid '%d' exited", proc.Exec.Process.Pid),
|
Body: fmt.Sprintf("process pid '%d' exited", proc.Exec.Process.Pid),
|
||||||
PID: proc.Exec.Process.Pid,
|
PID: proc.Exec.Process.Pid,
|
||||||
ExitCode: exitErr.ExitCode(),
|
ExitCode: exitErr.ExitCode(),
|
||||||
@ -67,16 +67,16 @@ func waitForProcessExit(proc *model.Process, ch chan<- model.Message) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
ch <- model.Message{
|
ch <- model.Event{
|
||||||
Type: model.MessageTypeFatal,
|
Type: model.EventTypeFatal,
|
||||||
Body: fmt.Sprintf("%q", err),
|
Body: fmt.Sprintf("%q", err),
|
||||||
}
|
}
|
||||||
process.UpdateStatus(proc, false, ch)
|
process.UpdateStatus(proc, false, ch)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
ch <- model.Message{
|
ch <- model.Event{
|
||||||
Type: model.MessageTypeProcessExited,
|
Type: model.EventTypeProcessExited,
|
||||||
Body: fmt.Sprintf("process pid '%d' exited", proc.Exec.Process.Pid),
|
Body: fmt.Sprintf("process pid '%d' exited", proc.Exec.Process.Pid),
|
||||||
PID: proc.Exec.Process.Pid,
|
PID: proc.Exec.Process.Pid,
|
||||||
ExitCode: 0,
|
ExitCode: 0,
|
||||||
|
|||||||
@ -8,13 +8,13 @@ import (
|
|||||||
"termtap.dev/internal/model"
|
"termtap.dev/internal/model"
|
||||||
)
|
)
|
||||||
|
|
||||||
func StartProxy(ps *model.ProxyServer, ch chan<- model.Message) {
|
func StartProxy(ps *model.ProxyServer, ch chan<- model.Event) {
|
||||||
if ps == nil || ps.Server == nil || ps.Listener == nil {
|
if ps == nil || ps.Server == nil || ps.Listener == nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
ch <- model.Message{
|
ch <- model.Event{
|
||||||
Type: model.MessageTypeProxyStarting,
|
Type: model.EventTypeProxyStarting,
|
||||||
Body: fmt.Sprintf("proxy server started on %s", (*ps.Listener).Addr().String()),
|
Body: fmt.Sprintf("proxy server started on %s", (*ps.Listener).Addr().String()),
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -23,8 +23,8 @@ func StartProxy(ps *model.ProxyServer, ch chan<- model.Message) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
ch <- model.Message{
|
ch <- model.Event{
|
||||||
Type: model.MessageTypeFatal,
|
Type: model.EventTypeFatal,
|
||||||
Body: fmt.Sprintf("fatal error in proxy server: %q", err),
|
Body: fmt.Sprintf("fatal error in proxy server: %q", err),
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
|
|||||||
@ -9,16 +9,16 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type Session struct {
|
type Session struct {
|
||||||
Messages <-chan model.Message
|
Events <-chan model.Event
|
||||||
|
|
||||||
msgCh chan model.Message
|
ch chan model.Event
|
||||||
proxy *model.ProxyServer
|
proxy *model.ProxyServer
|
||||||
proc *model.Process
|
proc *model.Process
|
||||||
stopOnce sync.Once
|
once sync.Once
|
||||||
}
|
}
|
||||||
|
|
||||||
func StartSession(cmd model.Command, addr string) (*Session, error) {
|
func StartSession(cmd model.Command, addr string) (*Session, error) {
|
||||||
msgs := make(chan model.Message, 256)
|
msgs := make(chan model.Event, 256)
|
||||||
|
|
||||||
ps, err := proxy.NewProxyServer(addr, msgs)
|
ps, err := proxy.NewProxyServer(addr, msgs)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -34,10 +34,10 @@ func StartSession(cmd model.Command, addr string) (*Session, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return &Session{
|
return &Session{
|
||||||
Messages: msgs,
|
Events: msgs,
|
||||||
msgCh: msgs,
|
ch: msgs,
|
||||||
proxy: ps,
|
proxy: ps,
|
||||||
proc: proc,
|
proc: proc,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -46,8 +46,8 @@ func (s *Session) Stop() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
s.stopOnce.Do(func() {
|
s.once.Do(func() {
|
||||||
StopProcess(s.proc, s.msgCh, syscall.SIGTERM)
|
StopProcess(s.proc, s.ch, syscall.SIGTERM)
|
||||||
proxy.Destroy(s.proxy, s.msgCh)
|
proxy.Destroy(s.proxy, s.ch)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@ -26,7 +26,7 @@ func Run(args []string) {
|
|||||||
}
|
}
|
||||||
defer session.Stop()
|
defer session.Stop()
|
||||||
|
|
||||||
if err := tui.Run(session.Messages); err != nil {
|
if err := tui.Run(session.Events); err != nil {
|
||||||
log.Fatalln(err)
|
log.Fatalln(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,32 +1,32 @@
|
|||||||
package model
|
package model
|
||||||
|
|
||||||
type MessageType string
|
type EventType string
|
||||||
|
|
||||||
const (
|
const (
|
||||||
MessageTypeSessionStarted MessageType = "SessionStarted"
|
EventTypeSessionStarted EventType = "SessionStarted"
|
||||||
MessageTypeSessionStopped MessageType = "SessionStopped"
|
EventTypeSessionStopped EventType = "SessionStopped"
|
||||||
|
|
||||||
MessageTypeProxyStarting MessageType = "ProxyStarting"
|
EventTypeProxyStarting EventType = "ProxyStarting"
|
||||||
MessageTypeProxyStarted MessageType = "ProxyStarted"
|
EventTypeProxyStarted EventType = "ProxyStarted"
|
||||||
MessageTypeProxyStopped MessageType = "ProxyStopped"
|
EventTypeProxyStopped EventType = "ProxyStopped"
|
||||||
|
|
||||||
MessageTypeRequestStarted MessageType = "RequestStarted"
|
EventTypeRequestStarted EventType = "RequestStarted"
|
||||||
MessageTypeRequestFinished MessageType = "RequestFinished"
|
EventTypeRequestFinished EventType = "RequestFinished"
|
||||||
MessageTypeRequestFailed MessageType = "RequestFailed"
|
EventTypeRequestFailed EventType = "RequestFailed"
|
||||||
|
|
||||||
MessageTypeProcessStarting MessageType = "ProcessStarting"
|
EventTypeProcessStarting EventType = "ProcessStarting"
|
||||||
MessageTypeProcessStarted MessageType = "ProcessStarted"
|
EventTypeProcessStarted EventType = "ProcessStarted"
|
||||||
MessageTypeProcessExited MessageType = "ProcessExited"
|
EventTypeProcessExited EventType = "ProcessExited"
|
||||||
MessageTypeProcessSignaled MessageType = "ProcessSignaled"
|
EventTypeProcessSignaled EventType = "ProcessSignaled"
|
||||||
MessageTypeProcessStdout MessageType = "ProcessStdout"
|
EventTypeProcessStdout EventType = "ProcessStdout"
|
||||||
MessageTypeProcessStderr MessageType = "ProcessStderr"
|
EventTypeProcessStderr EventType = "ProcessStderr"
|
||||||
|
|
||||||
MessageTypeFatal MessageType = "Fatal"
|
EventTypeFatal EventType = "Fatal"
|
||||||
MessageTypeWarn MessageType = "Warn"
|
EventTypeWarn EventType = "Warn"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Message struct {
|
type Event struct {
|
||||||
Type MessageType
|
Type EventType
|
||||||
Body string
|
Body string
|
||||||
PID int
|
PID int
|
||||||
ExitCode int
|
ExitCode int
|
||||||
|
|||||||
@ -15,7 +15,7 @@ func CommandString(c model.Command) string {
|
|||||||
return fmt.Sprintf("%s %s", c.Name, strings.Join(c.Args, " "))
|
return fmt.Sprintf("%s %s", c.Name, strings.Join(c.Args, " "))
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewProcess(cmd model.Command, addr string, ch chan<- model.Message) *model.Process {
|
func NewProcess(cmd model.Command, addr string, ch chan<- model.Event) *model.Process {
|
||||||
proc := exec.Command(cmd.Name, cmd.Args...)
|
proc := exec.Command(cmd.Name, cmd.Args...)
|
||||||
configureProcessForSignals(proc)
|
configureProcessForSignals(proc)
|
||||||
|
|
||||||
@ -23,24 +23,24 @@ func NewProcess(cmd model.Command, addr string, ch chan<- model.Message) *model.
|
|||||||
|
|
||||||
stdout, err := proc.StdoutPipe()
|
stdout, err := proc.StdoutPipe()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ch <- model.Message{
|
ch <- model.Event{
|
||||||
Type: model.MessageTypeWarn,
|
Type: model.EventTypeWarn,
|
||||||
Body: fmt.Sprintf("could not open stdout pipe: %q", err),
|
Body: fmt.Sprintf("could not open stdout pipe: %q", err),
|
||||||
PID: proc.Process.Pid,
|
PID: proc.Process.Pid,
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
go readPipe(stdout, model.MessageTypeProcessStdout, ch)
|
go readPipe(stdout, model.EventTypeProcessStdout, ch)
|
||||||
}
|
}
|
||||||
|
|
||||||
stderr, err := proc.StderrPipe()
|
stderr, err := proc.StderrPipe()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ch <- model.Message{
|
ch <- model.Event{
|
||||||
Type: model.MessageTypeWarn,
|
Type: model.EventTypeWarn,
|
||||||
Body: fmt.Sprintf("could not open stderr pipe: %q", err),
|
Body: fmt.Sprintf("could not open stderr pipe: %q", err),
|
||||||
PID: proc.Process.Pid,
|
PID: proc.Process.Pid,
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
go readPipe(stderr, model.MessageTypeProcessStderr, ch)
|
go readPipe(stderr, model.EventTypeProcessStderr, ch)
|
||||||
}
|
}
|
||||||
|
|
||||||
return &model.Process{
|
return &model.Process{
|
||||||
@ -66,17 +66,17 @@ func injectEnv(proc *exec.Cmd, addr string) {
|
|||||||
proc.Env = append(os.Environ(), injected...)
|
proc.Env = append(os.Environ(), injected...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func readPipe(pipe io.Reader, t model.MessageType, ch chan<- model.Message) {
|
func readPipe(pipe io.Reader, t model.EventType, ch chan<- model.Event) {
|
||||||
scanner := bufio.NewScanner(pipe)
|
scanner := bufio.NewScanner(pipe)
|
||||||
for scanner.Scan() {
|
for scanner.Scan() {
|
||||||
ch <- model.Message{
|
ch <- model.Event{
|
||||||
Type: t,
|
Type: t,
|
||||||
Body: scanner.Text(),
|
Body: scanner.Text(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func UpdateStatus(proc *model.Process, running bool, ch chan<- model.Message) {
|
func UpdateStatus(proc *model.Process, running bool, ch chan<- model.Event) {
|
||||||
if proc == nil {
|
if proc == nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -88,18 +88,18 @@ func UpdateStatus(proc *model.Process, running bool, ch chan<- model.Message) {
|
|||||||
proc.Running = running
|
proc.Running = running
|
||||||
|
|
||||||
var (
|
var (
|
||||||
t model.MessageType
|
t model.EventType
|
||||||
status string
|
status string
|
||||||
)
|
)
|
||||||
if running {
|
if running {
|
||||||
t = model.MessageTypeProcessStarted
|
t = model.EventTypeProcessStarted
|
||||||
status = "running"
|
status = "running"
|
||||||
} else {
|
} else {
|
||||||
t = model.MessageTypeProcessExited
|
t = model.EventTypeProcessExited
|
||||||
status = "stopped"
|
status = "stopped"
|
||||||
}
|
}
|
||||||
|
|
||||||
ch <- model.Message{
|
ch <- model.Event{
|
||||||
Type: t,
|
Type: t,
|
||||||
Body: fmt.Sprintf("Set process pid '%d' status to %s", proc.Exec.Process.Pid, status),
|
Body: fmt.Sprintf("Set process pid '%d' status to %s", proc.Exec.Process.Pid, status),
|
||||||
PID: proc.Exec.Process.Pid,
|
PID: proc.Exec.Process.Pid,
|
||||||
|
|||||||
@ -20,15 +20,15 @@ import (
|
|||||||
|
|
||||||
const maxPreviewBytes = 1024
|
const maxPreviewBytes = 1024
|
||||||
|
|
||||||
func proxyHandler(ch chan<- model.Message) http.Handler {
|
func proxyHandler(ch chan<- model.Event) http.Handler {
|
||||||
transport := http.DefaultTransport
|
transport := http.DefaultTransport
|
||||||
|
|
||||||
// TODO: This should be wired into the main channel, but that will require a model package
|
// TODO: This should be wired into the main channel, but that will require a model package
|
||||||
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
|
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
|
||||||
if req.Method == http.MethodConnect {
|
if req.Method == http.MethodConnect {
|
||||||
http.Error(w, "CONNECT is not supported yet", http.StatusNotImplemented)
|
http.Error(w, "CONNECT is not supported yet", http.StatusNotImplemented)
|
||||||
ch <- model.Message{
|
ch <- model.Event{
|
||||||
Type: model.MessageTypeWarn,
|
Type: model.EventTypeWarn,
|
||||||
Body: fmt.Sprintf("CONNECT is not supported: %s", req.Host),
|
Body: fmt.Sprintf("CONNECT is not supported: %s", req.Host),
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
@ -36,8 +36,8 @@ func proxyHandler(ch chan<- model.Message) http.Handler {
|
|||||||
|
|
||||||
if req.URL.Scheme == "" || req.URL.Host == "" {
|
if req.URL.Scheme == "" || req.URL.Host == "" {
|
||||||
http.Error(w, "request must use absolute-form URLs through the proxy", http.StatusBadRequest)
|
http.Error(w, "request must use absolute-form URLs through the proxy", http.StatusBadRequest)
|
||||||
ch <- model.Message{
|
ch <- model.Event{
|
||||||
Type: model.MessageTypeWarn,
|
Type: model.EventTypeWarn,
|
||||||
Body: fmt.Sprintf("rejected non-proxy request %s %s", req.Method, req.URL.String()),
|
Body: fmt.Sprintf("rejected non-proxy request %s %s", req.Method, req.URL.String()),
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
@ -60,8 +60,8 @@ func proxyHandler(ch chan<- model.Message) http.Handler {
|
|||||||
|
|
||||||
requestPreview, err := readAndRestoreBody(&req.Body)
|
requestPreview, err := readAndRestoreBody(&req.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ch <- model.Message{
|
ch <- model.Event{
|
||||||
Type: model.MessageTypeWarn,
|
Type: model.EventTypeWarn,
|
||||||
Body: fmt.Sprintf("(%s) failed to read request body", request.ID),
|
Body: fmt.Sprintf("(%s) failed to read request body", request.ID),
|
||||||
Request: request,
|
Request: request,
|
||||||
}
|
}
|
||||||
@ -80,8 +80,8 @@ func proxyHandler(ch chan<- model.Message) http.Handler {
|
|||||||
request.RequestHeaders = outReq.Header
|
request.RequestHeaders = outReq.Header
|
||||||
request.RawURL = outReq.URL.String()
|
request.RawURL = outReq.URL.String()
|
||||||
|
|
||||||
ch <- model.Message{
|
ch <- model.Event{
|
||||||
Type: model.MessageTypeRequestStarted,
|
Type: model.EventTypeRequestStarted,
|
||||||
Body: fmt.Sprintf("-> %+v", request),
|
Body: fmt.Sprintf("-> %+v", request),
|
||||||
Request: request,
|
Request: request,
|
||||||
}
|
}
|
||||||
@ -96,8 +96,8 @@ func proxyHandler(ch chan<- model.Message) http.Handler {
|
|||||||
request.Duration = time.Since(start).Round(time.Microsecond)
|
request.Duration = time.Since(start).Round(time.Microsecond)
|
||||||
request.Status = status
|
request.Status = status
|
||||||
|
|
||||||
ch <- model.Message{
|
ch <- model.Event{
|
||||||
Type: model.MessageTypeRequestFailed,
|
Type: model.EventTypeRequestFailed,
|
||||||
Body: fmt.Sprintf("upstream error for %s %s: %v", outReq.Method, outReq.URL.String(), err),
|
Body: fmt.Sprintf("upstream error for %s %s: %v", outReq.Method, outReq.URL.String(), err),
|
||||||
Request: request,
|
Request: request,
|
||||||
}
|
}
|
||||||
@ -107,8 +107,8 @@ func proxyHandler(ch chan<- model.Message) http.Handler {
|
|||||||
|
|
||||||
responsePreview, err := readAndRestoreBody(&resp.Body)
|
responsePreview, err := readAndRestoreBody(&resp.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ch <- model.Message{
|
ch <- model.Event{
|
||||||
Type: model.MessageTypeWarn,
|
Type: model.EventTypeWarn,
|
||||||
Body: fmt.Sprintf("(%s) failed to read response body", request.ID),
|
Body: fmt.Sprintf("(%s) failed to read response body", request.ID),
|
||||||
Request: request,
|
Request: request,
|
||||||
}
|
}
|
||||||
@ -124,8 +124,8 @@ func proxyHandler(ch chan<- model.Message) http.Handler {
|
|||||||
request.Duration = time.Since(start).Round(time.Microsecond)
|
request.Duration = time.Since(start).Round(time.Microsecond)
|
||||||
request.Status = resp.StatusCode
|
request.Status = resp.StatusCode
|
||||||
|
|
||||||
ch <- model.Message{
|
ch <- model.Event{
|
||||||
Type: model.MessageTypeRequestFailed,
|
Type: model.EventTypeRequestFailed,
|
||||||
Body: fmt.Sprintf("write response body %s %s: %v", outReq.Method, outReq.URL.String(), err),
|
Body: fmt.Sprintf("write response body %s %s: %v", outReq.Method, outReq.URL.String(), err),
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
@ -136,8 +136,8 @@ func proxyHandler(ch chan<- model.Message) http.Handler {
|
|||||||
request.ResponseHeaders = resp.Header
|
request.ResponseHeaders = resp.Header
|
||||||
request.Pending = false
|
request.Pending = false
|
||||||
|
|
||||||
ch <- model.Message{
|
ch <- model.Event{
|
||||||
Type: model.MessageTypeRequestFinished,
|
Type: model.EventTypeRequestFinished,
|
||||||
Body: fmt.Sprintf("<- %+v %s", request, formatHeaders(resp.Request.Header)),
|
Body: fmt.Sprintf("<- %+v %s", request, formatHeaders(resp.Request.Header)),
|
||||||
Request: request,
|
Request: request,
|
||||||
}
|
}
|
||||||
|
|||||||
@ -10,7 +10,7 @@ import (
|
|||||||
"termtap.dev/internal/model"
|
"termtap.dev/internal/model"
|
||||||
)
|
)
|
||||||
|
|
||||||
func NewProxyServer(addr string, ch chan<- model.Message) (*model.ProxyServer, error) {
|
func NewProxyServer(addr string, ch chan<- model.Event) (*model.ProxyServer, error) {
|
||||||
listener, err := net.Listen("tcp", addr)
|
listener, err := net.Listen("tcp", addr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -28,14 +28,14 @@ func NewProxyServer(addr string, ch chan<- model.Message) (*model.ProxyServer, e
|
|||||||
}
|
}
|
||||||
|
|
||||||
// BUG: Not sure what all this does
|
// BUG: Not sure what all this does
|
||||||
func Destroy(ps *model.ProxyServer, ch chan<- model.Message) {
|
func Destroy(ps *model.ProxyServer, ch chan<- model.Event) {
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
|
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
if ps != nil && ps.Server != nil {
|
if ps != nil && ps.Server != nil {
|
||||||
_ = ps.Server.Shutdown(ctx)
|
_ = ps.Server.Shutdown(ctx)
|
||||||
ch <- model.Message{
|
ch <- model.Event{
|
||||||
Type: model.MessageTypeProxyStopped,
|
Type: model.EventTypeProxyStarted,
|
||||||
Body: "proxy server was destroyed",
|
Body: "proxy server was destroyed",
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -13,18 +13,18 @@ const (
|
|||||||
maxRequests = 200
|
maxRequests = 200
|
||||||
)
|
)
|
||||||
|
|
||||||
type appMsg struct {
|
type EventMsg struct {
|
||||||
value model.Message
|
value model.Event
|
||||||
}
|
}
|
||||||
|
|
||||||
type modelErrMsg struct {
|
type ErrMsg struct {
|
||||||
err error
|
err error
|
||||||
}
|
}
|
||||||
|
|
||||||
type Model struct {
|
type Model struct {
|
||||||
msgCh <-chan model.Message
|
msgCh <-chan model.Event
|
||||||
|
|
||||||
events []model.Message
|
events []model.Event
|
||||||
requestOrder []uuid.UUID
|
requestOrder []uuid.UUID
|
||||||
requests map[uuid.UUID]model.Request
|
requests map[uuid.UUID]model.Request
|
||||||
|
|
||||||
@ -32,10 +32,10 @@ type Model struct {
|
|||||||
height int
|
height int
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewModel(msgCh <-chan model.Message) Model {
|
func NewModel(msgCh <-chan model.Event) Model {
|
||||||
return Model{
|
return Model{
|
||||||
msgCh: msgCh,
|
msgCh: msgCh,
|
||||||
events: make([]model.Message, 0, maxEvents),
|
events: make([]model.Event, 0, maxEvents),
|
||||||
requestOrder: make([]uuid.UUID, 0, maxRequests),
|
requestOrder: make([]uuid.UUID, 0, maxRequests),
|
||||||
requests: map[uuid.UUID]model.Request{},
|
requests: map[uuid.UUID]model.Request{},
|
||||||
width: 100,
|
width: 100,
|
||||||
@ -43,7 +43,7 @@ func NewModel(msgCh <-chan model.Message) Model {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func Run(msgCh <-chan model.Message) error {
|
func Run(msgCh <-chan model.Event) error {
|
||||||
p := tea.NewProgram(NewModel(msgCh), tea.WithAltScreen())
|
p := tea.NewProgram(NewModel(msgCh), tea.WithAltScreen())
|
||||||
_, err := p.Run()
|
_, err := p.Run()
|
||||||
return err
|
return err
|
||||||
@ -53,13 +53,13 @@ func (m Model) Init() tea.Cmd {
|
|||||||
return waitForAppMessage(m.msgCh)
|
return waitForAppMessage(m.msgCh)
|
||||||
}
|
}
|
||||||
|
|
||||||
func waitForAppMessage(msgCh <-chan model.Message) tea.Cmd {
|
func waitForAppMessage(msgCh <-chan model.Event) tea.Cmd {
|
||||||
return func() tea.Msg {
|
return func() tea.Msg {
|
||||||
msg, ok := <-msgCh
|
msg, ok := <-msgCh
|
||||||
if !ok {
|
if !ok {
|
||||||
return modelErrMsg{err: fmt.Errorf("event channel closed")}
|
return ErrMsg{err: fmt.Errorf("event channel closed")}
|
||||||
}
|
}
|
||||||
|
|
||||||
return appMsg{value: msg}
|
return EventMsg{value: msg}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -22,14 +22,14 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
|||||||
}
|
}
|
||||||
return m, nil
|
return m, nil
|
||||||
|
|
||||||
case modelErrMsg:
|
case ErrMsg:
|
||||||
m.events = append(m.events, model.Message{
|
m.events = append(m.events, model.Event{
|
||||||
Type: model.MessageTypeWarn,
|
Type: model.EventTypeWarn,
|
||||||
Body: fmt.Sprintf("tui event stream closed: %v", msg.err),
|
Body: fmt.Sprintf("tui event stream closed: %v", msg.err),
|
||||||
})
|
})
|
||||||
return m, nil
|
return m, nil
|
||||||
|
|
||||||
case appMsg:
|
case EventMsg:
|
||||||
m.pushEvent(msg.value)
|
m.pushEvent(msg.value)
|
||||||
m.applyMessage(msg.value)
|
m.applyMessage(msg.value)
|
||||||
return m, waitForAppMessage(m.msgCh)
|
return m, waitForAppMessage(m.msgCh)
|
||||||
@ -38,18 +38,18 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
|||||||
return m, nil
|
return m, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Model) pushEvent(msg model.Message) {
|
func (m *Model) pushEvent(msg model.Event) {
|
||||||
m.events = append(m.events, msg)
|
m.events = append(m.events, msg)
|
||||||
if len(m.events) > maxEvents {
|
if len(m.events) > maxEvents {
|
||||||
m.events = m.events[len(m.events)-maxEvents:]
|
m.events = m.events[len(m.events)-maxEvents:]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Model) applyMessage(msg model.Message) {
|
func (m *Model) applyMessage(msg model.Event) {
|
||||||
switch msg.Type {
|
switch msg.Type {
|
||||||
case model.MessageTypeRequestStarted:
|
case model.EventTypeRequestStarted:
|
||||||
m.upsertRequest(msg.Request, true)
|
m.upsertRequest(msg.Request, true)
|
||||||
case model.MessageTypeRequestFinished, model.MessageTypeRequestFailed:
|
case model.EventTypeRequestFinished, model.EventTypeRequestFailed:
|
||||||
m.upsertRequest(msg.Request, false)
|
m.upsertRequest(msg.Request, false)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user