Help us shape the future of Go! Take the 2026 Go Developer Survey

gin

package module
v1.20.0-alpha.1 Latest Latest
Warning

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

Go to latest
Published: Oct 30, 2026 License: MIT Imports: 43 Imported by: 0

README

Gin

Doc Go Release Test Report Card Codecov License

Gin HTTP driver for github.com/gradii/framework, a fork of goravel/gin published as github.com/gradii/gin.

Install

Run the command below in your project to install the package automatically:

./artisan package:install github.com/gradii/gin

Or check the setup file to install the package manually.

Configuration

You can define the template configuration. If omitted, DefaultTemplate() is used automatically as a fallback, which loads views from resources/views and any registered package views.

You can provide a custom template configuration in two forms:

  • func() (render.HTMLRender, error) — a callback that returns a custom HTML renderer (e.g. to configure custom delimiters or a FuncMap).
  • render.HTMLRender — a pre-built renderer instance.

Custom example:

import (
    "html/template"

    "github.com/gin-gonic/gin/render"
    "github.com/gradii/gin"
)

"template": func() (render.HTMLRender, error) {
    return gin.NewTemplate(gin.RenderOptions{
        Delims:  &gin.Delims{Left: "{[", Right: "]}"},
        FuncMap: template.FuncMap{
            "upper": strings.ToUpper,
        },
    })
},

Testing

Run command below to run test:

go test ./...

Documentation

Index

Constants

View Source
const BindingRoute = "goravel.gin.route"

Variables

View Source
var (
	App              foundation.Application
	ConfigFacade     config.Config
	LogFacade        log.Log
	ValidationFacade validation.Validation
	ViewFacade       view.View
)

Functions

func Background

func Background() contractshttp.Context

func Cors

func Cors() http.Middleware

func DefaultTemplate

func DefaultTemplate() (*render.HTMLProduction, error)

DefaultTemplate compiles every registered view source with the functions registered on the view facade and no custom delimiters. Custom delimiters still require the http.drivers.<driver>.template callback, which keeps the registered functions because NewTemplate installs them itself.

func NewAction

func NewAction(method, path, handler string) contractsroute.Action

func NewContextRequest

func NewContextRequest(ctx *Context, log log.Log, validation contractsvalidate.Validation) contractshttp.ContextRequest

func NewContextResponse

func NewContextResponse(instance *gin.Context, origin contractshttp.ResponseOrigin) contractshttp.ContextResponse

func NewGroup

func NewGroup(config config.Config, instance gin.IRouter, prefix string, middlewares []contractshttp.Middleware, lastMiddlewares []contractshttp.Middleware) contractsroute.Router

func NewTemplate

func NewTemplate(options RenderOptions) (*render.HTMLProduction, error)

NewTemplate compiles the application's resources/views and every source registered on the view facade into a gin renderer. The functions registered on the view facade through View.AddFuncs, which is also where ApplicationBuilder.WithViewFuncs puts the application's, are installed first; options.FuncMap is added on top and overrides them by name. Precedence, naming and collision handling are those of view.Compile. It returns (nil, nil) when no view file exists, so callers keep gin's default renderer.

func ResponseMiddleware

func ResponseMiddleware() contractshttp.Middleware

func Timeout

func Timeout(timeout time.Duration) contractshttp.Middleware

Timeout creates middleware to set a timeout for a request

func Tls

Types

type Action

type Action struct {
	// contains filtered or unexported fields
}

func (*Action) Name

func (r *Action) Name(name string) contractsroute.Action

func (*Action) WithoutMiddleware

func (r *Action) WithoutMiddleware(middleware ...contractshttp.Middleware) contractsroute.Action

type BodyWriter

type BodyWriter struct {
	gin.ResponseWriter
	// contains filtered or unexported fields
}

func (*BodyWriter) Body

func (w *BodyWriter) Body() *bytes.Buffer

func (*BodyWriter) Header

func (w *BodyWriter) Header() http.Header

func (*BodyWriter) Write

func (w *BodyWriter) Write(b []byte) (int, error)

func (*BodyWriter) WriteString

func (w *BodyWriter) WriteString(s string) (int, error)

type Context

type Context struct {
	// contains filtered or unexported fields
}

func NewContext

func NewContext(c *gin.Context) *Context

func (*Context) Context

func (c *Context) Context() context.Context

func (*Context) Deadline

func (c *Context) Deadline() (deadline time.Time, ok bool)

func (*Context) Done

func (c *Context) Done() <-chan struct{}

func (*Context) Err

func (c *Context) Err() error

func (*Context) Instance

func (c *Context) Instance() *gin.Context

func (*Context) Request

func (c *Context) Request() contractshttp.ContextRequest

func (*Context) Response

func (c *Context) Response() contractshttp.ContextResponse

func (*Context) Value

func (c *Context) Value(key any) any

