github.com/kenshin579/tutorials-go/go-echo-server@v0.0.0-20230308085259-a57c47a93bb4/echo/route/http/echo_handler.go (about)

     1  package http
     2  
     3  import (
     4  	"io"
     5  	"net/http"
     6  
     7  	"github.com/labstack/gommon/log"
     8  
     9  	"github.com/labstack/echo/v4"
    10  )
    11  
    12  type echoHandler struct {
    13  }
    14  
    15  func NewEchoHandler(e *echo.Echo) *echoHandler {
    16  	handler := &echoHandler{}
    17  
    18  	e.POST("/echo", handler.EchoHandler)
    19  
    20  	return handler
    21  }
    22  
    23  // EchoHandler godoc
    24  // @ID EchoHandler
    25  // @Tags Echo
    26  // @Summary Echo API
    27  // @Description Echo API
    28  // @Success 200 {string} Body "Request 값을 그대로 반환한다"
    29  // @Router /echo [get]
    30  func (handler echoHandler) EchoHandler(ctx echo.Context) error {
    31  	body, err := io.ReadAll(ctx.Request().Body)
    32  	if err != nil {
    33  		log.Error(err)
    34  	}
    35  	log.Infof("request:%v", string(body))
    36  
    37  	return ctx.JSON(http.StatusOK, body)
    38  }