binigo

package
v1.8.0 Latest Latest
Warning

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

Go to latest
Published: Oct 1, 2025 License: MIT Imports: 18 Imported by: 0

README

Framework Core Code

Place these artifacts here:

  • application.go (Application Core)
  • router.go (Router Implementation)
  • context.go (Context - Request/Response)
  • container.go (Service Container)
  • middleware.go (Built-in Middleware)
  • database.go (Database Layer)
  • validation.go (Validation System)

Each file should be at: pkg/filename.go

Documentation

Index

Constants

View Source
const (
	ColorReset  = "\033[0m"
	ColorRed    = "\033[31m"
	ColorGreen  = "\033[32m"
	ColorYellow = "\033[33m"
	ColorBlue   = "\033[34m"
	ColorCyan   = "\033[36m"
	ColorWhite  = "\033[37m"
	ColorGray   = "\033[90m"
)

ANSI color codes

Variables

View Source
var Version = "dev"

Version of the framework (set by build flags)

Functions

This section is empty.

Types

type Application

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

Application is the main framework instance

func Bootstrap

func Bootstrap() *Application

Helper to create and configure a new application

func NewApplication

func NewApplication(config *Config) *Application

NewApplication creates a new framework instance

func (*Application) Any

func (a *Application) Any(path string, handler HandlerFunc) []*Route

Any registers a route for all HTTP methods

func (*Application) Container

func (a *Application) Container() *Container

Container returns the service container

func (*Application) Delete

func (a *Application) Delete(path string, handler HandlerFunc) *Route

func (*Application) Get

func (a *Application) Get(path string, handler HandlerFunc) *Route

Route registration helpers

func (*Application) Group

func (a *Application) Group(prefix string, fn func(r *Router)) *Router

Group creates a route group with shared middleware/prefix

func (*Application) Options

func (a *Application) Options(path string, handler HandlerFunc) *Route

func (*Application) Patch

func (a *Application) Patch(path string, handler HandlerFunc) *Route

func (*Application) Post

func (a *Application) Post(path string, handler HandlerFunc) *Route

func (*Application) Put

func (a *Application) Put(path string, handler HandlerFunc) *Route

func (*Application) Router

func (a *Application) Router() *Router

Router returns the router instance

func (*Application) Run

func (a *Application) Run(addr string) error

Run starts the HTTP server

func (*Application) Use

func (a *Application) Use(middleware ...MiddlewareFunc)

Use adds global middleware

type Config

type Config struct {
	AppName     string
	Environment string
	Debug       bool
	Port        string
	Database    DatabaseConfig
	DatabaseURL string
}

Config structure

func LoadConfig

func LoadConfig() *Config

LoadConfig loads configuration from environment

type Container

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

Container is the dependency injection container

func NewContainer

func NewContainer() *Container

NewContainer creates a new service container

func (*Container) Alias

func (c *Container) Alias(alias, abstract string)

Alias creates an alias for a binding

func (*Container) Bind

func (c *Container) Bind(abstract string, resolver func(*Container) interface{})

Bind registers a binding in the container

func (*Container) Bound

func (c *Container) Bound(abstract string) bool

Bound checks if a binding exists

func (*Container) Call

func (c *Container) Call(fn interface{}, params ...interface{}) ([]interface{}, error)

Call invokes a function with dependency injection

func (*Container) Flush

func (c *Container) Flush()

Flush clears all bindings and instances

func (*Container) Forget

func (c *Container) Forget(abstract string)

Forget removes a binding from the container

func (*Container) Instance

func (c *Container) Instance(abstract string, instance interface{})

Instance registers an existing instance as shared

func (*Container) Make

func (c *Container) Make(abstract string) (interface{}, error)

Make resolves a binding from the container

func (*Container) MustMake

func (c *Container) MustMake(abstract string) interface{}

MustMake resolves a binding or panics

func (*Container) RegisterProvider

func (c *Container) RegisterProvider(provider ServiceProvider, app *Application) error

RegisterProvider registers a service provider

func (*Container) Resolve

func (c *Container) Resolve(target interface{}) error

Resolve builds an instance of the given type with dependency injection

func (*Container) Singleton

func (c *Container) Singleton(abstract string, resolver func(*Container) interface{})

