handler

package
v0.0.6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 24, 2022 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Template = template.Must(template.New("template").Parse(`
<html>
	<head>
		<title>JSON Editor</title>
		<script src="https://cdn.jsdelivr.net/npm/@json-editor/json-editor@latest/dist/jsoneditor.min.js"></script>
	</head>
	<body>

		<div id="editor_holder">
		</div>
		<button id="btn-submit">Submit</button>

		<script>
			var element = document.getElementById('editor_holder');
			var editor = new JSONEditor(element, {
				schema: JSON.parse("{{.Schema}}")
			});
			{{if .JSON}}editor.setValue(JSON.parse("{{.JSON}}"));{{end}}

			function defaultSubmit(val) {
				fetch(".", {
					method: "POST",
					headers: {
						"Content-Type": "application/json; charset=utf-8",
					},
					body: val 
				}).catch(error => console.error(error));
			}

			var btnSubmit = document.getElementById('btn-submit');
			btnSubmit.addEventListener('click', () => {
				var val = JSON.stringify(editor.getValue());
				if (typeof submit === "undefined") {
					defaultsubmit(val);
				} else {
					submit(val);
				}
			});
		</script>
	</body>
</html>`))

Template is default template which creates an editor for a JSON Schema.

Functions

func New

func New(schema io.Reader, options ...Option) (http.Handler, error)

New creates a new http.Handler which provides editor of schema.

func PostToWriter

func PostToWriter(w io.Writer, h http.Handler) http.Handler

PostToWriter copies POST request body to the writer w with a new line.

Types

type Option

type Option func(o *Options) error

func WithJSON

func WithJSON(r io.Reader) Option

func WithTemplate added in v0.0.3

func WithTemplate(tmpl *template.Template) Option

type Options

type Options struct {
	JSON     string
	Template *template.Template
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL