play

package
v0.58.0 Latest Latest
Warning

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

Go to latest
Published: Oct 6, 2024 License: BSD-3-Clause Imports: 19 Imported by: 0

Documentation

Overview

Package play provides callable APIs and HTTP handlers to format and run Go code, similar to Go playground but using HTTP instead of WebSocket.

For HTTP API, this package expose two handlers: HTTPHandleFormat and HTTPHandleRun. Both HTTP APIs accept JSON content type, with the following request format,

{
	"body":<string>
}

where "body" field contains the Go code to be formatted or run. Both have the following response format,

{
	"code": <integer, HTTP status code>,
	"name": <string, error type>,
	"message": <string, optional message>,
	"data": <string>
}

For the HTTPHandleFormat, the response "data" contains the formatted Go code. For the HTTPHandleRun, the response "data" contains the output from running the Go code, the "message" contains an error pre-Run, like bad request or file system related error.

Index

Examples

Constants

This section is empty.

Variables

View Source
var GoVersion = `1.23.2`

GoVersion define the Go tool version for go.mod to be used to run the code.

Functions

func Format

func Format(req Request) (out []byte, err error)

Format the Go code in the [Request.Body] and return the result to out. Any syntax error on the code will be returned as error.

Example
const codeIndentMissingImport = `
package main
func main() {
  fmt.Println("Hello, world")
}
`
var req = Request{
	Body: codeIndentMissingImport,
}
var (
	out []byte
	err error
)
out, err = Format(req)
if err != nil {
	log.Fatal(err)
}
fmt.Printf("%s", out)
Output:

package main

import "fmt"

func main() {
	fmt.Println("Hello, world")
}

func HTTPHandleFormat

func HTTPHandleFormat(httpresw http.ResponseWriter, httpreq *http.Request)

HTTPHandleFormat define the HTTP handler for formating Go code.

Example
var mux = http.NewServeMux()
mux.HandleFunc(`POST /api/play/format`, HTTPHandleFormat)

const codeIndentMissingImport = `
package main
func main() {
  fmt.Println("Hello, world")
}
`
var req = Request{
	Body: codeIndentMissingImport,
}
var (
	rawbody []byte
	err     error
)
rawbody, err = json.Marshal(&req)
if err != nil {
	log.Fatal(err)
}

var resprec = httptest.NewRecorder()
var httpreq = httptest.NewRequest(`POST`, `/api/play/format`, bytes.NewReader(rawbody))
httpreq.Header.Set(`Content-Type`, `application/json`)

mux.ServeHTTP(resprec, httpreq)
var resp = resprec.Result()

rawbody, err = io.ReadAll(resp.Body)
if err != nil {
	log.Fatal(err)
}

fmt.Printf(`%s`, rawbody)
Output:

{"data":"package main\n\nimport \"fmt\"\n\nfunc main() {\n\tfmt.Println(\"Hello, world\")\n}\n","code":200}

func HTTPHandleRun

func HTTPHandleRun(httpresw http.ResponseWriter, httpreq *http.Request)

HTTPHandleRun define the HTTP handler for running Go code. Each client is identified by unique cookie, so if two Run requests come from the same client, the previous Run will be cancelled.

Example
var mux = http.NewServeMux()
mux.HandleFunc(`POST /api/play/run`, HTTPHandleRun)

const codeRun = `
package main
import "fmt"
func main() {
	fmt.Println("Hello, world")
}
`
var req = Request{
	Body: codeRun,
}
var (
	rawbody []byte
	err     error
)
rawbody, err = json.Marshal(&req)
if err != nil {
	log.Fatal(err)
}

var resprec = httptest.NewRecorder()
var httpreq = httptest.NewRequest(`POST`, `/api/play/run`, bytes.NewReader(rawbody))
httpreq.Header.Set(`Content-Type`, `application/json`)

mux.ServeHTTP(resprec, httpreq)
var resp = resprec.Result()

rawbody, err = io.ReadAll(resp.Body)
if err != nil {
	log.Fatal(err)
}

fmt.Printf(`%s`, rawbody)
Output:

{"data":"Hello, world\n","code":200}

func Run

func Run(req *Request) (out []byte, err error)

Run the Go code in the [Request.Body].

Example
const codeRun = `
package main
import "fmt"
func main() {
	fmt.Println("Hello, world")
}`

var req = Request{
	Body: codeRun,
}
var (
	out []byte
	err error
)
out, err = Run(&req)
if err != nil {
	fmt.Printf(`error: %s`, err)
}
fmt.Printf(`%s`, out)
Output:

Hello, world

Types

type Request

type Request struct {

	// Body contains the Go code to be Format-ed or Run.
	Body string `json:"body"`
	// contains filtered or unexported fields
}

Request for calling Format and Run.

Jump to

Keyboard shortcuts

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