Split embedded and standalone chart surfaces
This commit is contained in:
@@ -1,7 +1,10 @@
|
||||
package viewer
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"mime"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
@@ -9,10 +12,21 @@ import (
|
||||
)
|
||||
|
||||
type HandlerOptions struct {
|
||||
Title string
|
||||
Title string
|
||||
Standalone bool
|
||||
}
|
||||
|
||||
func NewHandler(opts HandlerOptions) http.Handler {
|
||||
opts.Standalone = false
|
||||
return newHandler(opts)
|
||||
}
|
||||
|
||||
func NewStandaloneHandler(opts HandlerOptions) http.Handler {
|
||||
opts.Standalone = true
|
||||
return newHandler(opts)
|
||||
}
|
||||
|
||||
func newHandler(opts HandlerOptions) http.Handler {
|
||||
title := strings.TrimSpace(opts.Title)
|
||||
if title == "" {
|
||||
title = "Reanimator Chart"
|
||||
@@ -25,7 +39,15 @@ func NewHandler(opts HandlerOptions) http.Handler {
|
||||
_, _ = w.Write([]byte("ok"))
|
||||
})
|
||||
mux.HandleFunc("GET /", func(w http.ResponseWriter, r *http.Request) {
|
||||
html, err := RenderHTML(nil, title)
|
||||
var (
|
||||
html []byte
|
||||
err error
|
||||
)
|
||||
if opts.Standalone {
|
||||
html, err = web.RenderUpload(pageData{Title: title})
|
||||
} else {
|
||||
html, err = RenderHTML(nil, title)
|
||||
}
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
@@ -34,31 +56,28 @@ func NewHandler(opts HandlerOptions) http.Handler {
|
||||
_, _ = w.Write(html)
|
||||
})
|
||||
mux.HandleFunc("POST /render", func(w http.ResponseWriter, r *http.Request) {
|
||||
var payload string
|
||||
if strings.Contains(r.Header.Get("Content-Type"), "application/json") {
|
||||
body, err := io.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
payload = string(body)
|
||||
} else {
|
||||
if err := r.ParseForm(); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
payload = r.FormValue("snapshot")
|
||||
payload, err := readSnapshotPayload(r)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
page, err := buildPageData([]byte(payload), title)
|
||||
if err != nil {
|
||||
page = pageData{
|
||||
Title: title,
|
||||
Error: err.Error(),
|
||||
InputJSON: prettyJSON(payload),
|
||||
if opts.Standalone {
|
||||
html, renderErr := web.RenderUpload(pageData{
|
||||
Title: title,
|
||||
Error: err.Error(),
|
||||
})
|
||||
if renderErr != nil {
|
||||
http.Error(w, renderErr.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||
_, _ = w.Write(html)
|
||||
return
|
||||
}
|
||||
} else {
|
||||
page.InputJSON = prettyJSON(payload)
|
||||
page = pageData{Title: title, Error: err.Error()}
|
||||
}
|
||||
|
||||
html, renderErr := web.Render(page)
|
||||
@@ -71,3 +90,51 @@ func NewHandler(opts HandlerOptions) http.Handler {
|
||||
})
|
||||
return mux
|
||||
}
|
||||
|
||||
func readSnapshotPayload(r *http.Request) (string, error) {
|
||||
mediaType, _, err := mime.ParseMediaType(r.Header.Get("Content-Type"))
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("parse content type: %w", err)
|
||||
}
|
||||
|
||||
switch mediaType {
|
||||
case "application/json":
|
||||
body, err := io.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("read request body: %w", err)
|
||||
}
|
||||
return string(body), nil
|
||||
case "multipart/form-data":
|
||||
if err := r.ParseMultipartForm(32 << 20); err != nil {
|
||||
return "", fmt.Errorf("parse multipart form: %w", err)
|
||||
}
|
||||
|
||||
payload, err := readSnapshotFile(r, "snapshot_file")
|
||||
if err == nil && strings.TrimSpace(payload) != "" {
|
||||
return payload, nil
|
||||
}
|
||||
if err != nil && !errors.Is(err, http.ErrMissingFile) {
|
||||
return "", err
|
||||
}
|
||||
return r.FormValue("snapshot"), nil
|
||||
default:
|
||||
if err := r.ParseForm(); err != nil {
|
||||
return "", fmt.Errorf("parse form: %w", err)
|
||||
}
|
||||
return r.FormValue("snapshot"), nil
|
||||
}
|
||||
}
|
||||
|
||||
func readSnapshotFile(r *http.Request, field string) (string, error) {
|
||||
file, _, err := r.FormFile(field)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("read uploaded file: %w", err)
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
body, err := io.ReadAll(file)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("read uploaded file contents: %w", err)
|
||||
}
|
||||
return string(body), nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user