goweb

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: May 15, 2025 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type APIError

type APIError struct {
	Code    int    `json:"code"`
	Message string `json:"message"`
}

APIError represents an API error

type App

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

App is the main web application structure

func New

func New() *App

New creates a new App instance with default configuration

func NewWithConfig

func NewWithConfig(config AppConfig) *App

NewWithConfig creates a new App with custom configuration

func (*App) Add

func (a *App) Add(path string, setup func(*Group))

Add registers routes for a group

func (*App) Group

func (a *App) Group(path string) *Group

Group creates a new route group

func (*App) Routes

func (a *App) Routes() []string

Routes returns all registered routes for debugging

func (*App) Run

func (a *App) Run(address string) error

Run starts the HTTP server

func (*App) RunWithConfig

func (a *App) RunWithConfig(address string, config AppConfig) error

RunWithConfig starts the HTTP server with the given configuration

func (*App) ServeHTTP added in v0.0.2

func (a *App) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP allows the App to implement the http.Handler interface

func (*App) SetErrorHandler

func (a *App) SetErrorHandler(handler ErrorHandler)

SetErrorHandler sets a custom error handler

func (*App) SetLogger

func (a *App) SetLogger(logger Logger)

SetLogger sets a custom logger

func (*App) Static

func (a *App) Static(path, dir string)

Static serves static files from the given directory

func (*App) Use

func (a *App) Use(middleware ...Middleware)

Use adds middleware to the application

func (*App) UseFunc

func (a *App) UseFunc(handler func(next http.Handler) http.Handler)

UseFunc adds handler function as middleware

type AppConfig

type AppConfig struct {
	CorsOptions  cors.Options
	ReadTimeout  time.Duration
	WriteTimeout time.Duration
	Logger       Logger
	ErrorHandler ErrorHandler
}

AppConfig holds configuration for the App

func DefaultAppConfig

func DefaultAppConfig() AppConfig

DefaultAppConfig returns a default configuration

type Context

type Context struct {
	Writer  http.ResponseWriter
	Request *http.Request
	Params  map[string]string
	// contains filtered or unexported fields
}

Context represents the request context

func (*Context) Abort

func (c *Context) Abort()

Abort stops the chain execution

func (*Context) Bind

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

Bind binds request body to a struct

func (*Context) BindAndValidate

func (c *Context) BindAndValidate(obj interface{}) error

BindAndValidate binds and validates request body

func (*Context) Cookie

func (c *Context) Cookie(name string) (*http.Cookie, error)

Cookie gets a cookie by name

func (*Context) Error

func (c *Context) Error(status int, message string, args ...interface{})

Error sends an error JSON response

func (*Context) File

func (c *Context) File(filepath string)

File sends a file as the response

func (*Context) Get

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

Get retrieves a value from the context

func (*Context) GetParam

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

GetParam gets a URL parameter by name

func (*Context) HTML

func (c *Context) HTML(status int, html string)

HTML sends an HTML response

func (*Context) HandleError

func (c *Context) HandleError(err error)

HandleError handles an error with the registered error handler

func (*Context) IsAborted

func (c *Context) IsAborted() bool

IsAborted returns whether the context was aborted

func (*Context) JSON

func (c *Context) JSON(status int, data interface{})

JSON sends a JSON response

func (*Context) LogRequest

func (c *Context) LogRequest()

LogRequest logs information about the current request

func (*Context) Logger

func (c *Context) Logger() Logger

Logger returns the application logger

func (*Context) MustGet

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

MustGet retrieves a value from the context and panics if not found

func (*Context) Query

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

Query gets a query parameter by name

func (*Context) QueryDefault

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

QueryDefault gets a query parameter by name with a default value

func (*Context) Redirect

func (c *Context) Redirect(status int, location string)

Redirect performs an HTTP redirect

func (*Context) Set

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

Set stores a value in the context

func (*Context) SetCookie

func (c *Context) SetCookie(cookie *http.Cookie)

SetCookie sets a cookie

func (*Context) String

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

String sends a string response

func (*Context) Success

func (c *Context) Success(data interface{})

Success sends a successful JSON response

func (*Context) WithMeta

func (c *Context) WithMeta(data interface{}, meta interface{})

WithMeta adds metadata to a success response

type ErrorHandler

type ErrorHandler interface {
	Handle(ctx *Context, err error)
}

ErrorHandler is responsible for handling errors

type Group

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

Group represents a group of routes

func (*Group) DELETE

func (g *Group) DELETE(path string, action HandlerFunc)

DELETE registers a route with DELETE method

func (*Group) GET

func (g *Group) GET(path string, action HandlerFunc)

GET registers a route with GET method

func (*Group) OPTIONS

func (g *Group) OPTIONS(path string, action HandlerFunc)

OPTIONS registers a route with OPTIONS method

func (*Group) PATCH

func (g *Group) PATCH(path string, action HandlerFunc)

PATCH registers a route with PATCH method

func (*Group) POST

func (g *Group) POST(path string, action HandlerFunc)

POST registers a route with POST method

func (*Group) PUT

func (g *Group) PUT(path string, action HandlerFunc)

PUT registers a route with PUT method

func (*Group) Route

func (g *Group) Route(path string, action HandlerFunc, methods ...string)

Route registers a route with the given HTTP methods

func (*Group) Use

func (g *Group) Use(middleware ...Middleware)

Use adds middleware to the group

type HandlerFunc

type HandlerFunc func(*Context)

HandlerFunc is a function that processes a request with a context

type Logger

type Logger interface {
	Info(format string, args ...interface{})
	Error(format string, args ...interface{})
	Debug(format string, args ...interface{})
}

Logger interface for custom logging

type Middleware

type Middleware func(HandlerFunc) HandlerFunc

Middleware is a function that processes requests before they reach the handler

func LoggerMiddleware

func LoggerMiddleware() Middleware

LoggerMiddleware logs all requests

func RecoverMiddleware

func RecoverMiddleware() Middleware

RecoverMiddleware recovers from panics

func TimeoutMiddleware

func TimeoutMiddleware(timeout time.Duration) Middleware

TimeoutMiddleware adds a timeout to the request

type Response

type Response struct {
	Success bool        `json:"success"`
	Data    interface{} `json:"data,omitempty"`
	Error   *APIError   `json:"error,omitempty"`
	Meta    interface{} `json:"meta,omitempty"`
}

Response is a standardized API response

Jump to

Keyboard shortcuts

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