Singleton registers a singleton binding

type Context

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

Context wraps fasthttp context with helper methods

func NewContext

func NewContext(ctx *fasthttp.RequestCtx, app *Application) *Context

NewContext creates a new context instance

func (*Context) Abort

func (c *Context) Abort()

Abort stops the handler chain execution

func (*Context) AbortWithJSON

func (c *Context) AbortWithJSON(code int, data interface{}) error

AbortWithJSON stops execution and returns JSON

func (*Context) AbortWithStatus

func (c *Context) AbortWithStatus(code int)

AbortWithStatus stops execution and sets status code

func (*Context) App

func (c *Context) App() *Application

App returns the application instance

func (*Context) Bind

func (c *Context) Bind(v interface{}) error

Bind binds request body to a struct

func (*Context) Container

func (c *Context) Container() *Container

Container returns the service container

func (*Context) Cookie

func (c *Context) Cookie(name, value string, maxAge ...int) *Context

Cookie sets a cookie

func (*Context) Error

func (c *Context) Error(message string, code ...int) error

Error returns an error JSON response

func (*Context) FastHTTP

func (c *Context) FastHTTP() *fasthttp.RequestCtx

FastHTTP returns the underlying fasthttp context

func (*Context) File

func (c *Context) File(name string) (*multipart.FileHeader, error)

File gets uploaded file

func (*Context) FormValue

func (c *Context) FormValue(name string) string

FormValue gets a form value

func (*Context) Get

func (c *Context) Get(key string) interface{}

Get retrieves a value from the context

func (*Context) GetCookie

func (c *Context) GetCookie(name string) string

GetCookie gets a cookie value

func (*Context) GetString

func (c *Context) GetString(key string) string

GetString retrieves a string value from the context

func (*Context) HTML

func (c *Context) HTML(html string) error

HTML sends HTML response

func (*Context) Header

func (c *Context) Header(name string) string

Header gets a request header

func (*Context) IP

func (c *Context) IP() string

IP gets client IP address

func (*Context) Input

func (c *Context) Input(name string) interface{}

Input gets input from request body (JSON)

func (*Context) JSON

func (c *Context) JSON(data interface{}) error

JSON sends JSON response

func (*Context) Method

func (c *Context) Method() string

Method gets the HTTP method

func (*Context) MustGet

func (c *Context) MustGet(key string) interface{}

MustGet retrieves a value or panics if not found

func (*Context) Param

func (c *Context) Param(name string) string

Param gets a route parameter

func (*Context) Path

func (c *Context) Path() string

Path gets the request path

func (*Context) Query

func (c *Context) Query(name string) string

Query gets a query parameter

func (*Context) QueryDefault

func (c *Context) QueryDefault(name, defaultValue string) string

QueryDefault gets a query parameter with default value

func (*Context) Redirect

func (c *Context) Redirect(url string, statusCode ...int) error

Redirect performs HTTP redirect

func (*Context) Set

func (c *Context) Set(key string, value interface{})

Set stores a value in the context

func (*Context) SetHeader

func (c *Context) SetHeader(key, value string) *Context

SetHeader sets a response header

func (*Context) Status

func (c *Context) Status(code int) *Context

Status sets the HTTP status code

func (*Context) String

func (c *Context) String(format string, values ...interface{}) error

String sends plain text response

func (*Context) Success

func (c *Context) Success(data interface{}, message ...string) error

Success returns a success JSON response

func (*Context) Validate

func (c *Context) Validate(rules map[string]string) error

Validate validates the request using a validator

func (*Context) ValidateJSON

func (c *Context) ValidateJSON(rules map[string][]string) (*Validator, error)

Context validation helper

func (*Context) ValidateRequest

func (c *Context) ValidateRequest(rules map[string][]string) error

ValidateRequest validates request and returns errors if any

type DB

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

DB represents a database connection

func NewDB

func NewDB(config DatabaseConfig) (*DB, error)

NewDB creates a new database connection

func (*DB) Close

func (db *DB) Close() error

Close closes the database connection

func (*DB) Exec

func (db *DB) Exec(query string, args ...interface{}) (sql.Result, error)

Exec executes a query without returning rows

