Documentation ¶
Index ¶
- Constants
- Variables
- func Bool2Str(b bool) string
- func Float2Int(num float64) int
- func Float2Str(num float64) string
- func GetParams(req *http.Request) map[string]string
- func Int2Float(num int) float64
- func Int2Str(num int) string
- func MimeText(ext string) string
- func NewRouter() *router
- func RegisterMiddleware(name string, middleware func(http.Handler) http.Handler)
- func RegisterServer(config ServerConfig)
- func Rune2Str(r rune) string
- func StartServer(r ...*router)
- func Str2Bool(str string) (bool, error)
- func Str2Float(str string) (float64, error)
- func Str2Int(str string) (int, error)
- func Str2Int64(str string) (int64, error)
- func ToBool(src interface{}) (interface{}, error)
- func ToFloat(value interface{}) (float64, error)
- func ToInt(value interface{}) (int, error)
- func ToInt64(v interface{}) (interface{}, error)
- func ToString(value interface{}) string
- type CORSConfig
- type CompressionConfig
- type Config
- type DatabaseConfig
- type ErrorCode
- type FileHandler
- type FormDataHandler
- type HttpContext
- func (ctx *HttpContext) AddCookie(cookie *http.Cookie)
- func (ctx *HttpContext) Body() io.ReadCloser
- func (ctx *HttpContext) CloseNotify() <-chan bool
- func (ctx *HttpContext) ContentLength() int64
- func (ctx *HttpContext) Flush()
- func (ctx *HttpContext) FormValue(key string) string
- func (ctx *HttpContext) Header() http.Header
- func (ctx *HttpContext) Hijack() (net.Conn, *bufio.ReadWriter, error)
- func (ctx *HttpContext) Host() string
- func (ctx *HttpContext) Method() string
- func (ctx *HttpContext) ParmBool(key string) (bool, error)
- func (ctx *HttpContext) ParmDataTime(key string) (time.Time, error)
- func (ctx *HttpContext) ParmDate(key string) (time.Time, error)
- func (ctx *HttpContext) ParmFloat(key string) (float64, error)
- func (ctx *HttpContext) ParmInt(key string) (int64, error)
- func (ctx *HttpContext) ParmInt_(key string) int64
- func (ctx *HttpContext) ParmMonth(key string) (time.Time, error)
- func (ctx *HttpContext) ParmStr(key string) string
- func (ctx *HttpContext) ParmStrings(key string) []string
- func (ctx *HttpContext) ParmTime(key string) (time.Time, error)
- func (ctx *HttpContext) ParmTimeParse(key string, format string) (time.Time, error)
- func (ctx *HttpContext) ParmYear(key string) (time.Time, error)
- func (ctx *HttpContext) ParseJSONBody(target interface{}) error
- func (ctx *HttpContext) PostFormValue(key string) string
- func (ctx *HttpContext) Pusher() http.Pusher
- func (ctx *HttpContext) RemoteAddr() string
- func (ctx *HttpContext) ReqHeader() http.Header
- func (ctx *HttpContext) SetCookie(cookie *http.Cookie)
- func (ctx *HttpContext) URL() *url.URL
- func (ctx *HttpContext) UserAgent() string
- func (ctx *HttpContext) Write(data []byte) (int, error)
- func (ctx *HttpContext) WriteByte(data []byte)
- func (ctx *HttpContext) WriteErrorJSON(statusCode interface{}, errMsg interface{})
- func (ctx *HttpContext) WriteErrorXML(statusCode int, errMsg interface{})
- func (ctx *HttpContext) WriteHeader(statusCode int)
- func (ctx *HttpContext) WriteString(s string)
- func (ctx *HttpContext) WriteSuccessJSON(data interface{})
- func (ctx *HttpContext) WriteSuccessXML(data interface{})
- type KeepAliveConfig
- type LoggingConfig
- type MongoDBConfig
- type MySQLConfig
- type NodeType
- type OracleConfig
- type PostgreSQLConfig
- type RateLimitConfig
- type RedisConfig
- type ResponseResult
- type SQLiteConfig
- type SecurityConfig
- type ServerConfig
- type StaticFilesConfig
- type TLSConfig
- type TimeoutsConfig
- type TrieNode
Constants ¶
const MaxMultipartBytes = 32 << 20 // 32 MB
const ParamsKey = contextKey("params")
ParamsKey is the context key for route parameters.
Variables ¶
var ErrorCodeToString = map[ErrorCode]string{ AuthError: "AuthError", ParamError: "ParamError", BizError: "BizError", NetError: "NetError", DBError: "DBError", IOError: "IOError", OtherError: "OtherError", }
ErrorCodeToString maps ErrorCode values to their corresponding strings.
var Router = NewRouter()
var String stringHandler
String is a package-level variable representing a string handler.
var Time timeHandler
Time is a package-level variable representing a time handler.
Functions ¶
func RegisterMiddleware ¶
Middleware registration function
func RegisterServer ¶
func RegisterServer(config ServerConfig)
Server registration function with configuration synchronization
func Rune2Str ¶
Rune2Str This function converts a Unicode code point into its corresponding string representation, handling printable ASCII characters, control characters, and Unicode escape sequences.
func StartServer ¶
func StartServer(r ...*router)
func ToBool ¶
func ToBool(src interface{}) (interface{}, error)
ToBool converts an interface{} to a boolean. Bool is a ValueConverter that converts input values to bools.
The conversion rules are:
- booleans are returned unchanged
- for integer types, 1 is true 0 is false, other integers are an error
- for strings and []byte, same rules as strconv.ParseBool
- all other types are an error
Types ¶
type CORSConfig ¶
type CompressionConfig ¶
type Config ¶
type Config struct {
Servers []ServerConfig `json:"servers"`
}
type DatabaseConfig ¶
type DatabaseConfig struct { PostgreSQL PostgreSQLConfig `json:"postgresql"` Oracle OracleConfig `json:"oracle"` SQLite SQLiteConfig `json:"sqlite"` MySQL MySQLConfig `json:"mysql"` MongoDB MongoDBConfig `json:"mongodb"` Redis RedisConfig `json:"redis"` }
var (
DBConfig *DatabaseConfig
)
func InitializeDBConfig ¶
func InitializeDBConfig() (*DatabaseConfig, error)
func LoadDBConfig ¶
func LoadDBConfig(filePath string) (*DatabaseConfig, error)
LoadDBConfig loads configuration from a JSON file
type ErrorCode ¶
type ErrorCode int
ErrorCode represents custom error codes.
const ( // AuthError indicates an error related to permission. AuthError ErrorCode = iota*1000 + 1000 // ParamError indicates an error related to parameters. ParamError // BizError indicates a business logic error. BizError // NetError indicates a network-related error. NetError // DBError indicates a database-related error. DBError // IOError indicates an I/O-related error. IOError // OtherError indicates an error that does not fall into any specific category. OtherError )
type FileHandler ¶
type FileHandler func(key string, file multipart.File, fileHeader *multipart.FileHeader)
FileHandler is a callback function type for handling file data.
type FormDataHandler ¶
FormDataHandler is a callback function type for handling form data key-value pairs.
type HttpContext ¶
HttpContext represents the HTTP context.
func (*HttpContext) AddCookie ¶
func (ctx *HttpContext) AddCookie(cookie *http.Cookie)
AddCookie adds a Set-Cookie header to the provided ResponseWriter's headers.
func (*HttpContext) Body ¶
func (ctx *HttpContext) Body() io.ReadCloser
Body returns the request body.
func (*HttpContext) CloseNotify ¶
func (ctx *HttpContext) CloseNotify() <-chan bool
CloseNotify returns a channel that receives a single value when the client connection has gone away.
func (*HttpContext) ContentLength ¶
func (ctx *HttpContext) ContentLength() int64
ContentLength returns the length of the request body.
func (*HttpContext) Flush ¶
func (ctx *HttpContext) Flush()
Flush sends any buffered data to the client.
func (*HttpContext) FormValue ¶
func (ctx *HttpContext) FormValue(key string) string
FormValue returns the first value for the named component of the query.
func (*HttpContext) Header ¶
func (ctx *HttpContext) Header() http.Header
Header returns the header map that will be sent by WriteHeader.
func (*HttpContext) Hijack ¶
func (ctx *HttpContext) Hijack() (net.Conn, *bufio.ReadWriter, error)
Hijack hijacks the connection.
func (*HttpContext) Host ¶
func (ctx *HttpContext) Host() string
Host returns the host name provided by the request.
func (*HttpContext) Method ¶
func (ctx *HttpContext) Method() string
Method returns the HTTP request method.
func (*HttpContext) ParmBool ¶
func (ctx *HttpContext) ParmBool(key string) (bool, error)
ParmBool parses a boolean parameter from the request.
func (*HttpContext) ParmDataTime ¶
func (ctx *HttpContext) ParmDataTime(key string) (time.Time, error)
ParmDataTime parses a date-time parameter (yyyy-mm-dd hh:mm:ss) from the request.
func (*HttpContext) ParmDate ¶
func (ctx *HttpContext) ParmDate(key string) (time.Time, error)
ParmDate parses a date parameter (yyyy-mm-dd) from the request.
func (*HttpContext) ParmFloat ¶
func (ctx *HttpContext) ParmFloat(key string) (float64, error)
ParmFloat parses a float parameter from the request.
func (*HttpContext) ParmInt ¶
func (ctx *HttpContext) ParmInt(key string) (int64, error)
ParmInt parses an integer parameter from the request.
func (*HttpContext) ParmInt_ ¶
func (ctx *HttpContext) ParmInt_(key string) int64
ParmInt_ is an alternate method to parse an integer parameter from the request.
func (*HttpContext) ParmMonth ¶
func (ctx *HttpContext) ParmMonth(key string) (time.Time, error)
ParmMonth parses a month parameter (yyyy-mm) from the request.
func (*HttpContext) ParmStr ¶
func (ctx *HttpContext) ParmStr(key string) string
ParmStr parses a string parameter from the request.
func (*HttpContext) ParmStrings ¶
func (ctx *HttpContext) ParmStrings(key string) []string
ParmStrings retrieves multiple values for a parameter from the request.
func (*HttpContext) ParmTime ¶
func (ctx *HttpContext) ParmTime(key string) (time.Time, error)
ParmTime parses a time parameter (HH:MM) from the request.
func (*HttpContext) ParmTimeParse ¶
ParmTimeParse parses a custom time format from the request.
func (*HttpContext) ParmYear ¶
func (ctx *HttpContext) ParmYear(key string) (time.Time, error)
ParmYear parses a year parameter (yyyy) from the request.
func (*HttpContext) ParseJSONBody ¶
func (ctx *HttpContext) ParseJSONBody(target interface{}) error
ParseJSONBody reads the JSON body from the request and unmarshals it into the target object.
func (*HttpContext) PostFormValue ¶
func (ctx *HttpContext) PostFormValue(key string) string
PostFormValue returns the first value for the named component of the POST or PUT request body.
func (*HttpContext) Pusher ¶
func (ctx *HttpContext) Pusher() http.Pusher
Pusher returns the HTTP/2 Pusher for the provided ResponseWriter.
func (*HttpContext) RemoteAddr ¶
func (ctx *HttpContext) RemoteAddr() string
RemoteAddr returns the network address of the client sending the request.
func (*HttpContext) ReqHeader ¶
func (ctx *HttpContext) ReqHeader() http.Header
ReqHeader returns the request header.
func (*HttpContext) SetCookie ¶
func (ctx *HttpContext) SetCookie(cookie *http.Cookie)
SetCookie adds a Set-Cookie header to the provided ResponseWriter's headers.
func (*HttpContext) URL ¶
func (ctx *HttpContext) URL() *url.URL
URL returns the URL of the request.
func (*HttpContext) UserAgent ¶
func (ctx *HttpContext) UserAgent() string
UserAgent returns the user agent string provided in the request header.
func (*HttpContext) Write ¶
func (ctx *HttpContext) Write(data []byte) (int, error)
Write writes the data to the connection as part of an HTTP reply.
func (*HttpContext) WriteByte ¶
func (ctx *HttpContext) WriteByte(data []byte)
WriteByte writes byte to the client.
func (*HttpContext) WriteErrorJSON ¶
func (ctx *HttpContext) WriteErrorJSON(statusCode interface{}, errMsg interface{})
WriteErrorJSON writes an error message as JSON to the response with the specified status code.
func (*HttpContext) WriteErrorXML ¶
func (ctx *HttpContext) WriteErrorXML(statusCode int, errMsg interface{})
WriteErrorXML writes an error message as XML to the response with the specified status code.
func (*HttpContext) WriteHeader ¶
func (ctx *HttpContext) WriteHeader(statusCode int)
WriteHeader sends an HTTP response header with the provided status code.
func (*HttpContext) WriteString ¶
func (ctx *HttpContext) WriteString(s string)
WriteString writes a string to the response.
func (*HttpContext) WriteSuccessJSON ¶
func (ctx *HttpContext) WriteSuccessJSON(data interface{})
WriteSuccessJSON writes an object as JSON to the response with a 200 status code.
func (*HttpContext) WriteSuccessXML ¶
func (ctx *HttpContext) WriteSuccessXML(data interface{})
WriteSuccessXML writes an object as XML to the response with a 200 status code.
type KeepAliveConfig ¶
type LoggingConfig ¶
type MongoDBConfig ¶
type MySQLConfig ¶
type OracleConfig ¶
type PostgreSQLConfig ¶
type RateLimitConfig ¶
type RateLimitConfig struct {
RequestsPerSecond int `json:"requests_per_second"`
}
type RedisConfig ¶
type ResponseResult ¶
type ResponseResult struct { Code int `json:"code"` URL string `json:"url,omitempty"` Desc string `json:"desc,omitempty"` Data interface{} `json:"data"` }
ResponseResult represents a unified response structure.
type SQLiteConfig ¶
type SQLiteConfig struct {
DBPath string `json:"dbpath"`
}
type SecurityConfig ¶
type SecurityConfig struct { AllowedHosts []string `json:"allowed_hosts"` CORS CORSConfig `json:"cors"` CSRFProtection bool `json:"csrf_protection"` }
type ServerConfig ¶
type ServerConfig struct { Domain string `json:"domain"` Port int `json:"port"` ReadTimeout time.Duration `json:"read_timeout"` WriteTimeout time.Duration `json:"write_timeout"` MaxHeaderBytes int `json:"max_header_bytes"` TLS TLSConfig `json:"tls"` Limits RateLimitConfig `json:"limits"` RateLimit RateLimitConfig `json:"rate_limit"` Logging LoggingConfig `json:"logging"` Security SecurityConfig `json:"security"` Timeouts TimeoutsConfig `json:"timeouts"` KeepAlive KeepAliveConfig `json:"keep_alive"` Compression CompressionConfig `json:"compression"` StaticFiles StaticFilesConfig `json:"static_files"` Middleware []string `json:"middleware"` }
type StaticFilesConfig ¶
type TimeoutsConfig ¶
type TrieNode ¶
type TrieNode struct { Children []*TrieNode `json:"children"` // Child nodes of the current node. Handler func(ctx *HttpContext) `json:"-"` // Handler function for the node. Level int `json:"level"` // Depth level of the node in the trie. Pattern string `json:"pattern"` // Pattern of the node. NodeType NodeType `json:"node_type"` // Type of the node (Static, Param, Regex). Method string `json:"method"` // HTTP method associated with the route. FullPath string `json:"full_path"` // Full path to the node. Path string `json:"path"` // Name of the current node. }
TrieNode represents a node in the trie.