installed gin

This commit is contained in:
2026-01-13 22:07:20 -07:00
parent 851234d9fa
commit 7481cec0d5
29 changed files with 544 additions and 1094 deletions
+7 -7
View File
@@ -1,22 +1,22 @@
package response
import (
"net/http"
"github.com/gin-gonic/gin"
)
type (
HandlerFunc = func(r *http.Request) (Response, error)
HandlerFunc = func(c *gin.Context) (Response, error)
Middleware = func(HandlerFunc) HandlerFunc
)
func Handler(f HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
res, err := f(r)
func Handler(f HandlerFunc) gin.HandlerFunc {
return func(c *gin.Context) {
res, err := f(c)
if err != nil {
WriteError(w, err)
WriteError(c, err)
} else {
Write(w, r, res)
Write(c, res)
}
}
}
+8 -4
View File
@@ -4,9 +4,13 @@ import (
"fmt"
"io"
"net/http"
"github.com/gin-gonic/gin"
)
func Write(w http.ResponseWriter, r *http.Request, res Response) {
func Write(c *gin.Context, res Response) {
w := c.Writer
// w.Header() must be set before ResponseWriter.WriteHeader is called
// or redirect is attempted
hdrs := w.Header()
@@ -19,7 +23,7 @@ func Write(w http.ResponseWriter, r *http.Request, res Response) {
}
if code, to, ok := res.GetRedirect(); ok {
http.Redirect(w, r, to, code.Int())
http.Redirect(w, c.Request, to, code.Int())
return
}
@@ -47,8 +51,8 @@ func Write(w http.ResponseWriter, r *http.Request, res Response) {
}
func WriteError(w http.ResponseWriter, err error) {
http.Error(w, err.Error(), GetStatusFromError(err))
func WriteError(c *gin.Context, err error) {
c.String(GetStatusFromError(err), err.Error())
}
func GetStatusFromError(err error) int {