func (*DB) Raw

func (db *DB) Raw(query string, args ...interface{}) (*sql.Rows, error)

Raw executes a raw SQL query

func (*DB) Table

func (db *DB) Table(table string) *QueryBuilder

Table starts a new query builder for a table

func (*DB) Transaction

func (db *DB) Transaction(fn func(*sql.Tx) error) error

Transaction begins a transaction

type DatabaseConfig

type DatabaseConfig struct {
	Driver   string
	Host     string
	Port     string
	Database string
	Username string
	Password string
}

type HandlerFunc

type HandlerFunc func(*Context) error

HandlerFunc is the function signature for route handlers

type Map

type Map map[string]interface{}

Map is a shorthand for map[string]interface{}

type MiddlewareFunc

type MiddlewareFunc func(HandlerFunc) HandlerFunc

MiddlewareFunc wraps a handler with middleware functionality

func AuthMiddleware

func AuthMiddleware() MiddlewareFunc

AuthMiddleware checks for authentication

func CORSMiddleware

func CORSMiddleware(allowedOrigins ...string) MiddlewareFunc

CORSMiddleware handles CORS headers

func CompressionMiddleware

func CompressionMiddleware() MiddlewareFunc

CompressionMiddleware enables gzip compression

func GuestMiddleware

func GuestMiddleware() MiddlewareFunc

GuestMiddleware ensures user is NOT authenticated

func JSONOnlyMiddleware

func JSONOnlyMiddleware() MiddlewareFunc

JSONOnlyMiddleware ensures requests are JSON

func LoggerMiddleware

func LoggerMiddleware() MiddlewareFunc

LoggerMiddleware logs incoming requests with colors and file logging

func RateLimitMiddleware

func RateLimitMiddleware(requestsPerMinute int) MiddlewareFunc

RateLimitMiddleware implements rate limiting

func RecoveryMiddleware

func RecoveryMiddleware() MiddlewareFunc

RecoveryMiddleware recovers from panics

func RequestIDMiddleware

func RequestIDMiddleware() MiddlewareFunc

RequestIDMiddleware adds a unique request ID

func SecureHeadersMiddleware

func SecureHeadersMiddleware() MiddlewareFunc

SecureHeadersMiddleware adds security headers

func TrimTrailingSlashMiddleware

func TrimTrailingSlashMiddleware() MiddlewareFunc

TrimTrailingSlashMiddleware removes trailing slashes from URLs

type Migration added in v1.4.0

type Migration interface {
	Up(db *sql.DB) error
	Down(db *sql.DB) error
	Name() string
}

Migration interface that all migrations must implement

type Migrator added in v1.4.0

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

Migrator handles database migrations

func NewMigrator added in v1.4.0

func NewMigrator(db *sql.DB) *Migrator

NewMigrator creates a new migrator instance

func (*Migrator) Register added in v1.4.0

func (m *Migrator) Register(migration Migration)

Register adds a migration to the migrator

func (*Migrator) Reset added in v1.4.0

func (m *Migrator) Reset() error

Reset rolls back all migrations

func (*Migrator) Rollback added in v1.4.0

func (m *Migrator) Rollback() error

Rollback rolls back the last migration

func (*Migrator) Run added in v1.4.0

func (m *Migrator) Run() error

Run executes all pending migrations

func (*Migrator) Status added in v1.7.0

func (m *Migrator) Status() error

Status shows the status of all migrations

type Model

type Model struct {
	ID        int64     `db:"id" json:"id"`
	CreatedAt time.Time `db:"created_at" json:"created_at"`
	UpdatedAt time.Time `db:"updated_at" json:"updated_at"`
}

Model represents a base model with common fields

type QueryBuilder

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

QueryBuilder provides a fluent interface for building queries

func (*QueryBuilder) Count

func (qb *QueryBuilder) Count() (int64, error)

Count returns the count of rows

func (*QueryBuilder) Delete

func (qb *QueryBuilder) Delete() (int64, error)

Delete deletes records

func (*QueryBuilder) First

func (qb *QueryBuilder) First(dest interface{}) error

First executes the query and returns the first row

func (*QueryBuilder) Get

func (qb *QueryBuilder) Get(dest interface{}) error

