hsrv

package
v0.2.20 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 Ctx

type Ctx struct {
	RW http.ResponseWriter
	R  *http.Request
	// contains filtered or unexported fields
}

func (*Ctx) File

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

func (*Ctx) FormData

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

func (*Ctx) Html

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

func (*Ctx) IP

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

func (*Ctx) Json

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

func (*Ctx) MultipartForm

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

func (*Ctx) MultipartFormLarge

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

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

func (*Ctx) ParseJson

func (r *Ctx) ParseJson(target any) error

func (*Ctx) ParseText

func (r *Ctx) ParseText() (string, error)

func (*Ctx) Sse

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

func (*Ctx) Stream

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

func (*Ctx) Text

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

func (*Ctx) UrlParam

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

func (*Ctx) WebContext

func (r *Ctx) WebContext() any

type CtxTag

type CtxTag string
const WebContextVName CtxTag = "WebContext"

type ErrorHandler

type ErrorHandler func(Ctx, error)

type Interceptor

type Interceptor interface {
	Order() int // 顺序
	Before(ctx Ctx) 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

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

type RequestHandler

type RequestHandler func(Ctx)

type Server

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

	Logger          Logger         // 日志输出
	Config          Config         // 配置
	ErrorHandler    ErrorHandler   // 统一错误处理
	NotFoundHandler RequestHandler // 统一404处理

	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

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

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

func (*Server) Static

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
}

Jump to

Keyboard shortcuts

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