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

@@ -7,6 +7,7 @@ import (
type MSHostsConfig struct {
UsersHost string
StoreHost string
EventsHost string
}
type ServerConfig struct {
@@ -24,6 +25,7 @@ func New() *Config {
MSHosts: MSHostsConfig{
UsersHost: getEnv("ASTRA-USERS", "http://localhost:8082"),
StoreHost: getEnv("ASTRA-FILE-STORAGE", "http://localhost:8081"),
EventsHost: getEnv("ASTRA-EVENTS", "http://localhost:8083"),
},
Server: ServerConfig{
Port: getEnv("PORT", "8080"),

View File

@@ -4,6 +4,7 @@ import (
"astra-api-gateway/config"
"astra-api-gateway/handler"
"astra-api-gateway/mw"
"astra-api-gateway/services/events"
"astra-api-gateway/services/store"
"astra-api-gateway/services/users"
"github.com/joho/godotenv"
@@ -39,8 +40,14 @@ func main() {
e.GET("/users/:id", users.GetUsers)
e.DELETE("/users/:id", users.DeleteUser)
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_g := e.Group("/store")

View File

@@ -7,31 +7,25 @@ import (
"net/http"
)
func PostRequest(url string, model any, header http.Header) ([]byte, error) {
uJson, err := json.Marshal(model)
if err != nil {
return nil, err
}
func PostRequest(url string, body io.Reader, header http.Header) ([]byte, error, *http.Response) {
client := http.Client{}
req, err := http.NewRequest("POST", url, bytes.NewReader(uJson))
req, err := http.NewRequest("POST", url, body)
req.Header = header
req.Header.Set("Content-Type", "application/json")
resp, err := client.Do(req)
if err != nil {
return nil, err
return nil, err, resp
}
data, err := io.ReadAll(resp.Body)
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) {
@@ -84,7 +78,7 @@ func PatchRequest(url string, model any, header http.Header) ([]byte, error) {
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{}
req, err := http.NewRequest("DELETE", url, nil)
req.Header = header
@@ -92,14 +86,14 @@ func DeleteRequest(url string, header http.Header) ([]byte, error) {
resp, err := client.Do(req)
if err != nil {
return nil, err
return nil, err, resp
}
data, err := io.ReadAll(resp.Body)
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) {
// Bind
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)
u.Password = ""
data, err, _ := api.PostRequest(config.Env.MSHosts.UsersHost+"/login", c.Request().Body, c.Request().Header)
if err != nil {
return c.NoContent(http.StatusBadGateway)
@@ -85,7 +79,7 @@ func GetUsers(c echo.Context) error {
func DeleteUser(c echo.Context) error {
id := c.Param("id")
data, err := api.DeleteRequest(
data, err, resp := api.DeleteRequest(
config.Env.MSHosts.UsersHost+"/"+id,
c.Request().Header)
@@ -93,7 +87,7 @@ func DeleteUser(c echo.Context) error {
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) {
@@ -125,3 +119,15 @@ func GetOrganizations(c echo.Context) error {
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)
}