Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve the X-Request-Id and Request Details handling mechanism #1823

Merged
merged 1 commit into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 14 additions & 29 deletions src/api/rest/server/common/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,23 +44,20 @@ import (
// @Failure 500 {object} model.SimpleMsg
// @Router /config/{configId} [delete]
func RestInitConfig(c echo.Context) error {
reqID, idErr := common.StartRequestWithLog(c)
if idErr != nil {
return c.JSON(http.StatusBadRequest, map[string]string{"message": idErr.Error()})
}

if err := Validate(c, []string{"configId"}); err != nil {
log.Error().Err(err).Msg("")
return common.EndRequestWithLog(c, reqID, err, nil)
return common.EndRequestWithLog(c, err, nil)
}

err := common.InitConfig(c.Param("configId"))
if err != nil {
err := fmt.Errorf("Failed to init the config " + c.Param("configId"))
return common.EndRequestWithLog(c, reqID, err, nil)
return common.EndRequestWithLog(c, err, nil)
} else {
// return SendMessage(c, http.StatusOK, "The config "+c.Param("configId")+" has been initialized.")
content := map[string]string{"message": "The config " + c.Param("configId") + " has been initialized."}
return common.EndRequestWithLog(c, reqID, err, content)
return common.EndRequestWithLog(c, err, content)
}
}

Expand All @@ -77,10 +74,7 @@ func RestInitConfig(c echo.Context) error {
// @Failure 500 {object} model.SimpleMsg
// @Router /config/{configId} [get]
func RestGetConfig(c echo.Context) error {
reqID, idErr := common.StartRequestWithLog(c)
if idErr != nil {
return c.JSON(http.StatusBadRequest, map[string]string{"message": idErr.Error()})
}

if err := Validate(c, []string{"configId"}); err != nil {
log.Error().Err(err).Msg("")
return SendMessage(c, http.StatusBadRequest, err.Error())
Expand All @@ -89,9 +83,9 @@ func RestGetConfig(c echo.Context) error {
content, err := common.GetConfig(c.Param("configId"))
if err != nil {
err := fmt.Errorf("Failed to find the config " + c.Param("configId"))
return common.EndRequestWithLog(c, reqID, err, nil)
return common.EndRequestWithLog(c, err, nil)
} else {
return common.EndRequestWithLog(c, reqID, err, content)
return common.EndRequestWithLog(c, err, content)
}
}

Expand All @@ -113,15 +107,12 @@ type RestGetAllConfigResponse struct {
// @Failure 500 {object} model.SimpleMsg
// @Router /config [get]
func RestGetAllConfig(c echo.Context) error {
reqID, idErr := common.StartRequestWithLog(c)
if idErr != nil {
return c.JSON(http.StatusBadRequest, map[string]string{"message": idErr.Error()})
}

var content RestGetAllConfigResponse

configList, err := common.ListConfig()
content.Config = configList
return common.EndRequestWithLog(c, reqID, err, content)
return common.EndRequestWithLog(c, err, content)
}

// RestPostConfig godoc
Expand All @@ -137,18 +128,15 @@ func RestGetAllConfig(c echo.Context) error {
// @Failure 500 {object} model.SimpleMsg
// @Router /config [post]
func RestPostConfig(c echo.Context) error {
reqID, idErr := common.StartRequestWithLog(c)
if idErr != nil {
return c.JSON(http.StatusBadRequest, map[string]string{"message": idErr.Error()})
}

u := &model.ConfigReq{}
if err := c.Bind(u); err != nil {
return common.EndRequestWithLog(c, reqID, err, nil)
return common.EndRequestWithLog(c, err, nil)
}

log.Debug().Msg("[Creating or Updating Config]")
content, err := common.UpdateConfig(u)
return common.EndRequestWithLog(c, reqID, err, content)
return common.EndRequestWithLog(c, err, content)

}

Expand All @@ -163,14 +151,11 @@ func RestPostConfig(c echo.Context) error {
// @Failure 404 {object} model.SimpleMsg
// @Router /config [delete]
func RestInitAllConfig(c echo.Context) error {
reqID, idErr := common.StartRequestWithLog(c)
if idErr != nil {
return c.JSON(http.StatusBadRequest, map[string]string{"message": idErr.Error()})
}

err := common.InitAllConfig()
content := map[string]string{
"message": "All configs has been initialized"}
return common.EndRequestWithLog(c, reqID, err, content)
return common.EndRequestWithLog(c, err, content)
}

// RestGetRequest godoc
Expand Down
41 changes: 10 additions & 31 deletions src/api/rest/server/common/label/label.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package label

import (
"fmt"
"net/http"

"github.com/labstack/echo/v4"

Expand All @@ -40,18 +39,14 @@ import (
// @Failure 500 {object} model.SimpleMsg "Internal Server Error"
// @Router /label/{labelType}/{uid} [put]
func RestCreateOrUpdateLabel(c echo.Context) error {
reqID, idErr := common.StartRequestWithLog(c)
if idErr != nil {
return c.JSON(http.StatusBadRequest, map[string]string{"message": idErr.Error()})
}

labelType := c.Param("labelType")
uid := c.Param("uid")

// Parse the incoming request body to get the labels
var labelReq model.Label
if err := c.Bind(&labelReq); err != nil {
return common.EndRequestWithLog(c, reqID, fmt.Errorf("Invalid request body"), nil)
return common.EndRequestWithLog(c, fmt.Errorf("Invalid request body"), nil)
}

// Get the resource key
Expand All @@ -60,10 +55,10 @@ func RestCreateOrUpdateLabel(c echo.Context) error {
// Create or update the label in the KV store
err := label.CreateOrUpdateLabel(labelType, uid, resourceKey, labelReq.Labels)
if err != nil {
return common.EndRequestWithLog(c, reqID, err, nil)
return common.EndRequestWithLog(c, err, nil)
}

return common.EndRequestWithLog(c, reqID, nil, map[string]string{"message": "Label created or updated successfully"})
return common.EndRequestWithLog(c, nil, map[string]string{"message": "Label created or updated successfully"})
}

// RestRemoveLabel godoc
Expand All @@ -81,10 +76,6 @@ func RestCreateOrUpdateLabel(c echo.Context) error {
// @Failure 500 {object} model.SimpleMsg "Internal Server Error"
// @Router /label/{labelType}/{uid}/{key} [delete]
func RestRemoveLabel(c echo.Context) error {
reqID, idErr := common.StartRequestWithLog(c)
if idErr != nil {
return c.JSON(http.StatusBadRequest, map[string]string{"message": idErr.Error()})
}

labelType := c.Param("labelType")
uid := c.Param("uid")
Expand All @@ -93,10 +84,10 @@ func RestRemoveLabel(c echo.Context) error {
// Remove the label from the KV store
err := label.RemoveLabel(labelType, uid, key)
if err != nil {
return common.EndRequestWithLog(c, reqID, err, nil)
return common.EndRequestWithLog(c, err, nil)
}

return common.EndRequestWithLog(c, reqID, nil, map[string]string{"message": "Label removed successfully"})
return common.EndRequestWithLog(c, nil, map[string]string{"message": "Label removed successfully"})
}

// RestGetLabels godoc
Expand All @@ -113,21 +104,17 @@ func RestRemoveLabel(c echo.Context) error {
// @Failure 500 {object} model.SimpleMsg "Internal Server Error"
// @Router /label/{labelType}/{uid} [get]
func RestGetLabels(c echo.Context) error {
reqID, idErr := common.StartRequestWithLog(c)
if idErr != nil {
return c.JSON(http.StatusBadRequest, map[string]string{"message": idErr.Error()})
}

labelType := c.Param("labelType")
uid := c.Param("uid")

// Get the labels from the KV store
labelInfo, err := label.GetLabels(labelType, uid)
if err != nil {
return common.EndRequestWithLog(c, reqID, err, nil)
return common.EndRequestWithLog(c, err, nil)
}

return common.EndRequestWithLog(c, reqID, nil, labelInfo)
return common.EndRequestWithLog(c, nil, labelInfo)
}

// ResourcesResponse is a struct to wrap the results of a label selector query
Expand Down Expand Up @@ -155,26 +142,22 @@ type ResourcesResponse struct {
// @Failure 500 {object} model.SimpleMsg "Internal Server Error"
// @Router /resources/{labelType} [get]
func RestGetResourcesByLabelSelector(c echo.Context) error {
reqID, idErr := common.StartRequestWithLog(c)
if idErr != nil {
return c.JSON(http.StatusBadRequest, map[string]string{"message": idErr.Error()})
}

labelType := c.Param("labelType")
labelSelector := c.QueryParam("labelSelector")

// Get resources based on the label selector
resources, err := label.GetResourcesByLabelSelector(labelType, labelSelector)
if err != nil {
return common.EndRequestWithLog(c, reqID, err, nil)
return common.EndRequestWithLog(c, err, nil)
}

// Wrap the results in a JSON object
response := ResourcesResponse{
Results: resources,
}

return common.EndRequestWithLog(c, reqID, nil, response)
return common.EndRequestWithLog(c, nil, response)
}

// RestGetSystemLabelInfo godoc
Expand All @@ -188,10 +171,6 @@ func RestGetResourcesByLabelSelector(c echo.Context) error {
// @Failure 500 {object} model.SimpleMsg "Internal Server Error"
// @Router /labelInfo [get]
func RestGetSystemLabelInfo(c echo.Context) error {
reqID, idErr := common.StartRequestWithLog(c)
if idErr != nil {
return c.JSON(http.StatusInternalServerError, map[string]string{"message": idErr.Error()})
}

// Use the GetLabelConstantsMap function to get the system label constants
systemLabels := model.GetLabelConstantsMap()
Expand All @@ -203,5 +182,5 @@ func RestGetSystemLabelInfo(c echo.Context) error {
LabelTypes: labelTypes,
}

return common.EndRequestWithLog(c, reqID, nil, systemLabelInfo)
return common.EndRequestWithLog(c, nil, systemLabelInfo)
}
Loading