func (*Context) WithContext

func (c *Context) WithContext(ctx context.Context)

func (*Context) WithValue

func (c *Context) WithValue(key any, value any)

type ContextRequest

type ContextRequest struct {
	// contains filtered or unexported fields
}

func (*ContextRequest) Abort

func (r *ContextRequest) Abort(code ...int)

func (*ContextRequest) AbortWithStatus

func (r *ContextRequest) AbortWithStatus(code int)

DEPRECATED: Use Abort instead.

func (*ContextRequest) AbortWithStatusJson

func (r *ContextRequest) AbortWithStatusJson(code int, jsonObj any)

DEPRECATED: Use Response().Json().Abort() instead.

func (*ContextRequest) All

func (r *ContextRequest) All() map[string]any

func (*ContextRequest) Bind

func (r *ContextRequest) Bind(obj any) error

func (*ContextRequest) BindQuery

func (r *ContextRequest) BindQuery(obj any) error

func (*ContextRequest) Cookie

func (r *ContextRequest) Cookie(key string, defaultValue ...string) string

func (*ContextRequest) File

func (*ContextRequest) Files

func (r *ContextRequest) Files(name string) ([]contractsfilesystem.File, error)

func (*ContextRequest) Form

func (r *ContextRequest) Form(key string, defaultValue ...string) string

func (*ContextRequest) FullUrl

func (r *ContextRequest) FullUrl() string

func (*ContextRequest) HasSession

func (r *ContextRequest) HasSession() bool

func (*ContextRequest) Header

func (r *ContextRequest) Header(key string, defaultValue ...string) string

func (*ContextRequest) Headers

func (r *ContextRequest) Headers() http.Header

func (*ContextRequest) Host

func (r *ContextRequest) Host() string

func (*ContextRequest) Info

func (r *ContextRequest) Info() contractshttp.Info

func (*ContextRequest) Input

func (r *ContextRequest) Input(key string, defaultValue ...string) string

func (*ContextRequest) InputArray

func (r *ContextRequest) InputArray(key string, defaultValue ...[]string) []string

func (*ContextRequest) InputBool

func (r *ContextRequest) InputBool(key string, defaultValue ...bool) bool

func (*ContextRequest) InputInt

func (r *ContextRequest) InputInt(key string, defaultValue ...int) int

func (*ContextRequest) InputInt64

func (r *ContextRequest) InputInt64(key string, defaultValue ...int64) int64

func (*ContextRequest) InputMap

func (r *ContextRequest) InputMap(key string, defaultValue ...map[string]any) map[string]any

func (*ContextRequest) InputMapArray

func (r *ContextRequest) InputMapArray(key string, defaultValue ...[]map[string]any) []map[string]any

func (*ContextRequest) Ip

func (r *ContextRequest) Ip() string

func (*ContextRequest) Json

func (r *ContextRequest) Json(key string, defaultValue ...string) string

func (*ContextRequest) Method

func (r *ContextRequest) Method() string

func (*ContextRequest) Name

func (r *ContextRequest) Name() string

func (*ContextRequest) Next

func (r *ContextRequest) Next()

func (*ContextRequest) Origin

func (r *ContextRequest) Origin() *http.Request

func (*ContextRequest) OriginPath

func (r *ContextRequest) OriginPath() string

func (*ContextRequest) Path

func (r *ContextRequest) Path() string

func (*ContextRequest) Queries

func (r *ContextRequest) Queries() map[string]string

func (*ContextRequest) Query

func (r *ContextRequest) Query(key string, defaultValue ...string) string

func (*ContextRequest) QueryArray

func (r *ContextRequest) QueryArray(key string) []string

func (*ContextRequest) QueryBool

func (r *ContextRequest) QueryBool(key string, defaultValue ...bool) bool

func (*ContextRequest) QueryInt

func (r *ContextRequest) QueryInt(key string, defaultValue ...int) int

func (*ContextRequest) QueryInt64

func (r *ContextRequest) QueryInt64(key string, defaultValue ...int64) int64

func (*ContextRequest) QueryMap

func (r *ContextRequest) QueryMap(key string) map[string]string

func (*ContextRequest) Route

func (r *ContextRequest) Route(key string) string

func (*ContextRequest) RouteInt

func (r *ContextRequest) RouteInt(key string) int

func (*ContextRequest) RouteInt64

func (r *ContextRequest) RouteInt64(key string) int64

func (*ContextRequest) Session

func (r *ContextRequest) Session() contractsession.Session

func (*ContextRequest) SetSession

func (*ContextRequest) Url

func (r *ContextRequest) Url() string

func (*ContextRequest) Validate

func (r *ContextRequest) Validate(rules map[string]any, options ...contractsvalidate.Option) (contractsvalidate.Validator, error)

func (*ContextRequest) ValidateRequest

