gin

package module
v1.18.1 Latest Latest
Warning

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

Go to latest
Published: Nov 12, 2026 License: MIT Imports: 45 Imported by: 9 modules, 9 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

Gin

Doc Go Release Test Report Card Codecov License

Gin http driver for Goravel.

Install

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

./artisan package:install github.com/goravel/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/goravel/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 added in v1.1.4

func Cors() http.Middleware

func DefaultTemplate added in v1.1.4

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

func NewAction added in v1.4.0

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

func NewContextRequest added in v1.1.4

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

func NewContextResponse added in v1.1.4

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

func NewGroup added in v1.1.0

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

func NewTemplate added in v1.1.4

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

func ResponseMiddleware added in v1.1.0

func ResponseMiddleware() contractshttp.Middleware

func Timeout added in v1.3.0

func Timeout(timeout time.Duration) contractshttp.Middleware

Timeout creates middleware to set a timeout for a request

func Tls added in v1.1.4

Types

type Action added in v1.4.0

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

func (*Action) Name added in v1.4.0

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

func (*Action) WithoutMiddleware added in v1.18.0

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 added in v1.1.0

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 added in v1.1.0

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

func NewContext added in v1.1.0

func NewContext(c *gin.Context) *Context

func (*Context) Context added in v1.1.0

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

func (*Context) Deadline added in v1.1.0

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

func (*Context) Done added in v1.1.0

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

func (*Context) Err added in v1.1.0

func (c *Context) Err() error

func (*Context) Instance added in v1.1.0

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

func (*Context) Request added in v1.1.0

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

func (*Context) Response added in v1.1.0

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

func (*Context) Value added in v1.1.0

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

func (*Context) WithContext added in v1.3.0

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

func (*Context) WithValue added in v1.1.0

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

type ContextRequest added in v1.1.4

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

func (*ContextRequest) Abort added in v1.3.0

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

func (*ContextRequest) AbortWithStatus added in v1.1.4

func (r *ContextRequest) AbortWithStatus(code int)

DEPRECATED: Use Abort instead.

func (*ContextRequest) AbortWithStatusJson added in v1.1.4

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

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

func (*ContextRequest) All added in v1.1.4

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

func (*ContextRequest) Bind added in v1.1.4

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

func (*ContextRequest) BindQuery added in v1.3.0

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

func (*ContextRequest) Cookie added in v1.2.0

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

func (*ContextRequest) File added in v1.1.4

func (*ContextRequest) Files added in v1.4.0

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

func (*ContextRequest) Form added in v1.1.4

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

func (*ContextRequest) FullUrl added in v1.1.4

func (r *ContextRequest) FullUrl() string

func (*ContextRequest) HasSession added in v1.2.0

func (r *ContextRequest) HasSession() bool

func (*ContextRequest) Header added in v1.1.4

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

func (*ContextRequest) Headers added in v1.1.4

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

func (*ContextRequest) Host added in v1.1.4

func (r *ContextRequest) Host() string

func (*ContextRequest) Info added in v1.4.0

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

func (*ContextRequest) Input added in v1.1.4

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

func (*ContextRequest) InputArray added in v1.1.4

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

func (*ContextRequest) InputBool added in v1.1.4

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

func (*ContextRequest) InputInt added in v1.1.4

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

func (*ContextRequest) InputInt64 added in v1.1.4

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

func (*ContextRequest) InputMap added in v1.1.4

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

func (*ContextRequest) InputMapArray added in v1.4.0

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

func (*ContextRequest) Ip added in v1.1.4

func (r *ContextRequest) Ip() string

func (*ContextRequest) Json added in v1.1.4

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

func (*ContextRequest) Method added in v1.1.4

func (r *ContextRequest) Method() string

func (*ContextRequest) Name added in v1.4.0

func (r *ContextRequest) Name() string

func (*ContextRequest) Next added in v1.1.4

func (r *ContextRequest) Next()

func (*ContextRequest) Origin added in v1.1.4

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

func (*ContextRequest) OriginPath added in v1.4.0

func (r *ContextRequest) OriginPath() string

func (*ContextRequest) Path added in v1.1.4

func (r *ContextRequest) Path() string

func (*ContextRequest) Queries added in v1.1.4

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

func (*ContextRequest) Query added in v1.1.4

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

func (*ContextRequest) QueryArray added in v1.1.4

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

func (*ContextRequest) QueryBool added in v1.1.4

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

func (*ContextRequest) QueryInt added in v1.1.4

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

func (*ContextRequest) QueryInt64 added in v1.1.4

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

func (*ContextRequest) QueryMap added in v1.1.4

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

func (*ContextRequest) Route added in v1.1.4

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

func (*ContextRequest) RouteInt added in v1.1.4

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

func (*ContextRequest) RouteInt64 added in v1.1.4

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

func (*ContextRequest) Session added in v1.2.0

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

func (*ContextRequest) SetSession added in v1.2.0

func (*ContextRequest) Url added in v1.1.4

func (r *ContextRequest) Url() string

func (*ContextRequest) Validate added in v1.1.4

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

func (*ContextRequest) ValidateRequest added in v1.1.4

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

type ContextResponse added in v1.1.4

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

func (*ContextResponse) Cookie added in v1.2.0

func (*ContextResponse) Data added in v1.1.4

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

func (*ContextResponse) Download added in v1.1.4

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

func (*ContextResponse) File added in v1.1.4

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

func (*ContextResponse) Flush added in v1.1.4

func (r *ContextResponse) Flush()

func (*ContextResponse) Header added in v1.1.4

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

func (*ContextResponse) Json added in v1.1.4

func (*ContextResponse) NoContent added in v1.2.0

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

func (*ContextResponse) Origin added in v1.1.4

func (*ContextResponse) Redirect added in v1.1.4

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

