mirror of
https://github.com/ggicci/httpin.git
synced 2025-02-21 19:06:46 +02:00
refactor: renaming
This commit is contained in:
parent
b3cb3af44f
commit
6d9c8815fc
@ -34,7 +34,7 @@ type UserQuery struct {
|
||||
Use `httpin` to extract the data from `http.Request` for you:
|
||||
|
||||
```go
|
||||
httpinUserQuery, err := httpin.NewEngine(UserQuery{})
|
||||
httpinUserQuery, err := httpin.New(UserQuery{})
|
||||
if err != nil {
|
||||
// You can create your engines at server start up.
|
||||
// And reuse them in your http handlers.
|
||||
|
24
chain.go
24
chain.go
@ -1,6 +1,7 @@
|
||||
package httpin
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
@ -9,3 +10,26 @@ import (
|
||||
// https://github.com/justinas/alice to chain your HTTP middleware functions
|
||||
// and the app handler.
|
||||
type Middleware func(http.Handler) http.Handler
|
||||
|
||||
func NewInput(inputStruct interface{}) Middleware {
|
||||
core, err := New(inputStruct)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return func(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
|
||||
// Here we read the request and decode it to fill our structure.
|
||||
// Once failed, the request should end here.
|
||||
input, err := core.Read(r)
|
||||
if err != nil {
|
||||
http.Error(rw, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
// We put the `input` to the request's context, and it will pass to the next hop.
|
||||
ctx := context.WithValue(r.Context(), Input, input)
|
||||
next.ServeHTTP(rw, r.WithContext(ctx))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@ -4,6 +4,6 @@ type Directive struct {
|
||||
Key string // e.g. query.page, header.x-api-token
|
||||
}
|
||||
|
||||
func BuildDirective(key string) (*Directive, error) {
|
||||
func buildDirective(key string) (*Directive, error) {
|
||||
return &Directive{Key: key}, nil
|
||||
}
|
||||
|
51
engine.go
51
engine.go
@ -1,51 +0,0 @@
|
||||
package httpin
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"reflect"
|
||||
)
|
||||
|
||||
type Engine struct {
|
||||
inputType reflect.Type
|
||||
tree *FieldResolver
|
||||
}
|
||||
|
||||
func NewEngine(inputStruct interface{}, opts ...EngineOption) (*Engine, error) {
|
||||
typ := reflect.TypeOf(inputStruct) // retrieve type information
|
||||
if typ.Kind() == reflect.Ptr {
|
||||
typ = typ.Elem()
|
||||
}
|
||||
if typ.Kind() != reflect.Struct {
|
||||
return nil, UnsupportedTypeError{Type: typ}
|
||||
}
|
||||
|
||||
engine := &Engine{
|
||||
inputType: typ,
|
||||
}
|
||||
|
||||
if err := engine.build(); err != nil {
|
||||
return nil, fmt.Errorf("httpin: build: %w", err)
|
||||
}
|
||||
|
||||
return engine, nil
|
||||
}
|
||||
|
||||
func (e *Engine) ReadRequest(r *http.Request) (interface{}, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
// newInstance creates a new instance of the input struct.
|
||||
func (e *Engine) newInstance() reflect.Value {
|
||||
return reflect.New(e.inputType)
|
||||
}
|
||||
|
||||
// build builds extractors for the exported fields of the input struct.
|
||||
func (e *Engine) build() error {
|
||||
tree, err := buildResolverTree(e.inputType)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
e.tree = tree
|
||||
return nil
|
||||
}
|
@ -1 +0,0 @@
|
||||
package httpin
|
50
httpin.go
50
httpin.go
@ -1,9 +1,9 @@
|
||||
package httpin
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"reflect"
|
||||
)
|
||||
|
||||
type ContextKey int
|
||||
@ -12,25 +12,41 @@ const (
|
||||
Input ContextKey = iota // the primary key to get the input object in the context injected by httpin
|
||||
)
|
||||
|
||||
func New(inputStruct interface{}) Middleware {
|
||||
engine, err := NewEngine(inputStruct)
|
||||
if err != nil {
|
||||
panic(fmt.Errorf("httpin: unable to create engine: %w", err))
|
||||
type Core struct {
|
||||
inputType reflect.Type
|
||||
tree *FieldResolver
|
||||
}
|
||||
|
||||
return func(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
|
||||
// Here we read the request and decode it to fill our structure.
|
||||
// Once failed, the request should end here.
|
||||
input, err := engine.ReadRequest(r)
|
||||
if err != nil {
|
||||
http.Error(rw, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
|
||||
return
|
||||
func New(inputStruct interface{}, opts ...CoreOption) (*Core, error) {
|
||||
typ := reflect.TypeOf(inputStruct) // retrieve type information
|
||||
if typ.Kind() == reflect.Ptr {
|
||||
typ = typ.Elem()
|
||||
}
|
||||
if typ.Kind() != reflect.Struct {
|
||||
return nil, UnsupportedTypeError{Type: typ}
|
||||
}
|
||||
|
||||
// We put the `input` to the request's context, and it will pass to the next hop.
|
||||
ctx := context.WithValue(r.Context(), Input, input)
|
||||
next.ServeHTTP(rw, r.WithContext(ctx))
|
||||
})
|
||||
core := &Core{
|
||||
inputType: typ,
|
||||
}
|
||||
|
||||
if err := core.build(); err != nil {
|
||||
return nil, fmt.Errorf("httpin: build: %w", err)
|
||||
}
|
||||
|
||||
return core, nil
|
||||
}
|
||||
|
||||
func (e *Core) Read(r *http.Request) (interface{}, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
// build builds extractors for the exported fields of the input struct.
|
||||
func (e *Core) build() error {
|
||||
tree, err := buildResolverTree(e.inputType)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
e.tree = tree
|
||||
return nil
|
||||
}
|
||||
|
@ -1,3 +1,3 @@
|
||||
package httpin
|
||||
|
||||
type EngineOption func(*Engine)
|
||||
type CoreOption func(*Core)
|
||||
|
@ -89,7 +89,7 @@ func parseStructTag(field reflect.StructField) ([]*Directive, error) {
|
||||
return directives, nil // skip
|
||||
}
|
||||
for _, key := range strings.Split(inTag, ",") {
|
||||
directive, err := BuildDirective(key)
|
||||
directive, err := buildDirective(key)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user