add store service

This commit is contained in:
kandrusyak
2023-06-30 17:13:46 +03:00
parent 7f4122360f
commit 8f904217f2
5 changed files with 39 additions and 7 deletions

View File

@@ -6,6 +6,7 @@ import (
type MSHostsConfig struct { type MSHostsConfig struct {
UsersHost string UsersHost string
StoreHost string
} }
type ServerConfig struct { type ServerConfig struct {
@@ -22,6 +23,7 @@ func New() *Config {
return &Config{ return &Config{
MSHosts: MSHostsConfig{ MSHosts: MSHostsConfig{
UsersHost: getEnv("USERS_HOST", ""), UsersHost: getEnv("USERS_HOST", ""),
StoreHost: getEnv("STORE_HOST", ""),
}, },
Server: ServerConfig{ Server: ServerConfig{
Port: getEnv("PORT", ""), Port: getEnv("PORT", ""),

View File

@@ -4,6 +4,7 @@ import (
"astra-api-gateway/config" "astra-api-gateway/config"
"astra-api-gateway/handler" "astra-api-gateway/handler"
"astra-api-gateway/mw" "astra-api-gateway/mw"
"astra-api-gateway/services/store"
"astra-api-gateway/services/users" "astra-api-gateway/services/users"
"github.com/joho/godotenv" "github.com/joho/godotenv"
"github.com/labstack/echo/v4" "github.com/labstack/echo/v4"
@@ -39,6 +40,10 @@ func main() {
e.DELETE("/users/:id", users.DeleteUser) e.DELETE("/users/:id", users.DeleteUser)
e.PATCH("/users/:id", users.UpdateUser) 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)) e.Logger.Fatal(e.Start(":" + config.Env.Server.Port))
} }

View File

@@ -34,25 +34,27 @@ func PostRequest(url string, model any, header http.Header) ([]byte, error) {
return data, nil 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{} client := http.Client{}
req, err := http.NewRequest("GET", url, nil) 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 = header
req.Header.Set("Content-Type", "application/json") req.Header.Set("Content-Type", "application/json")
resp, err := client.Do(req) resp, err := client.Do(req)
if err != nil { if err != nil {
return nil, err return nil, err, nil
} }
data, err := io.ReadAll(resp.Body) data, err := io.ReadAll(resp.Body)
if err != nil { 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) { func PatchRequest(url string, model any, header http.Header) ([]byte, error) {

23
services/store/store.go Normal file
View File

@@ -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)
}

View File

@@ -55,7 +55,7 @@ func Login(c echo.Context) (err error) {
} }
func GetCurrentUser(c echo.Context) 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 { if err != nil {
return c.NoContent(http.StatusBadGateway) return c.NoContent(http.StatusBadGateway)
@@ -67,7 +67,7 @@ func GetCurrentUser(c echo.Context) error {
func GetUsers(c echo.Context) error { func GetUsers(c echo.Context) error {
id := c.Param("id") id := c.Param("id")
data, err := api.GetRequest( data, err, _ := api.GetRequest(
config.Env.MSHosts.UsersHost+"/"+id, config.Env.MSHosts.UsersHost+"/"+id,
c.Request().Header, c.Request().URL.RawQuery) c.Request().Header, c.Request().URL.RawQuery)