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 {
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", ""),

View File

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

View File

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

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 {
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)