From 8f904217f25df4674d15b9f1706e66f14e7d243c Mon Sep 17 00:00:00 2001 From: kandrusyak Date: Fri, 30 Jun 2023 17:13:46 +0300 Subject: [PATCH] add store service --- config/config.go | 2 ++ main.go | 5 +++++ pkg/api/apiCall.go | 12 +++++++----- services/store/store.go | 23 +++++++++++++++++++++++ services/users/users.go | 4 ++-- 5 files changed, 39 insertions(+), 7 deletions(-) create mode 100644 services/store/store.go diff --git a/config/config.go b/config/config.go index a55f7ff..595ad6e 100644 --- a/config/config.go +++ b/config/config.go @@ -6,6 +6,7 @@ import ( type MSHostsConfig struct { UsersHost string + StoreHost string } type ServerConfig struct { @@ -22,6 +23,7 @@ func New() *Config { return &Config{ MSHosts: MSHostsConfig{ UsersHost: getEnv("USERS_HOST", ""), + StoreHost: getEnv("STORE_HOST", ""), }, Server: ServerConfig{ Port: getEnv("PORT", ""), diff --git a/main.go b/main.go index 7d3867f..ca69d23 100644 --- a/main.go +++ b/main.go @@ -4,6 +4,7 @@ import ( "astra-api-gateway/config" "astra-api-gateway/handler" "astra-api-gateway/mw" + "astra-api-gateway/services/store" "astra-api-gateway/services/users" "github.com/joho/godotenv" "github.com/labstack/echo/v4" @@ -39,6 +40,10 @@ func main() { e.DELETE("/users/:id", users.DeleteUser) e.PATCH("/users/:id", users.UpdateUser) + // Store + store_g := e.Group("/store") + store_g.GET("/*", store.GetFileFromStore) + e.Logger.Fatal(e.Start(":" + config.Env.Server.Port)) } diff --git a/pkg/api/apiCall.go b/pkg/api/apiCall.go index 61ba0f8..d8ab5a1 100644 --- a/pkg/api/apiCall.go +++ b/pkg/api/apiCall.go @@ -34,25 +34,27 @@ func PostRequest(url string, model any, header http.Header) ([]byte, error) { return data, nil } -func GetRequest(url string, header http.Header, args ...string) ([]byte, error) { +func GetRequest(url string, header http.Header, args ...string) ([]byte, error, *http.Response) { client := http.Client{} req, err := http.NewRequest("GET", url, nil) - req.URL.RawQuery = args[0] + if len(args) > 0 { + req.URL.RawQuery = args[0] + } req.Header = header req.Header.Set("Content-Type", "application/json") resp, err := client.Do(req) if err != nil { - return nil, err + return nil, err, nil } data, err := io.ReadAll(resp.Body) if err != nil { - return nil, err + return nil, err, nil } - return data, nil + return data, nil, resp } func PatchRequest(url string, model any, header http.Header) ([]byte, error) { diff --git a/services/store/store.go b/services/store/store.go new file mode 100644 index 0000000..10b8d99 --- /dev/null +++ b/services/store/store.go @@ -0,0 +1,23 @@ +package store + +import ( + "astra-api-gateway/config" + "astra-api-gateway/pkg/api" + "github.com/labstack/echo/v4" + "net/http" + "strings" +) + +func GetFileFromStore(c echo.Context) error { + fileName := strings.ReplaceAll(c.Request().RequestURI, "/store", "") + + data, err, req := api.GetRequest( + config.Env.MSHosts.StoreHost+fileName, + c.Request().Header, + ) + if err != nil { + return c.NoContent(http.StatusBadGateway) + } + + return c.Blob(req.StatusCode, req.Header.Get("Content-Type"), data) +} diff --git a/services/users/users.go b/services/users/users.go index e8d4999..7a394ec 100644 --- a/services/users/users.go +++ b/services/users/users.go @@ -55,7 +55,7 @@ func Login(c echo.Context) (err error) { } func GetCurrentUser(c echo.Context) error { - data, err := api.GetRequest(config.Env.MSHosts.UsersHost+"/me", c.Request().Header) + data, err, _ := api.GetRequest(config.Env.MSHosts.UsersHost+"/me", c.Request().Header) if err != nil { return c.NoContent(http.StatusBadGateway) @@ -67,7 +67,7 @@ func GetCurrentUser(c echo.Context) error { func GetUsers(c echo.Context) error { id := c.Param("id") - data, err := api.GetRequest( + data, err, _ := api.GetRequest( config.Env.MSHosts.UsersHost+"/"+id, c.Request().Header, c.Request().URL.RawQuery)