feat:add context interface, and change core.writeresponse function to use it

This commit is contained in:
Muyao CHEN 2024-10-04 23:40:45 +02:00
parent 321b4704a2
commit 4794137d42
2 changed files with 6 additions and 2 deletions

View File

@ -0,0 +1,5 @@
package core
type Context interface {
JSON(code int, obj any)
}

View File

@ -26,7 +26,6 @@ import (
"net/http" "net/http"
"git.vinchent.xyz/vinchent/howmuch/internal/pkg/errno" "git.vinchent.xyz/vinchent/howmuch/internal/pkg/errno"
"github.com/gin-gonic/gin"
) )
type ErrResponse struct { type ErrResponse struct {
@ -36,7 +35,7 @@ type ErrResponse struct {
// WriteResponse writes the response to the HTTP response writer with HTTP code // WriteResponse writes the response to the HTTP response writer with HTTP code
// and potential errors. // and potential errors.
func WriteResponse(c *gin.Context, err error, data any) { func WriteResponse(c Context, err error, data any) {
// No error, write json response directly // No error, write json response directly
if err == nil { if err == nil {
c.JSON(http.StatusOK, data) c.JSON(http.StatusOK, data)