blob: 8aa3f6f6b369c1183a10bc44d63b027d1e5417d9 [file] [log] [blame]
// 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.
package main
import (
"bytes"
"encoding/json"
"go/ast"
"go/parser"
"go/printer"
"go/token"
"net/http"
)
func init() {
http.HandleFunc("/fmt", fmtHandler)
}
type fmtResponse struct {
Body string
Error string
}
func fmtHandler(w http.ResponseWriter, r *http.Request) {
resp := new(fmtResponse)
body, err := gofmt(r.FormValue("body"))
if err != nil {
resp.Error = err.Error()
} else {
resp.Body = 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)
if err != nil {
return "", err
}
ast.SortImports(fset, f)
var buf bytes.Buffer
config := &printer.Config{Mode: printer.UseSpaces | printer.TabIndent, Tabwidth: 8}
err = config.Fprint(&buf, fset, f)
if err != nil {
return "", err
}
return buf.String(), nil
}