func (*ContextResponse) Status added in v1.1.4

func (*ContextResponse) Stream added in v1.3.0

func (*ContextResponse) String added in v1.1.4

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

func (*ContextResponse) Success added in v1.1.4

func (*ContextResponse) View added in v1.1.4

func (*ContextResponse) WithoutCookie added in v1.2.0

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

func (*ContextResponse) Writer added in v1.1.4

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

type CorsMiddleware added in v1.18.0

type CorsMiddleware struct{}

func (*CorsMiddleware) Handle added in v1.18.0

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

func (*CorsMiddleware) Signature added in v1.18.0

func (c *CorsMiddleware) Signature() string

type DataResponse added in v1.1.4

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

func (*DataResponse) Abort added in v1.3.0

func (r *DataResponse) Abort() error

func (*DataResponse) Render added in v1.1.4

func (r *DataResponse) Render() error

type Delims added in v1.1.4

type Delims struct {
	Left  string
	Right string
}

type DownloadResponse added in v1.1.4

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

func (*DownloadResponse) Render added in v1.1.4

func (r *DownloadResponse) Render() error

type FileResponse added in v1.1.4

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

func (*FileResponse) Render added in v1.1.4

func (r *FileResponse) Render() error

type Group added in v1.1.0

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

func (*Group) Any added in v1.1.0

func (*Group) Delete added in v1.1.0

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

func (*Group) Get added in v1.1.0

func (*Group) Group added in v1.1.0

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

func (*Group) Middleware added in v1.1.0

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

func (*Group) Options added in v1.1.0

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

func (*Group) Patch added in v1.1.0

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

func (*Group) Post added in v1.1.0

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

func (*Group) Prefix added in v1.1.0

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

func (*Group) Put added in v1.1.0

func (*Group) Resource added in v1.1.0

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

func (*Group) Static added in v1.1.0

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

func (*Group) StaticFS added in v1.1.0

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

func (*Group) StaticFile added in v1.1.0

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

func (*Group) WithMiddlewares added in v1.4.0

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

func (*Group) WithoutMiddleware added in v1.18.0

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

type HtmlResponse added in v1.1.4

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

func (*HtmlResponse) Render added in v1.1.4

func (r *HtmlResponse) Render() error

type JsonResponse added in v1.1.4

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

func (*JsonResponse) Abort added in v1.3.0

func (r *JsonResponse) Abort() error

func (*JsonResponse) Render added in v1.1.4

func (r *JsonResponse) Render() error

type NoContentResponse added in v1.2.0

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

func (*NoContentResponse) Abort added in v1.3.0

func (r *NoContentResponse) Abort() error

func (*NoContentResponse) Render added in v1.2.0

func (r *NoContentResponse) Render() error

type RedirectResponse added in v1.1.4

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

func (*RedirectResponse) Abort added in v1.3.0

func (r *RedirectResponse) Abort() error

func (*RedirectResponse) Render added in v1.1.4

func (r *RedirectResponse) Render() error

type RenderOptions added in v1.1.4

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

type Route added in v1.1.0

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

func NewRoute added in v1.1.0

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

func (*Route) Fallback added in v1.1.0

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

func (*Route) GetGlobalMiddleware added in v1.17.0

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

func (*Route) GetRoutes added in v1.4.0

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

func (*Route) GlobalMiddleware added in v1.1.0

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

func (*Route) Info added in v1.4.0

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

func (*Route) Listen added in v1.3.0

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

func (*Route) ListenTLS added in v1.3.0

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

func (*Route) ListenTLSWithCert added in v1.3.0

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

func (*Route) Recover added in v1.3.0

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

func (*Route) Run added in v1.1.0

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

func (*Route) RunTLS added in v1.1.0

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

func (*Route) RunTLSWithCert added in v1.1.0

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

func (*Route) ServeHTTP added in v1.1.0

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

func (*Route) SetGlobalMiddleware added in v1.17.0

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

func (*Route) Shutdown added in v1.2.4

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

func (*Route) Test added in v1.3.0

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 added in v1.4.0

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

type Status added in v1.1.0

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

func NewStatus added in v1.1.4

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

func (*Status) Data added in v1.1.0

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

func (*Status) Json added in v1.1.0

func (*Status) Stream added in v1.3.0

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

func (*Status) String added in v1.1.0

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

type StreamResponse added in v1.3.0

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

func (*StreamResponse) Render added in v1.3.0

func (r *StreamResponse) Render() error

type StreamWriter added in v1.3.0

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

func NewStreamWriter added in v1.3.0

func NewStreamWriter(instance *gin.Context) *StreamWriter

func (*StreamWriter) Flush added in v1.3.0

func (w *StreamWriter) Flush() error

func (*StreamWriter) Write added in v1.3.0

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

func (*StreamWriter) WriteString added in v1.3.0

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

type StringResponse added in v1.1.4

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

func (*StringResponse) Abort added in v1.3.0

func (r *StringResponse) Abort() error

func (*StringResponse) Render added in v1.1.4

func (r *StringResponse) Render() error

type TimeoutMiddleware added in v1.18.0

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

func (*TimeoutMiddleware) Handle added in v1.18.0

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

func (*TimeoutMiddleware) Signature added in v1.18.0

func (t *TimeoutMiddleware) Signature() string

type TlsMiddleware added in v1.18.0

type TlsMiddleware struct{}

func (*TlsMiddleware) Handle added in v1.18.0

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

func (*TlsMiddleware) Signature added in v1.18.0

func (t *TlsMiddleware) Signature() string

type View added in v1.1.4

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

func NewView added in v1.1.4

func NewView(instance *gin.Context) *View

func (*View) First added in v1.1.4

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

func (*View) Make added in v1.1.4

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
⌂ 换网址