func (r *ContextRequest) ValidateRequest(request contractshttp.FormRequest) (contractsvalidate.Errors, error)

type ContextResponse

type ContextResponse struct {
	// contains filtered or unexported fields
}

func (*ContextResponse) Cookie

func (*ContextResponse) Data

func (r *ContextResponse) Data(code int, contentType string, data []byte) contractshttp.AbortableResponse

func (*ContextResponse) Download

func (r *ContextResponse) Download(filepath, filename string) contractshttp.Response

func (*ContextResponse) File

func (r *ContextResponse) File(filepath string) contractshttp.Response

func (*ContextResponse) Flush

func (r *ContextResponse) Flush()

func (*ContextResponse) Header

func (r *ContextResponse) Header(key, value string) contractshttp.ContextResponse

func (*ContextResponse) Json

func (*ContextResponse) NoContent

func (r *ContextResponse) NoContent(code ...int) contractshttp.AbortableResponse

func (*ContextResponse) Origin

func (*ContextResponse) Redirect

func (r *ContextResponse) Redirect(code int, location string) contractshttp.AbortableResponse

func (*ContextResponse) Status

func (*ContextResponse) Stream

func (*ContextResponse) String

func (r *ContextResponse) String(code int, format string, values ...any) contractshttp.AbortableResponse

func (*ContextResponse) Success

func (*ContextResponse) View

func (*ContextResponse) WithoutCookie

func (r *ContextResponse) WithoutCookie(name string) contractshttp.ContextResponse

func (*ContextResponse) Writer

func (r *ContextResponse) Writer() http.ResponseWriter

type CorsMiddleware

type CorsMiddleware struct{}

func (*CorsMiddleware) Handle

func (c *CorsMiddleware) Handle(ctx http.Context)

func (*CorsMiddleware) Signature

func (c *CorsMiddleware) Signature() string

type DataResponse

type DataResponse struct {
	// contains filtered or unexported fields
}

func (*DataResponse) Abort

func (r *DataResponse) Abort() error

func (*DataResponse) Render

func (r *DataResponse) Render() error

type Delims

type Delims struct {
	Left  string
	Right string
}

type DownloadResponse

type DownloadResponse struct {
	// contains filtered or unexported fields
}

func (*DownloadResponse) Render

func (r *DownloadResponse) Render() error

type FileResponse

type FileResponse struct {
	// contains filtered or unexported fields
}

func (*FileResponse) Render

func (r *FileResponse) Render() error

type Group

type Group struct {
	// contains filtered or unexported fields
}

func (*Group) Any

func (*Group) Delete

func (r *Group) Delete(path string, handler contractshttp.HandlerFunc) contractsroute.Action

func (*Group) Get

func (*Group) Group

func (r *Group) Group(handler contractsroute.GroupFunc)

func (*Group) Middleware

func (r *Group) Middleware(middlewares ...contractshttp.Middleware) contractsroute.Router

func (*Group) Options

func (r *Group) Options(path string, handler contractshttp.HandlerFunc) contractsroute.Action

func (*Group) Patch

func (r *Group) Patch(path string, handler contractshttp.HandlerFunc) contractsroute.Action

func (*Group) Post

func (r *Group) Post(path string, handler contractshttp.HandlerFunc) contractsroute.Action

func (*Group) Prefix

func (r *Group) Prefix(path string) contractsroute.Router

func (*Group) Put

func (*Group) Resource

func (r *Group) Resource(path string, controller contractshttp.ResourceController) contractsroute.Action

func (*Group) Static

func (r *Group) Static(path, root string) contractsroute.Action

func (*Group) StaticFS

func (r *Group) StaticFS(path string, fs http.FileSystem) contractsroute.Action

func (*Group) StaticFile

func (r *Group) StaticFile(path, filepath string) contractsroute.Action

func (*Group) WithMiddlewares

func (r *Group) WithMiddlewares() gin.IRoutes

func (*Group) WithoutMiddleware

func (r *Group) WithoutMiddleware(middlewares ...contractshttp.Middleware) contractsroute.Router

type HtmlResponse

type HtmlResponse struct {
	// contains filtered or unexported fields
}

func (*HtmlResponse) Render

func (r *HtmlResponse) Render() error

type JsonResponse

type JsonResponse struct {
	// contains filtered or unexported fields
}

func (*JsonResponse) Abort

func (r *JsonResponse) Abort() error

func (*JsonResponse) Render

func (r *JsonResponse) Render() error

type NoContentResponse

type NoContentResponse struct {
	// contains filtered or unexported fields
}

func (*NoContentResponse) Abort

func (r *NoContentResponse) Abort() error

func (*NoContentResponse) Render

func (r *NoContentResponse) Render() error

type RedirectResponse

type RedirectResponse struct {
	// contains filtered or unexported fields
}

func (*RedirectResponse) Abort

