hsrv

package
v0.2.21 Latest Latest
Warning

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

Go to latest
Published: Jan 16, 2024 License: Apache-2.0 Imports: 22 Imported by: 0

README

简单Web服务框架

Documentation

Index

Constants

View Source
const (
	LvlDebug = 0
	LvlInfo  = 1
	LvlWarn  = 2
	LvlError = 3
)

Variables

View Source
var LvlName = [4]string{"DEBUG", "INFO", "WARN", "ERROR"}

Functions

This section is empty.

Types

type Config

type Config struct {
	Port    int
	Timeout int64
	Tls     *TlsConfig
}

type CtxTag added in v0.0.10

type CtxTag string
const WebContextVName CtxTag = "WebContext"

type ErrorHandler

type ErrorHandler func(Response, Request, error)

type Interceptor added in v0.0.12

type Interceptor interface {
	Order() int // 顺序
	Before(Response, Request) bool
}

type Logger

type Logger interface {
	Debug(v ...any)
	Info(v ...any)
	Warn(v ...any)
	Error(v ...any)
	Debugf(format string, v ...any)
	Infof(format string, v ...any)
	Warnf(format string, v ...any)
	Errorf(format string, v ...any)
}

type MultiForm

type MultiForm struct {
	*multipart.Form
	// contains filtered or unexported fields
}

func (*MultiForm) Close

func (m *MultiForm) Close()

func (*MultiForm) ParseFile

func (m *MultiForm) ParseFile(writer func(string, io.Reader, error))

type PostProcessor added in v0.0.12

type PostProcessor interface {
	Order() int // 顺序
	After(Response) bool
}

type Request

type Request struct {
	*http.Request
	Ctx *reqCtx
}

func (*Request) FormData

func (r *Request) FormData() (map[string][]string, error)

func (*Request) IP added in v0.0.11

func (r *Request) IP() (string, error)

func (*Request) Json

func (r *Request) Json(target any) error

func (*Request) MultipartForm

func (r *Request) MultipartForm() (f *MultiForm, err error)

func (*Request) MultipartFormLarge added in v0.0.12

func (r *Request) MultipartFormLarge(size int64) (f *MultiForm, err error)

处理更大的formdata size: 单位为 Mib

func (*Request) Stream

func (r *Request) Stream(wr io.Writer) (err error)

func (*Request) Text

func (r *Request) Text() (string, error)

func (*Request) UrlParam

func (r *Request) UrlParam() (map[string]string, error)

func (*Request) WebContext added in v0.0.10

func (r *Request) WebContext() any

type RequestHandler

type RequestHandler func(Response, Request)

type Response

type Response struct {
	http.ResponseWriter
	Ctx *reqCtx
}

func (*Response) File

func (r *Response) File(input io.Reader, size int64, name string) (int64, error)

func (*Response) Html added in v0.0.13

func (r *Response) Html(html string, statusCode int) (err error)

func (*Response) Json

func (r *Response) Json(target any, statusCode int) error

func (*Response) Sse added in v0.0.13

func (r *Response) Sse(fun func(hf http.Flusher, r *Response)) (err error)

func (*Response) Text

func (r *Response) Text(txt string, statusCode int) (err error)

type Server

type Server struct {
	Ctx context.Context // 全局上下文

	Logger          Logger                  // 日志输出
	Config          Config                  // 配置
	ErrorHandler    ErrorHandler            // 统一错误处理
	NotFoundHandler RequestHandler          // 统一404处理
	CtxDataGetter   func(*http.Request) any // 上下文生成器

	WebContext any // 全局web上下文(所有连接共享), 会被放到Request中
	// contains filtered or unexported fields
}

func NewServer

func NewServer(config Config) *Server

func (*Server) Handle

func (s *Server) Handle(path string, method string, handler RequestHandler)

listen method on path path: listen path (if end with '/' ,will listen all start with $path) method: listen method such as 'GET', 'POST', 'PUT', 'DELETE' handler: function handler ======= 监听方法 path: 路径 (如果以 '/' 为结尾,则会监听所有以$path开头的路径) method: 监听的服务器方法 handler: 执行方法的句柄

func (*Server) Interceptor added in v0.0.12

func (s *Server) Interceptor(prefix string, interceptor Interceptor)

function around handler prefix: url prefix for interceptor

func (*Server) ListenAndServe

func (s *Server) ListenAndServe() error

func (*Server) PostProcessor added in v0.0.12

func (s *Server) PostProcessor(prefix string, postProcessor PostProcessor)

func (*Server) Static added in v0.0.10

func (s *Server) Static(path, static string)

func (*Server) Stop

func (s *Server) Stop()

type TlsConfig

type TlsConfig struct {
	CrtPath string
	KeyPath string
	CaPath  string
	Cors    []string
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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