| // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 |
| 15 | package http |
| 16 |
| 17 | import ( |
| 18 | "encoding/json" |
| 19 | "net/http" |
| 20 |
| 21 | "github.com/fatedier/frp/pkg/util/log" |
| 22 | ) |
| 23 |
| 24 | type GeneralResponse struct { |
| 25 | Code int |
| 26 | Msg string |
| 27 | } |
| 28 |
| 29 | type V2Response struct { |
| 30 | Code int `json:"code"` |
| 31 | Msg string `json:"msg"` |
| 32 | Data any `json:"data"` |
| 33 | } |
| 34 |
| 35 | // APIHandler is a handler function that returns a response object or an error. |
| 36 | type APIHandler func(ctx *Context) (any, error) |
| 37 |
| 38 | // MakeHTTPHandlerFunc turns a normal APIHandler into a http.HandlerFunc. |
| 39 | func MakeHTTPHandlerFunc(handler APIHandler) http.HandlerFunc { |
| 40 | return func(w http.ResponseWriter, r *http.Request) { |
| 41 | ctx := NewContext(w, r) |
| 42 | res, err := handler(ctx) |
| 43 | if err != nil { |
| 44 | log.Warnf("http response [%s]: error: %v", r.URL.Path, err) |
| 45 | code := http.StatusInternalServerError |
| 46 | if e, ok := err.(*Error); ok { |
| 47 | code = e.Code |
| 48 | } |
| 49 |
| 50 | w.Header().Set("Content-Type", "application/json") |
| 51 | w.WriteHeader(code) |
| 52 | _ = json.NewEncoder(w).Encode(GeneralResponse{Code: code, Msg: err.Error()}) |
| 53 | return |
| 54 | } |
| 55 |
| 56 | if res == nil { |
| 57 | w.WriteHeader(http.StatusOK) |
| 58 | return |
| 59 | } |
| 60 |
| 61 | switch v := res.(type) { |
| 62 | case []byte: |
| 63 | _, _ = w.Write(v) |
| 64 | case string: |
| 65 | _, _ = w.Write([]byte(v)) |
| 66 | default: |
| 67 | w.Header().Set("Content-Type", "application/json") |
| 68 | w.WriteHeader(http.StatusOK) |
| 69 | _ = json.NewEncoder(w).Encode(v) |
| 70 | } |
| 71 | } |
| 72 | } |
| 73 |
| 74 | // MakeHTTPHandlerFuncV2 wraps a handler response in the dashboard API v2 envelope. |
| 75 | func MakeHTTPHandlerFuncV2(handler APIHandler) http.HandlerFunc { |
| 76 | return func(w http.ResponseWriter, r *http.Request) { |
| 77 | ctx := NewContext(w, r) |
| 78 | res, err := handler(ctx) |
| 79 | if err != nil { |
| 80 | log.Warnf("http response [%s]: error: %v", r.URL.Path, err) |
| 81 | code := http.StatusInternalServerError |
| 82 | if e, ok := err.(*Error); ok { |
| 83 | code = e.Code |
| 84 | } |
| 85 |
| 86 | w.Header().Set("Content-Type", "application/json") |
| 87 | w.WriteHeader(code) |
| 88 | _ = json.NewEncoder(w).Encode(V2Response{Code: code, Msg: err.Error(), Data: nil}) |
| 89 | return |
| 90 | } |
| 91 |
| 92 | w.Header().Set("Content-Type", "application/json") |
| 93 | w.WriteHeader(http.StatusOK) |
| 94 | _ = json.NewEncoder(w).Encode(V2Response{Code: http.StatusOK, Msg: "success", Data: res}) |
| 95 | } |
| 96 | } |