| // 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. |
| http.HandleFunc("/fmt", fmtHandler) |
| type fmtResponse struct { |
| func fmtHandler(w http.ResponseWriter, r *http.Request) { |
| body, err := gofmt(r.FormValue("body")) |
| json.NewEncoder(w).Encode(resp) |
| func gofmt(body string) (string, error) { |
| fset := token.NewFileSet() |
| f, err := parser.ParseFile(fset, "prog.go", body, parser.ParseComments) |
| err = printer.Fprint(&buf, fset, f) |