waveterm/pkg/waveapp/waveappserverimpl.go

150 lines
3.8 KiB
Go
Raw Normal View History

2024-11-02 18:58:13 +01:00
// Copyright 2024, Command Line Inc.
// SPDX-License-Identifier: Apache-2.0
2024-11-14 22:32:58 +01:00
package waveapp
2024-11-02 18:58:13 +01:00
import (
"bytes"
"context"
"fmt"
"log"
"net/http"
2024-11-21 03:05:13 +01:00
"github.com/wavetermdev/waveterm/pkg/panichandler"
2024-11-02 18:58:13 +01:00
"github.com/wavetermdev/waveterm/pkg/vdom"
"github.com/wavetermdev/waveterm/pkg/wshrpc"
)
2024-11-14 22:32:58 +01:00
type WaveAppServerImpl struct {
2024-11-02 18:58:13 +01:00
Client *Client
BlockId string
}
2024-11-14 22:32:58 +01:00
func (*WaveAppServerImpl) WshServerImpl() {}
2024-11-02 18:58:13 +01:00
2024-11-14 22:32:58 +01:00
func (impl *WaveAppServerImpl) VDomRenderCommand(ctx context.Context, feUpdate vdom.VDomFrontendUpdate) chan wshrpc.RespOrErrorUnion[*vdom.VDomBackendUpdate] {
respChan := make(chan wshrpc.RespOrErrorUnion[*vdom.VDomBackendUpdate], 5)
defer func() {
2024-11-21 03:05:13 +01:00
panicErr := panichandler.PanicHandler("VDomRenderCommand")
if panicErr != nil {
respChan <- wshrpc.RespOrErrorUnion[*vdom.VDomBackendUpdate]{
2024-11-21 03:05:13 +01:00
Error: panicErr,
}
close(respChan)
}
}()
2024-11-02 18:58:13 +01:00
if feUpdate.Dispose {
defer close(respChan)
2024-11-02 18:58:13 +01:00
log.Printf("got dispose from frontend\n")
impl.Client.doShutdown("got dispose from frontend")
return respChan
2024-11-02 18:58:13 +01:00
}
2024-11-02 18:58:13 +01:00
if impl.Client.GetIsDone() {
close(respChan)
return respChan
2024-11-02 18:58:13 +01:00
}
impl.Client.Root.RenderTs = feUpdate.Ts
2024-11-02 18:58:13 +01:00
// set atoms
for _, ss := range feUpdate.StateSync {
impl.Client.Root.SetAtomVal(ss.Atom, ss.Value, false)
}
// run events
for _, event := range feUpdate.Events {
if event.GlobalEventType != "" {
2024-11-02 18:58:13 +01:00
if impl.Client.GlobalEventHandler != nil {
impl.Client.GlobalEventHandler(impl.Client, event)
}
} else {
impl.Client.Root.Event(event.WaveId, event.EventType, event)
2024-11-02 18:58:13 +01:00
}
}
// update refs
for _, ref := range feUpdate.RefUpdates {
impl.Client.Root.UpdateRef(ref)
}
var update *vdom.VDomBackendUpdate
var err error
if feUpdate.Resync || true {
update, err = impl.Client.fullRender()
} else {
update, err = impl.Client.incrementalRender()
}
update.CreateTransferElems()
if err != nil {
respChan <- wshrpc.RespOrErrorUnion[*vdom.VDomBackendUpdate]{
Error: err,
}
close(respChan)
return respChan
2024-11-02 18:58:13 +01:00
}
// Split the update into chunks and send them sequentially
updates := vdom.SplitBackendUpdate(update)
go func() {
2024-11-21 03:05:13 +01:00
defer panichandler.PanicHandler("VDomRenderCommand:splitUpdates")
defer close(respChan)
for _, splitUpdate := range updates {
respChan <- wshrpc.RespOrErrorUnion[*vdom.VDomBackendUpdate]{
Response: splitUpdate,
}
}
}()
return respChan
2024-11-02 18:58:13 +01:00
}
2024-11-14 22:32:58 +01:00
func (impl *WaveAppServerImpl) VDomUrlRequestCommand(ctx context.Context, data wshrpc.VDomUrlRequestData) chan wshrpc.RespOrErrorUnion[wshrpc.VDomUrlRequestResponse] {
2024-11-02 18:58:13 +01:00
respChan := make(chan wshrpc.RespOrErrorUnion[wshrpc.VDomUrlRequestResponse])
writer := NewStreamingResponseWriter(respChan)
go func() {
defer close(respChan) // Declared first, so it executes last
defer writer.Close() // Ensures writer is closed before the channel is closed
2024-11-02 18:58:13 +01:00
defer func() {
2024-11-21 03:05:13 +01:00
panicErr := panichandler.PanicHandler("VDomUrlRequestCommand")
if panicErr != nil {
2024-11-02 18:58:13 +01:00
writer.WriteHeader(http.StatusInternalServerError)
2024-11-21 03:05:13 +01:00
writer.Write([]byte(fmt.Sprintf("internal server error: %v", panicErr)))
2024-11-02 18:58:13 +01:00
}
}()
// Create an HTTP request from the RPC request data
var bodyReader *bytes.Reader
if data.Body != nil {
bodyReader = bytes.NewReader(data.Body)
} else {
bodyReader = bytes.NewReader([]byte{})
}
httpReq, err := http.NewRequest(data.Method, data.URL, bodyReader)
if err != nil {
writer.WriteHeader(http.StatusInternalServerError)
writer.Write([]byte(err.Error()))
return
}
for key, value := range data.Headers {
httpReq.Header.Set(key, value)
}
if httpReq.URL.Path == "/wave/global.css" && impl.Client.GlobalStylesOption != nil {
ServeFileOption(writer, httpReq, *impl.Client.GlobalStylesOption)
return
}
2024-11-02 18:58:13 +01:00
if impl.Client.OverrideUrlHandler != nil {
impl.Client.OverrideUrlHandler.ServeHTTP(writer, httpReq)
return
}
impl.Client.UrlHandlerMux.ServeHTTP(writer, httpReq)
}()
return respChan
}