Ginject
API Reference

API Overview

Complete reference for all Ginject public types, interfaces, and functions organized by package.

API Reference

Package core

App

type App struct {
    Logger common.Logger
    // unexported fields
}
 
func New() *App
func (a *App) Create(module func() *Module) *App
func (a *App) Listen(port int) error
func (a *App) SetGlobalPrefix(prefix string)
func (a *App) EnableVersioning(v versioning.VersioningType)
func (a *App) UseMiddleware(fns ...common.MiddlewareFn)
func (a *App) UseGuard(guarders ...common.Guarder)
func (a *App) UseInterceptor(interceptors ...common.Interceptable)
func (a *App) UseExceptionFilter(filters ...common.ExceptionFilterable)

Module

type Module struct {
    IsGlobal             bool
    OnInit               func()
    RESTExceptionFilters []common.RESTLayer
    RESTMiddlewares      []common.RESTLayer
    RESTGuards           []common.RESTLayer
    RESTInterceptors     []common.RESTLayer
}

ModuleBuilder

func ModuleBuilder() *moduleBuilder
 
func (m *moduleBuilder) Imports(modules ...any) *moduleBuilder
func (m *moduleBuilder) Controllers(cs ...Controller) *moduleBuilder
func (m *moduleBuilder) Providers(ps ...Provider) *moduleBuilder
func (m *moduleBuilder) Build() *Module

Provider Interface

type Provider interface {
    NewProvider() Provider
}

Controller Interface

type Controller interface {
    NewController() Controller
}

Package ctx

ExecutionContext

type ExecutionContext struct {
    context.Context
}
 
func NewHTTPExecutionContext(parent context.Context, timeout time.Duration) (*ExecutionContext, context.CancelFunc)
func NewWSExecutionContext() (*ExecutionContext, context.CancelFunc)
func WithTimeout(parent *ExecutionContext, d time.Duration) (*ExecutionContext, context.CancelFunc)
 
func (e *ExecutionContext) Protocol() Protocol
func (e *ExecutionContext) Set(key metaKey, val any) *ExecutionContext
func (e *ExecutionContext) Get(key metaKey) (any, bool)
func (e *ExecutionContext) MustGet(key metaKey) any

Well-Known Metadata Keys

const (
    MetaRequestID metaKey = "requestID"
    MetaTraceID   metaKey = "traceID"
    MetaUser      metaKey = "user"
)

Protocol

type Protocol string
 
const (
    ProtoHTTP Protocol = "http"
    ProtoWS   Protocol = "ws"
    ProtoRPC  Protocol = "rpc"
    ProtoGQL  Protocol = "gql"
)

WSContext

type WSContext struct {
    Conn    *websocket.Conn
    ConnID  string
    Message WSMessage
}
 
func NewWSContext(conn *websocket.Conn) (*WSContext, context.CancelFunc)
func (ws *WSContext) Exec() *ExecutionContext
func (ws *WSContext) Cancel()
func (ws *WSContext) NewMessageContext(d time.Duration) (*ExecutionContext, context.CancelFunc)

WSMessage

type WSMessage struct {
    Event   string    `json:"event"`
    Payload WSPayload `json:"payload"`
}

GoSafe

type ErrHandler func(err error, stack []byte)
 
func GoSafe(ctx context.Context, fn func(context.Context), errFn ErrHandler)

Request Parameter Types

TypeDescription
ctx.BodyJSON request body — use .Bind(&dto)
ctx.QueryURL query parameters — use .Get("key")
ctx.ParamPath parameters — use .Get("id")
ctx.HeaderRequest headers — use .Get("X-Key")
ctx.FormMultipart / urlencoded form — use .Get("field")
ctx.FileUploaded file — use .Get("field")
ctx.WSPayloadWebSocket event payload — use .Get("field")
ctx.NextCall next middleware — func()
ctx.RedirectRedirect helper — func(url string)

Package common

MiddlewareFn

type MiddlewareFn func(c *ctx.Context, next ctx.Next)

Guarder

type Guarder interface {
    CanActivate(c *ctx.Context) bool
}

Interceptable

type Interceptable interface {
    Intercept(c *ctx.Context, agg *aggregation.Aggregation) any
}

ExceptionFilterable

type ExceptionFilterable interface {
    Catch(e *exception.Exception, c *ctx.Context)
}

REST (controller embed)

type REST struct {
    Middleware
    Guard
    Interceptor
    ExceptionFilter
}
 
func (r *REST) Prefix(prefix string)
func (r *REST) BindMiddleware(fn MiddlewareFn, handlers ...any)
func (r *REST) BindGuard(guarder Guarder, handlers ...any)
func (r *REST) BindInterceptor(interceptor Interceptable, handlers ...any)
func (r *REST) BindExceptionFilter(filter ExceptionFilterable, handlers ...any)

Package aggregation

type AggregationOperator = func(*ctx.Context, any) any
 
func (a *Aggregation) Pipe(operators ...AggregationOperator) any
 
func Transform(fn func(*ctx.Context, any) any) AggregationOperator
func Tap(fn func(*ctx.Context, any)) AggregationOperator
func Timeout(d time.Duration) AggregationOperator
func Error(fn func(*ctx.Context, error) any) AggregationOperator

Package exception

type Exception struct {
    Status  int
    Message string
}
 
func BadRequestException(msg string) *Exception            // 400
func UnauthorizedException(msg string) *Exception          // 401
func ForbiddenException(msg string) *Exception             // 403
func NotFoundException(msg string) *Exception              // 404
func ConflictException(msg string) *Exception              // 409
func UnprocessableEntityException(msg string) *Exception   // 422
func TooManyRequestsException(msg string) *Exception       // 429
func InternalServerErrorException(msg string) *Exception   // 500

Package guards

type Strategy int
 
const (
    FixedWindow   Strategy = iota
    SlidingWindow Strategy = iota
    TokenBucket   Strategy = iota
)
 
type ThrottlerOptions struct {
    Limit    int64
    TTL      time.Duration
    Strategy Strategy
    KeyFunc  func(*ctx.Context) string
    Store    cache.Cache
}
 
func NewThrottler(opts ThrottlerOptions) ThrottlerGuard

Package modules/cache

type Cache interface {
    Get(ctx context.Context, key string) ([]byte, bool)
    Set(ctx context.Context, key string, val []byte, ttl time.Duration) error
    SetNX(ctx context.Context, key string, val []byte, ttl time.Duration) (bool, error)
    Delete(ctx context.Context, key string) error
    Keys(ctx context.Context) []string
    TTL(ctx context.Context, key string) (time.Duration, bool)
}
 
type CacheService struct {
    Backend Cache
}
 
func NewMemoryCache() Cache

Package middleware

func CORS(opts CORSOptions) common.MiddlewareFn
func Helmet() common.MiddlewareFn
func RequestLogger() common.MiddlewareFn
func CSRF(opts CSRFOptions) common.MiddlewareFn

Package versioning

type VersioningType int
 
const (
    URL    VersioningType = iota
    Header VersioningType = iota
    Query  VersioningType = iota
)