|
1 | 1 | package langserver
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "context" |
4 | 5 | "encoding/json"
|
5 |
| - "fmt" |
| 6 | + "io/ioutil" |
6 | 7 | "net/http"
|
| 8 | + "strconv" |
7 | 9 | "strings"
|
8 | 10 |
|
| 11 | + "github.com/x1unix/go-playground/pkg/analyzer" |
9 | 12 | "github.com/x1unix/go-playground/pkg/goplay"
|
10 |
| - |
11 | 13 | "go.uber.org/zap"
|
12 |
| - |
13 |
| - "github.com/x1unix/go-playground/pkg/analyzer" |
14 | 14 | )
|
15 | 15 |
|
16 | 16 | type SnippetResponse struct {
|
@@ -39,19 +39,11 @@ type ErrorResponse struct {
|
39 | 39 | Error string `json:"error"`
|
40 | 40 | }
|
41 | 41 |
|
42 |
| -func NewErrorResponse(err error) ErrorResponse { |
43 |
| - return ErrorResponse{Error: err.Error(), code: http.StatusInternalServerError} |
44 |
| -} |
45 |
| - |
46 |
| -// Errorf creates error response |
47 |
| -func Errorf(code int, format string, args ...interface{}) ErrorResponse { |
48 |
| - return ErrorResponse{ |
49 |
| - code: code, |
50 |
| - Error: fmt.Sprintf(format, args...), |
51 |
| - } |
| 42 | +func NewErrorResponse(err error) *ErrorResponse { |
| 43 | + return &ErrorResponse{Error: err.Error(), code: http.StatusInternalServerError} |
52 | 44 | }
|
53 | 45 |
|
54 |
| -func (r ErrorResponse) Write(w http.ResponseWriter) http.ResponseWriter { |
| 46 | +func (r *ErrorResponse) Write(w http.ResponseWriter) http.ResponseWriter { |
55 | 47 | w.Header().Add("Content-Type", "application/json")
|
56 | 48 | w.WriteHeader(r.code)
|
57 | 49 | if err := json.NewEncoder(w).Encode(r); err != nil {
|
@@ -90,3 +82,52 @@ func WriteJSON(w http.ResponseWriter, i interface{}) {
|
90 | 82 | w.Write(data)
|
91 | 83 | return
|
92 | 84 | }
|
| 85 | + |
| 86 | +// goImportsCode reads code from request and performs "goimports" on it |
| 87 | +// if any error occurs, it sends error response to client and closes connection |
| 88 | +// |
| 89 | +// if "format" url query param is undefined or set to "false", just returns code as is |
| 90 | +func goImportsCode(ctx context.Context, src []byte) ([]byte, bool, error) { |
| 91 | + resp, err := goplay.GoImports(ctx, src) |
| 92 | + if err != nil { |
| 93 | + if err == goplay.ErrSnippetTooLarge { |
| 94 | + return nil, false, NewHTTPError(http.StatusRequestEntityTooLarge, err) |
| 95 | + } |
| 96 | + |
| 97 | + return nil, false, err |
| 98 | + } |
| 99 | + |
| 100 | + if err = resp.HasError(); err != nil { |
| 101 | + return nil, false, err |
| 102 | + } |
| 103 | + |
| 104 | + changed := resp.Body != string(src) |
| 105 | + return []byte(resp.Body), changed, nil |
| 106 | +} |
| 107 | + |
| 108 | +func shouldFormatCode(r *http.Request) (bool, error) { |
| 109 | + val := r.URL.Query().Get(formatQueryParam) |
| 110 | + if val == "" { |
| 111 | + return false, nil |
| 112 | + } |
| 113 | + |
| 114 | + boolVal, err := strconv.ParseBool(val) |
| 115 | + if err != nil { |
| 116 | + return false, Errorf( |
| 117 | + http.StatusBadRequest, |
| 118 | + "invalid %q query parameter value (expected boolean)", formatQueryParam, |
| 119 | + ) |
| 120 | + } |
| 121 | + |
| 122 | + return boolVal, nil |
| 123 | +} |
| 124 | + |
| 125 | +func getPayloadFromRequest(r *http.Request) ([]byte, error) { |
| 126 | + src, err := ioutil.ReadAll(r.Body) |
| 127 | + if err != nil { |
| 128 | + return nil, Errorf(http.StatusBadGateway, "failed to read request: %s", err) |
| 129 | + } |
| 130 | + |
| 131 | + r.Body.Close() |
| 132 | + return src, nil |
| 133 | +} |
0 commit comments