|
| 1 | +// This file is part of MinIO Console Server |
| 2 | +// Copyright (c) 2020 MinIO, Inc. |
| 3 | +// |
| 4 | +// This program is free software: you can redistribute it and/or modify |
| 5 | +// it under the terms of the GNU Affero General Public License as published by |
| 6 | +// the Free Software Foundation, either version 3 of the License, or |
| 7 | +// (at your option) any later version. |
| 8 | +// |
| 9 | +// This program is distributed in the hope that it will be useful, |
| 10 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | +// GNU Affero General Public License for more details. |
| 13 | +// |
| 14 | +// You should have received a copy of the GNU Affero General Public License |
| 15 | +// along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | + |
| 17 | +package restapi |
| 18 | + |
| 19 | +import ( |
| 20 | + "bufio" |
| 21 | + "errors" |
| 22 | + "fmt" |
| 23 | + "net/http" |
| 24 | + "strings" |
| 25 | + |
| 26 | + apiErrors "github.com/go-openapi/errors" |
| 27 | +) |
| 28 | + |
| 29 | +// serverMkube handles calls for mkube |
| 30 | +func serverMkube(client *http.Client, w http.ResponseWriter, req *http.Request) { |
| 31 | + // destination of the request, the mkube server |
| 32 | + targetURL := fmt.Sprintf("%s%s", getM3Host(), req.URL.String()) |
| 33 | + |
| 34 | + // set the HTTP method, url, and m3Req body |
| 35 | + m3Req, err := http.NewRequest(req.Method, targetURL, req.Body) |
| 36 | + if err != nil { |
| 37 | + apiErrors.ServeError(w, req, err) |
| 38 | + return |
| 39 | + } |
| 40 | + |
| 41 | + // set the m3Req headers |
| 42 | + m3Req.Header = req.Header |
| 43 | + resp, err := client.Do(m3Req) |
| 44 | + if err != nil { |
| 45 | + if strings.Contains(err.Error(), "connection refused") { |
| 46 | + apiErrors.ServeError(w, req, errors.New("service M3 is not available")) |
| 47 | + return |
| 48 | + } |
| 49 | + apiErrors.ServeError(w, req, err) |
| 50 | + return |
| 51 | + } |
| 52 | + defer resp.Body.Close() |
| 53 | + // Write the m3 response to the response writer |
| 54 | + scanner := bufio.NewScanner(resp.Body) |
| 55 | + for i := 0; scanner.Scan() && i < 5; i++ { |
| 56 | + w.Write(scanner.Bytes()) |
| 57 | + } |
| 58 | + if err := scanner.Err(); err != nil { |
| 59 | + apiErrors.ServeError(w, req, err) |
| 60 | + } |
| 61 | + |
| 62 | +} |
0 commit comments