ginhelper

package
v2.7.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 21, 2025 License: LGPL-3.0 Imports: 13 Imported by: 0

Documentation

Overview

Funciones auxiliares para GIN-GONIC

Funciones auxiliares para GIN-GONIC

Funciones auxiliares para GIN-GONIC

Example
package main

import (
	"bytes"
	"net/http"
	"net/http/httptest"

	"github.com/gin-contrib/cors"
	"github.com/gin-gonic/gin"
	"github.com/horus-es/go-util/v2/ginhelper"
)

func main() {
	// Modo producción
	gin.SetMode(gin.ReleaseMode)
	// Crea el router
	router := gin.New()
	// Evitamos los redirect si falta la barra final
	router.RedirectTrailingSlash = false
	// Middleware recuperación errores
	router.Use(ginhelper.MiddlewarePanic())
	// Middleware CORS
	corsCfg := cors.DefaultConfig()
	corsCfg.AllowAllOrigins = true
	corsCfg.AddAllowHeaders("Authorization")
	router.Use(cors.New(corsCfg))
	// Middleware no implementado y debugLogger
	router.Use(ginhelper.MiddlewareNotImplemented(), ginhelper.MiddlewareLogger(true))

	// Rutas
	router.GET("/ping", func(c *gin.Context) {
		c.String(200, "pong")
	})
	router.POST("/json", func(c *gin.Context) {
		c.String(201, `{"response":"Sorry, the market was closed ..."}`)
	})
	router.POST("/multipart", func(c *gin.Context) {
		c.PureJSON(201, gin.H{})
	})

	// Solicitud Ping/Pong
	req, _ := http.NewRequest("GET", "/ping", nil)
	w := httptest.NewRecorder()
	router.ServeHTTP(w, req)

	// Solicitud JSON
	req, _ = http.NewRequest("POST", "/json", bytes.NewBufferString(`{"request":"Buy cheese and bread for breakfast."}`))
	req.Header.Set("Content-Type", "application/json")
	w = httptest.NewRecorder()
	router.ServeHTTP(w, req)

	// Solicitud multipart/form-data
	body := `-----------------------------9051914041544843365972754266
Content-Disposition: form-data; name="text"

text default
-----------------------------9051914041544843365972754266
Content-Disposition: form-data; name="file1"; filename="a.txt"
Content-Type: text/plain

Content of a.txt.

-----------------------------9051914041544843365972754266
Content-Disposition: form-data; name="file2"; filename="a.html"
Content-Type: text/html

<!DOCTYPE html><title>Content of a.html.</title>

-----------------------------9051914041544843365972754266--`

	req, _ = http.NewRequest("POST", "/multipart", bytes.NewBufferString(body))
	req.Header.Set("Content-Type", "multipart/form-data; boundary=---------------------------9051914041544843365972754266")
	w = httptest.NewRecorder()
	router.ServeHTTP(w, req)

}
Output:

INFO: GET /ping
INFO: HTTP 200 OK - 0ms
INFO: pong
INFO: ==================================================
INFO: POST /json
INFO: {"request":"Buy cheese and bread for breakfast."}
INFO: HTTP 201 Created - 0ms
INFO: {"response":"Sorry, the market was closed ..."}
INFO: ==================================================
INFO: POST /multipart
INFO: Content-Type: multipart/form-data
INFO: Content-Length: 538
INFO: HTTP 201 Created - 0ms
INFO: {}
INFO: ==================================================

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func BadRequestResponse

func BadRequestResponse(msg string, causa any) map[string]any

Genera una respuesta json/REST a una solicitud incorrecta. Incluye un mensaje de error para el usuario y opcionalmente la causa del error para depuración.

func HandleNoRoute

func HandleNoRoute(debug bool, proxy string) gin.HandlerFunc

Devuelve el handler de ruta inxistente (a.k.a. 404)

func InitGinHelper

func InitGinHelper(logger *logger.Logger)

Establece el logger. Si el logger es nil, se usa el logger por defecto.

func MiddlewareLogger

func MiddlewareLogger(debug bool) gin.HandlerFunc

Devuelve el logger de depuración o de producción

func MiddlewareNotImplemented

func MiddlewareNotImplemented() gin.HandlerFunc

Middleware para inicializar estado a no implementado

func MiddlewarePanic

func MiddlewarePanic() gin.HandlerFunc

Middleware de recuperación de errores

func MiddlewareTransaction

func MiddlewareTransaction() gin.HandlerFunc

Middleware de gestión de transacciones

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL