gout

package module
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: Feb 16, 2026 License: GPL-3.0 Imports: 16 Imported by: 0

README

Gout

This is a go web framework, like Gin.

Documentation

Overview

Package gout is a go web framework

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BindJSON added in v0.2.0

func BindJSON[T any](c *Context) (T, error)

BindJSON parses the request body as JSON and binds it to a struct.

func Success added in v0.2.0

func Success[T any](c *Context, data T)

Success writes a successful JSON response with a standard structure.

Types

type Context

type Context struct {
	// origin objects
	Writer http.ResponseWriter
	Req    *http.Request
	// request info
	Path   string
	Method string
	Params map[string]string
	// response info
	StatusCode int
	// contains filtered or unexported fields
}

Context holds the HTTP request and response objects, as well as URL path, method middleware handlers.

func (*Context) ClientIP added in v0.2.1

func (c *Context) ClientIP() string

ClientIP returns the client's IP address.

func (*Context) Data

func (c *Context) Data(code int, data []byte)

Data writes a raw byte slice response with the specified status code.

func (*Context) Fail

func (c *Context) Fail(code int, err string)

func (*Context) File added in v0.2.1

func (c *Context) File(filepath string)

File writes the specified file into the body stream in an efficient way.

func (*Context) HTML

func (c *Context) HTML(code int, name string, data any)

HTML renders a HTML template with the specified status code and data.

func (*Context) JSON

func (c *Context) JSON(code int, obj any)

JSON writes a JSON response with the specified status code.

func (*Context) Next

func (c *Context) Next()

Next executes the remaining handlers in the chain.

func (*Context) Param

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

Param returns the value of the path parameter specified by the key.

func (*Context) PostFrom

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

PostFrom returns the value of the form parameter specified by the key.

func (*Context) Query

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

Query returns the value of the query parameter specified by the key.

func (*Context) SetCookie added in v0.2.1

func (c *Context) SetCookie(
	name, value string,
	maxAge int,
	path, domain string,
	secure, httpOnly bool,
)

SetCookie adds a Set-Cookie header to the ResponseWriter's headers.

func (*Context) SetHeader

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

SetHeader sets the value of the response header.

func (*Context) Status

func (c *Context) Status(code int)

Status sets the HTTP response status code.

func (*Context) String

func (c *Context) String(code int, format string, values ...any)

String writes a string response with specified status code and format.

type DataResp added in v0.2.0

type DataResp[T any] struct {
	Code int    `json:"code"`
	Msg  string `json:"msg"`
	Data T      `json:"data"`
}

DataResp defines a standard response struct

type Engine

type Engine struct {
	*RouterGroup
	// contains filtered or unexported fields
}

Engine implement the interface of ServeHTTP

func Default

func Default() *Engine

Default is base on New with Logger middleware

func New

func New() *Engine

New is the constructor of gout.Engine

func (*Engine) LoadHTMLGlob

func (engine *Engine) LoadHTMLGlob(pattern string)

LoadHTMLGlob loads HTML templates matching the specified pattern.

func (*Engine) Run

func (engine *Engine) Run(addr string) (err error)

Run defines the method to add POST request

func (*Engine) RunGracefully added in v0.2.1

func (engine *Engine) RunGracefully(addr string) error

RunGracefully starts the HTTP server and handlers graceful shutdown on interrupt signals.

func (*Engine) ServeHTTP

func (engine *Engine) ServeHTTP(w http.ResponseWriter, req *http.Request)

ServeHTTP conforms to the http.Handler interface.

func (*Engine) SetFuncMap

func (engine *Engine) SetFuncMap(funcMap template.FuncMap)

SetFuncMap sets a custom template.FuncMap for the Engine.

type FormatterType added in v0.2.1

type FormatterType int

FormatterType defines the type of log formatting.

const (
	TextFormatter FormatterType = iota
	JSONFormatter
)

type H

type H map[string]any

H is a shortcut for map[string]any

type HandlerFunc

type HandlerFunc func(c *Context)

HandlerFunc defines the request handler used by gout

func CORS added in v0.2.1

func CORS() HandlerFunc

CORS returns a middleware handler that enables Cross-Origin Resources Sharing.

func Logger

func Logger(configs ...LoggerConfig) HandlerFunc

Logger returns a middleware handler that logs HTTP requests.

func Recovery

func Recovery() HandlerFunc

Recovery returns a middleware handler that recovers from any panics and writes a 500 error.

type LoggerConfig added in v0.2.1

type LoggerConfig struct {
	Formatter FormatterType
}

LoggerConfig defines the configuration for the Logger middleware.

type RouterGroup

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

RouterGroup implement the group routes

func (*RouterGroup) DELETE added in v0.2.1

func (group *RouterGroup) DELETE(pattern string, handler HandlerFunc)

DELETE defines the method to add DELETE request

func (*RouterGroup) GET

func (group *RouterGroup) GET(pattern string, handler HandlerFunc)

GET define the method to add GET request

func (*RouterGroup) Group

func (group *RouterGroup) Group(prefix string) *RouterGroup

Group is defined to create a new RouterGroup remember all groups share the same Engine instance

func (*RouterGroup) HEAD added in v0.2.1

func (group *RouterGroup) HEAD(pattern string, handler HandlerFunc)

HEAD defines the method to add HEAD request

func (*RouterGroup) OPTIONS added in v0.2.1

func (group *RouterGroup) OPTIONS(pattern string, handler HandlerFunc)

OPTIONS defines the method to add OPTIONS request

func (*RouterGroup) PATCH added in v0.2.1

func (group *RouterGroup) PATCH(pattern string, handler HandlerFunc)

PATCH defines the method to add PATCH request

func (*RouterGroup) POST

func (group *RouterGroup) POST(pattern string, handler HandlerFunc)

POST defines the method to add POST request

func (*RouterGroup) PUT added in v0.2.1

func (group *RouterGroup) PUT(pattern string, handler HandlerFunc)

PUT defines the method to add PUT request

func (*RouterGroup) Static

func (group *RouterGroup) Static(relativePath string, root string)

Static regisiters a route to serve static files from the specified root directory.

func (*RouterGroup) Use

func (group *RouterGroup) Use(middleware ...HandlerFunc)

Use is defined to add middleware to the group

Jump to

Keyboard shortcuts

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