summaryrefslogtreecommitdiff
path: root/src/cmd/godoc/play.go
blob: 47a11f6c0bf8ef0a86eb3b57a544296c235bfb8e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// Copyright 2012 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// Common Playground functionality.

package main

import (
	"encoding/json"
	"fmt"
	"go/format"
	"net/http"
)

// The server that will service compile and share requests.
const playgroundBaseURL = "http://play.golang.org"

func registerPlaygroundHandlers(mux *http.ServeMux) {
	if *showPlayground {
		mux.HandleFunc("/compile", bounceToPlayground)
		mux.HandleFunc("/share", bounceToPlayground)
	} else {
		mux.HandleFunc("/compile", disabledHandler)
		mux.HandleFunc("/share", disabledHandler)
	}
	http.HandleFunc("/fmt", fmtHandler)
}

type fmtResponse struct {
	Body  string
	Error string
}

// fmtHandler takes a Go program in its "body" form value, formats it with
// standard gofmt formatting, and writes a fmtResponse as a JSON object.
func fmtHandler(w http.ResponseWriter, r *http.Request) {
	resp := new(fmtResponse)
	body, err := format.Source([]byte(r.FormValue("body")))
	if err != nil {
		resp.Error = err.Error()
	} else {
		resp.Body = string(body)
	}
	json.NewEncoder(w).Encode(resp)
}

// disabledHandler serves a 501 "Not Implemented" response.
func disabledHandler(w http.ResponseWriter, r *http.Request) {
	w.WriteHeader(http.StatusNotImplemented)
	fmt.Fprint(w, "This functionality is not available via local godoc.")
}