From fd486bbe0997bf8afb348050c81ead18ef3fc54d Mon Sep 17 00:00:00 2001 From: Simone Gotti Date: Fri, 22 Feb 2019 09:34:48 +0100 Subject: [PATCH] gateway: add internal git server repos api --- internal/services/gateway/api/repos.go | 92 ++++++++++++++++++++++++++ internal/services/gateway/gateway.go | 4 ++ 2 files changed, 96 insertions(+) create mode 100644 internal/services/gateway/api/repos.go diff --git a/internal/services/gateway/api/repos.go b/internal/services/gateway/api/repos.go new file mode 100644 index 0000000..4e9daa3 --- /dev/null +++ b/internal/services/gateway/api/repos.go @@ -0,0 +1,92 @@ +// Copyright 2019 Sorint.lab +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied +// See the License for the specific language governing permissions and +// limitations under the License. + +package api + +import ( + "io" + "net/http" + "net/url" + + csapi "github.com/sorintlab/agola/internal/services/configstore/api" + "go.uber.org/zap" + + "github.com/gorilla/mux" +) + +type ReposHandler struct { + log *zap.SugaredLogger + configstoreClient *csapi.Client +} + +func NewReposHandler(logger *zap.Logger, configstoreClient *csapi.Client) *ReposHandler { + return &ReposHandler{log: logger.Sugar(), configstoreClient: configstoreClient} +} + +func (h *ReposHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { + ctx := r.Context() + vars := mux.Vars(r) + path := vars["rest"] + + h.log.Infof("path: %s", path) + + u, err := url.Parse("http://172.17.0.1:4003") + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + u.Path = path + u.RawQuery = r.URL.RawQuery + + h.log.Infof("u: %s", u.String()) + // TODO(sgotti) Check authorized call from client + + defer r.Body.Close() + // proxy all the request body to the destination server + req, err := http.NewRequest(r.Method, u.String(), r.Body) + req = req.WithContext(ctx) + if err != nil { + http.Error(w, err.Error(), http.StatusBadRequest) + return + } + + // copy request headers + for k, vv := range r.Header { + for _, v := range vv { + req.Header.Add(k, v) + } + } + + resp, err := http.DefaultClient.Do(req) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + // copy response headers + for k, vv := range resp.Header { + for _, v := range vv { + w.Header().Add(k, v) + } + } + // copy status + w.WriteHeader(resp.StatusCode) + + defer resp.Body.Close() + // copy response body + if _, err := io.Copy(w, resp.Body); err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } +} diff --git a/internal/services/gateway/gateway.go b/internal/services/gateway/gateway.go index 678b007..15424d3 100644 --- a/internal/services/gateway/gateway.go +++ b/internal/services/gateway/gateway.go @@ -171,6 +171,8 @@ func (g *Gateway) Run(ctx context.Context) error { logsHandler := api.NewLogsHandler(logger, g.runserviceClient) + reposHandler := api.NewReposHandler(logger, g.configstoreClient) + loginUserHandler := api.NewLoginUserHandler(logger, g.ch, g.configstoreClient) oauth2callbackHandler := api.NewOAuth2CallbackHandler(logger, g.ch, g.configstoreClient) @@ -214,6 +216,8 @@ func (g *Gateway) Run(ctx context.Context) error { router.Handle("/login", loginUserHandler).Methods("POST") router.Handle("/oauth2/callback", oauth2callbackHandler).Methods("GET") + router.Handle("/repos/{rest:.*}", reposHandler).Methods("GET", "POST") + router.Handle("/webhooks", webhooksHandler).Methods("POST") router.PathPrefix("/").HandlerFunc(handlers.NewWebBundleHandlerFunc(g.c.APIExposedURL))