Get executes the query and returns all rows

func (*QueryBuilder) Insert

func (qb *QueryBuilder) Insert(data Map) (int64, error)

Insert inserts a new record

func (*QueryBuilder) Join

func (qb *QueryBuilder) Join(table, condition string) *QueryBuilder

Join adds a JOIN clause

func (*QueryBuilder) LeftJoin

func (qb *QueryBuilder) LeftJoin(table, condition string) *QueryBuilder

LeftJoin adds a LEFT JOIN clause

func (*QueryBuilder) Limit

func (qb *QueryBuilder) Limit(limit int) *QueryBuilder

Limit sets the LIMIT clause

func (*QueryBuilder) Offset

func (qb *QueryBuilder) Offset(offset int) *QueryBuilder

Offset sets the OFFSET clause

func (*QueryBuilder) OrWhere

func (qb *QueryBuilder) OrWhere(column string, operator string, value interface{}) *QueryBuilder

OrWhere adds an OR WHERE clause

func (*QueryBuilder) OrderBy

func (qb *QueryBuilder) OrderBy(column string, direction ...string) *QueryBuilder

OrderBy adds ORDER BY clause

func (*QueryBuilder) Select

func (qb *QueryBuilder) Select(columns ...string) *QueryBuilder

Select specifies columns to select

func (*QueryBuilder) Update

func (qb *QueryBuilder) Update(data Map) (int64, error)

Update updates records

func (*QueryBuilder) Where

func (qb *QueryBuilder) Where(column string, operator string, value interface{}) *QueryBuilder

Where adds a WHERE clause

func (*QueryBuilder) WhereIn

func (qb *QueryBuilder) WhereIn(column string, values []interface{}) *QueryBuilder

WhereIn adds a WHERE IN clause

type Route

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

Route represents a single route definition

func (*Route) Match

func (route *Route) Match(path string) (bool, map[string]string)

Match checks if the route matches the given path

func (*Route) Middleware

func (route *Route) Middleware(middleware ...MiddlewareFunc) *Route

Middleware adds middleware to this specific route

func (*Route) Name

func (route *Route) Name(name string) *Route

Name sets the route name

type Router

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

Router manages application routes

func NewRouter

func NewRouter() *Router

NewRouter creates a new router instance

func (*Router) Add

func (r *Router) Add(method, path string, handler HandlerFunc) *Route

Add registers a new route

func (*Router) Delete

func (r *Router) Delete(path string, handler HandlerFunc) *Route

func (*Router) Get

func (r *Router) Get(path string, handler HandlerFunc) *Route

Route registration methods

func (*Router) Group

func (r *Router) Group(prefix string, fn func(router *Router)) *Router

Group creates a route group

func (*Router) Handle

func (r *Router) Handle(ctx *Context) error

Handle processes the incoming request

func (*Router) Middleware

func (r *Router) Middleware(middleware ...MiddlewareFunc) *Router

Middleware adds middleware to the router group

func (*Router) Options

func (r *Router) Options(path string, handler HandlerFunc) *Route

func (*Router) Patch

func (r *Router) Patch(path string, handler HandlerFunc) *Route

func (*Router) Post

func (r *Router) Post(path string, handler HandlerFunc) *Route

func (*Router) Put

func (r *Router) Put(path string, handler HandlerFunc) *Route

func (*Router) Resource

func (r *Router) Resource(name string, controller interface{})

Resource creates RESTful routes for a resource

type ServiceProvider

type ServiceProvider interface {
	Register(*Container)
	Boot(*Application) error
}

ServiceProvider interface for registering services

type Validator

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

Validator handles request validation

func NewValidator

func NewValidator(data map[string]interface{}, rules map[string][]string) *Validator

NewValidator creates a new validator instance

func (*Validator) Errors

func (v *Validator) Errors() map[string][]string

Errors returns validation errors

func (*Validator) FirstError

func (v *Validator) FirstError() string

FirstError returns the first error message

func (*Validator) HasErrors

func (v *Validator) HasErrors() bool

HasErrors checks if there are any validation errors

func (*Validator) Validate

func (v *Validator) Validate() bool

Validate runs validation and returns whether it passes

Jump to

Keyboard shortcuts

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