add events

This commit is contained in:
andrusyakka
2023-07-17 14:30:22 +03:00
parent 9234b577bb
commit c8ce5a910e
5 changed files with 99 additions and 29 deletions

View File

@@ -5,8 +5,9 @@ import (
) )
type MSHostsConfig struct { type MSHostsConfig struct {
UsersHost string UsersHost string
StoreHost string StoreHost string
EventsHost string
} }
type ServerConfig struct { type ServerConfig struct {
@@ -22,8 +23,9 @@ type Config struct {
func New() *Config { func New() *Config {
return &Config{ return &Config{
MSHosts: MSHostsConfig{ MSHosts: MSHostsConfig{
UsersHost: getEnv("ASTRA-USERS", "http://localhost:8082"), UsersHost: getEnv("ASTRA-USERS", "http://localhost:8082"),
StoreHost: getEnv("ASTRA-FILE-STORAGE", "http://localhost:8081"), StoreHost: getEnv("ASTRA-FILE-STORAGE", "http://localhost:8081"),
EventsHost: getEnv("ASTRA-EVENTS", "http://localhost:8083"),
}, },
Server: ServerConfig{ Server: ServerConfig{
Port: getEnv("PORT", "8080"), Port: getEnv("PORT", "8080"),

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/events"
"astra-api-gateway/services/store" "astra-api-gateway/services/store"
"astra-api-gateway/services/users" "astra-api-gateway/services/users"
"github.com/joho/godotenv" "github.com/joho/godotenv"
@@ -39,8 +40,14 @@ func main() {
e.GET("/users/:id", users.GetUsers) e.GET("/users/:id", users.GetUsers)
e.DELETE("/users/:id", users.DeleteUser) e.DELETE("/users/:id", users.DeleteUser)
e.PATCH("/users/:id", users.UpdateUser) e.PATCH("/users/:id", users.UpdateUser)
e.GET("/users/organizations", users.GetOrganizations)
e.GET("/users/positions", users.GetPositions)
e.GET("/organizations", users.GetOrganizations) //Events
e.GET("/events", events.GetEvents)
e.GET("/events/:id", events.GetEvent)
e.POST("/events", events.CreateEvent)
e.DELETE("/events", events.DeleteEvent)
// Store // Store
store_g := e.Group("/store") store_g := e.Group("/store")

View File

@@ -7,31 +7,25 @@ import (
"net/http" "net/http"
) )
func PostRequest(url string, model any, header http.Header) ([]byte, error) { func PostRequest(url string, body io.Reader, header http.Header) ([]byte, error, *http.Response) {
uJson, err := json.Marshal(model)
if err != nil {
return nil, err
}
client := http.Client{} client := http.Client{}
req, err := http.NewRequest("POST", url, bytes.NewReader(uJson)) req, err := http.NewRequest("POST", url, body)
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, resp
} }
data, err := io.ReadAll(resp.Body) data, err := io.ReadAll(resp.Body)
if err != nil { if err != nil {
return nil, err return nil, err, resp
} }
return data, nil return data, nil, resp
} }
func GetRequest(url string, header http.Header, args ...string) ([]byte, error, *http.Response) { func GetRequest(url string, header http.Header, args ...string) ([]byte, error, *http.Response) {
@@ -84,7 +78,7 @@ func PatchRequest(url string, model any, header http.Header) ([]byte, error) {
return data, nil return data, nil
} }
func DeleteRequest(url string, header http.Header) ([]byte, error) { func DeleteRequest(url string, header http.Header) ([]byte, error, *http.Response) {
client := http.Client{} client := http.Client{}
req, err := http.NewRequest("DELETE", url, nil) req, err := http.NewRequest("DELETE", url, nil)
req.Header = header req.Header = header
@@ -92,14 +86,14 @@ func DeleteRequest(url string, header http.Header) ([]byte, error) {
resp, err := client.Do(req) resp, err := client.Do(req)
if err != nil { if err != nil {
return nil, err return nil, err, resp
} }
data, err := io.ReadAll(resp.Body) data, err := io.ReadAll(resp.Body)
if err != nil { if err != nil {
return nil, err return nil, err, resp
} }
return data, nil return data, nil, resp
} }

61
services/events/events.go Normal file
View File

@@ -0,0 +1,61 @@
package events
import (
"astra-api-gateway/config"
"astra-api-gateway/pkg/api"
"github.com/labstack/echo/v4"
"net/http"
)
func GetEvents(c echo.Context) error {
data, err, resp := api.GetRequest(
config.Env.MSHosts.EventsHost+"/",
c.Request().Header, c.Request().URL.RawQuery)
if err != nil {
return c.NoContent(http.StatusBadGateway)
}
return c.Blob(resp.StatusCode, resp.Header.Get("Content-Type"), data)
}
func GetEvent(c echo.Context) error {
id := c.Param("id")
data, err, resp := api.GetRequest(
config.Env.MSHosts.EventsHost+"/"+id,
c.Request().Header, c.Request().URL.RawQuery)
if err != nil {
return c.NoContent(http.StatusBadGateway)
}
return c.Blob(resp.StatusCode, resp.Header.Get("Content-Type"), data)
}
func CreateEvent(c echo.Context) error {
data, err, resp := api.PostRequest(
config.Env.MSHosts.EventsHost+"/",
c.Request().Body,
c.Request().Header)
if err != nil {
return c.NoContent(http.StatusBadGateway)
}
return c.Blob(resp.StatusCode, resp.Header.Get("Content-Type"), data)
}
func DeleteEvent(c echo.Context) error {
id := c.Param("id")
data, err, resp := api.DeleteRequest(
config.Env.MSHosts.EventsHost+"/"+id,
c.Request().Header)
if err != nil {
return c.NoContent(http.StatusBadGateway)
}
return c.Blob(resp.StatusCode, resp.Header.Get("Content-Type"), data)
}

View File

@@ -13,15 +13,9 @@ import (
) )
func Login(c echo.Context) (err error) { func Login(c echo.Context) (err error) {
// Bind
u := new(model.User) u := new(model.User)
if err = c.Bind(u); err != nil {
return
}
data, err := api.PostRequest(config.Env.MSHosts.UsersHost+"/login", u, c.Request().Header) data, err, _ := api.PostRequest(config.Env.MSHosts.UsersHost+"/login", c.Request().Body, c.Request().Header)
u.Password = ""
if err != nil { if err != nil {
return c.NoContent(http.StatusBadGateway) return c.NoContent(http.StatusBadGateway)
@@ -85,7 +79,7 @@ func GetUsers(c echo.Context) error {
func DeleteUser(c echo.Context) error { func DeleteUser(c echo.Context) error {
id := c.Param("id") id := c.Param("id")
data, err := api.DeleteRequest( data, err, resp := api.DeleteRequest(
config.Env.MSHosts.UsersHost+"/"+id, config.Env.MSHosts.UsersHost+"/"+id,
c.Request().Header) c.Request().Header)
@@ -93,7 +87,7 @@ func DeleteUser(c echo.Context) error {
return c.NoContent(http.StatusBadGateway) return c.NoContent(http.StatusBadGateway)
} }
return c.JSONBlob(http.StatusOK, data) return c.Blob(resp.StatusCode, resp.Header.Get("Content-Type"), data)
} }
func UpdateUser(c echo.Context) (err error) { func UpdateUser(c echo.Context) (err error) {
@@ -125,3 +119,15 @@ func GetOrganizations(c echo.Context) error {
return c.Blob(resp.StatusCode, resp.Header.Get("Content-Type"), data) return c.Blob(resp.StatusCode, resp.Header.Get("Content-Type"), data)
} }
func GetPositions(c echo.Context) error {
data, err, resp := api.GetRequest(
config.Env.MSHosts.UsersHost+"/positions",
c.Request().Header, c.Request().URL.RawQuery)
if err != nil {
return c.NoContent(http.StatusBadGateway)
}
return c.Blob(resp.StatusCode, resp.Header.Get("Content-Type"), data)
}