LEFT | RIGHT |
1 // Copyright 2012 The Go Authors. All rights reserved. | 1 // Copyright 2012 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 // Common Playground functionality. | 5 // Common Playground functionality. |
6 | 6 |
7 package main | 7 package main |
8 | 8 |
9 import ( | 9 import ( |
10 "encoding/json" | 10 "encoding/json" |
11 "fmt" | 11 "fmt" |
12 » gofmt "go/fmt" | 12 » "go/format" |
13 "net/http" | 13 "net/http" |
14 ) | 14 ) |
15 | 15 |
16 // The server that will service compile and share requests. | 16 // The server that will service compile and share requests. |
17 const playgroundBaseURL = "http://play.golang.org" | 17 const playgroundBaseURL = "http://play.golang.org" |
18 | 18 |
19 func registerPlaygroundHandlers(mux *http.ServeMux) { | 19 func registerPlaygroundHandlers(mux *http.ServeMux) { |
20 if *showPlayground { | 20 if *showPlayground { |
21 mux.HandleFunc("/compile", bounceToPlayground) | 21 mux.HandleFunc("/compile", bounceToPlayground) |
22 mux.HandleFunc("/share", bounceToPlayground) | 22 mux.HandleFunc("/share", bounceToPlayground) |
23 } else { | 23 } else { |
24 mux.HandleFunc("/compile", disabledHandler) | 24 mux.HandleFunc("/compile", disabledHandler) |
25 mux.HandleFunc("/share", disabledHandler) | 25 mux.HandleFunc("/share", disabledHandler) |
26 } | 26 } |
27 http.HandleFunc("/fmt", fmtHandler) | 27 http.HandleFunc("/fmt", fmtHandler) |
28 } | 28 } |
29 | 29 |
30 type fmtResponse struct { | 30 type fmtResponse struct { |
31 Body string | 31 Body string |
32 Error string | 32 Error string |
33 } | 33 } |
34 | 34 |
35 // fmtHandler takes a Go program in its "body" form value, formats it with | 35 // fmtHandler takes a Go program in its "body" form value, formats it with |
36 // standard gofmt formatting, and writes a fmtResponse as a JSON object. | 36 // standard gofmt formatting, and writes a fmtResponse as a JSON object. |
37 func fmtHandler(w http.ResponseWriter, r *http.Request) { | 37 func fmtHandler(w http.ResponseWriter, r *http.Request) { |
38 resp := new(fmtResponse) | 38 resp := new(fmtResponse) |
39 » body, err := gofmt.String(r.FormValue("body")) | 39 » body, err := format.Source([]byte(r.FormValue("body"))) |
40 if err != nil { | 40 if err != nil { |
41 resp.Error = err.Error() | 41 resp.Error = err.Error() |
42 } else { | 42 } else { |
43 » » resp.Body = body | 43 » » resp.Body = string(body) |
44 } | 44 } |
45 json.NewEncoder(w).Encode(resp) | 45 json.NewEncoder(w).Encode(resp) |
46 } | 46 } |
47 | 47 |
48 // disabledHandler serves a 501 "Not Implemented" response. | 48 // disabledHandler serves a 501 "Not Implemented" response. |
49 func disabledHandler(w http.ResponseWriter, r *http.Request) { | 49 func disabledHandler(w http.ResponseWriter, r *http.Request) { |
50 w.WriteHeader(http.StatusNotImplemented) | 50 w.WriteHeader(http.StatusNotImplemented) |
51 fmt.Fprint(w, "This functionality is not available via local godoc.") | 51 fmt.Fprint(w, "This functionality is not available via local godoc.") |
52 } | 52 } |
LEFT | RIGHT |