func (r *RedirectResponse) Abort() error

func (*RedirectResponse) Render

func (r *RedirectResponse) Render() error

type RenderOptions

type RenderOptions struct {
	Delims  *Delims
	FuncMap template.FuncMap
}

type Route

type Route struct {
	route.Router
	// contains filtered or unexported fields
}

func NewRoute

func NewRoute(config config.Config, parameters map[string]any) (*Route, error)

func (*Route) CompileViews

func (r *Route) CompileViews() error

CompileViews implements view.Compiler; the framework calls it after every provider has booted so compile errors surface at startup.

func (*Route) Fallback

func (r *Route) Fallback(handler contractshttp.HandlerFunc)

func (*Route) GetGlobalMiddleware

func (r *Route) GetGlobalMiddleware() []contractshttp.Middleware

func (*Route) GetRoutes

func (r *Route) GetRoutes() []contractshttp.Info

func (*Route) GlobalMiddleware

func (r *Route) GlobalMiddleware(middleware ...contractshttp.Middleware)

func (*Route) Info

func (r *Route) Info(name string) contractshttp.Info

func (*Route) Listen

func (r *Route) Listen(l net.Listener) error

func (*Route) ListenTLS

func (r *Route) ListenTLS(l net.Listener) error

func (*Route) ListenTLSWithCert

func (r *Route) ListenTLSWithCert(l net.Listener, certFile, keyFile string) error

func (*Route) Recover

func (r *Route) Recover(callback func(ctx contractshttp.Context, err any))

func (*Route) Run

func (r *Route) Run(host ...string) error

func (*Route) RunTLS

func (r *Route) RunTLS(host ...string) error

func (*Route) RunTLSWithCert

func (r *Route) RunTLSWithCert(host, certFile, keyFile string) error

func (*Route) ServeHTTP

func (r *Route) ServeHTTP(writer http.ResponseWriter, request *http.Request)

func (*Route) SetGlobalMiddleware

func (r *Route) SetGlobalMiddleware(middlewares []contractshttp.Middleware)

func (*Route) Shutdown

func (r *Route) Shutdown(ctx ...context.Context) error

func (*Route) Test

func (r *Route) Test(request *http.Request) (*http.Response, error)

type ServiceProvider

type ServiceProvider struct{}

func (*ServiceProvider) Boot

func (r *ServiceProvider) Boot(app foundation.Application)

func (*ServiceProvider) Register

func (r *ServiceProvider) Register(app foundation.Application)

func (*ServiceProvider) Relationship

func (r *ServiceProvider) Relationship() binding.Relationship

type Status

type Status struct {
	// contains filtered or unexported fields
}

func NewStatus

func NewStatus(instance *gin.Context, code int) *Status

func (*Status) Data

func (r *Status) Data(contentType string, data []byte) contractshttp.AbortableResponse

func (*Status) Json

func (*Status) Stream

func (r *Status) Stream(step func(w contractshttp.StreamWriter) error) contractshttp.Response

func (*Status) String

func (r *Status) String(format string, values ...any) contractshttp.AbortableResponse

type StreamResponse

type StreamResponse struct {
	// contains filtered or unexported fields
}

func (*StreamResponse) Render

func (r *StreamResponse) Render() error

type StreamWriter

type StreamWriter struct {
	// contains filtered or unexported fields
}

func NewStreamWriter

func NewStreamWriter(instance *gin.Context) *StreamWriter

func (*StreamWriter) Flush

func (w *StreamWriter) Flush() error

func (*StreamWriter) Write

func (w *StreamWriter) Write(data []byte) (int, error)

func (*StreamWriter) WriteString

func (w *StreamWriter) WriteString(s string) (int, error)

type StringResponse

type StringResponse struct {
	// contains filtered or unexported fields
}

func (*StringResponse) Abort

func (r *StringResponse) Abort() error

func (*StringResponse) Render

func (r *StringResponse) Render() error

type TimeoutMiddleware

type TimeoutMiddleware struct {
	Duration time.Duration
	// contains filtered or unexported fields
}

func (*TimeoutMiddleware) Handle

func (t *TimeoutMiddleware) Handle(ctx contractshttp.Context)

func (*TimeoutMiddleware) Signature

func (t *TimeoutMiddleware) Signature() string

type TlsMiddleware

type TlsMiddleware struct{}

func (*TlsMiddleware) Handle

func (t *TlsMiddleware) Handle(ctx contractshttp.Context)

func (*TlsMiddleware) Signature

func (t *TlsMiddleware) Signature() string

type View

type View struct {
	// contains filtered or unexported fields
}

func NewView

func NewView(instance *gin.Context) *View

func (*View) First

func (receive *View) First(views []string, data ...any) contractshttp.Response

func (*View) Make

func (receive *View) Make(view string, data ...any) contractshttp.Response

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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