thf

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Mar 1, 2023 License: MIT Imports: 6 Imported by: 0

README

thf

typical http.Handler func

http标准库中 func(http.ResponseWriter, *http.Request) 函数进行泛型处理

开发者只需关注输入,输出对象的声明,及错误处理方式

Installation

golang version 1.81+

go get github.com/otk-final/thf

Usage & Example

默认处理方式
func api(writer http.ResponseWriter, request *http.Request){
  //读取数据
  ioutil.ReadAll(request.Body)
  //反序列化 struct
  json.Unmarshal(body, &t)
  
  
  //业务处理
  //...
  
  
  //序列化
  bytes,_ := json.Marshal(&t)
  //响应数据
  writer.Write(bytes)
}

//路由
mux := http.NewServeMux()
mux.HandleFunc("/a",apiA)
mux.HandleFunc("/b",apiB)
mux.HandleFunc("/c",apiC)

不同api需要独立处理参数的序列化,和反序列化,通常一个单体应用参数序列化方式是同一的,如json.Unmarshal or json.Marshal

现将繁琐的操作交由 thf 框架层处理,其支持 7 种不同方法声明

  1. func(http.ResponseWriter, *http.Request, T) (R, error)		
    
  2. func(http.ResponseWriter, *http.Request, T) R
    
  3. func(http.ResponseWriter, *http.Request, T) error
    
  4. func(http.ResponseWriter, *http.Request, T)
    
  5. func(http.ResponseWriter, *http.Request) (R, error)
    
  6. func(http.ResponseWriter, *http.Request) R
    
  7. func(http.ResponseWriter, *http.Request) error
    
thf处理方式
// typical http.Handler func
func typicalApi(writer http.ResponseWriter, request *http.Request, in *Foo) (*Bar, error) {
	//TODO		
}

mux := http.NewServeMux()
mux.HandleFunc("/a",thf.Wrap(typicalApi).Func())
mux.HandleFunc("/b",thf.WrapIO(typicalApi).Func())
mux.HandleFunc("/c",thf.WrapIE(typicalApi).Func())
mux.HandleFunc("/d",thf.WrapI(typicalApi).Func())
mux.HandleFunc("/e",thf.WrapOE(typicalApi).Func())
mux.HandleFunc("/f",thf.WrapO(typicalApi).Func())
mux.HandleFunc("/g",thf.WrapE(typicalApi).Func())

// IOE 是 in out error 缩写
自定义编解码
//实现编解码接口

type Decoder[T any] interface {
  //参数解析
  Decode(*http.Request) (T, error)
}

type Encoder[R any] interface {
  //正常响应
  Out(http.ResponseWriter, *http.Request, R)
  //异常响应
  Error(http.ResponseWriter, *http.Request, error)
}


mux.HandleFunc("/custom",thf.Wrap(typicalApi).FuncBy(xxxDecoder,xxxEncoder))

Documentation

Overview

Package thf import "github.com/otk-final/thf"

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func InitRender

func InitRender(rd *render.Render)

Types

type Decoder

type Decoder[T any] interface {
	Decode(*http.Request) (T, error)
}

type Encoder

type Encoder[R any] interface {
	Out(http.ResponseWriter, *http.Request, R)
	Error(http.ResponseWriter, *http.Request, error)
}

type Wrapper

type Wrapper[In any, Out any] struct {
	// contains filtered or unexported fields
}

func Wrap

func Wrap[In any, Out any](target ioeFunc[In, Out]) Wrapper[In, Out]

func WrapE

func WrapE(target eFunc) Wrapper[void, void]

func WrapI

func WrapI[In any](target iFunc[In]) Wrapper[In, void]

func WrapIE

func WrapIE[In any](target ieFunc[In]) Wrapper[In, void]

func WrapIO

func WrapIO[In any, Out any](target ioFunc[In, Out]) Wrapper[In, Out]

func WrapO

func WrapO[Out any](target oFunc[Out]) Wrapper[void, Out]

func WrapOE

func WrapOE[Out any](target oeFunc[Out]) Wrapper[void, Out]

func (Wrapper[T, R]) Func

func (w Wrapper[T, R]) Func() http.HandlerFunc

func (Wrapper[In, Out]) FuncBy

func (w Wrapper[In, Out]) FuncBy(decode Decoder[In], encode Encoder[Out]) http.HandlerFunc

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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