http

package module
v0.0.24 Latest Latest
Warning

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

Go to latest
Published: Sep 15, 2021 License: BSD-3-Clause Imports: 25 Imported by: 2

README

Http component for Enorith

Usage

Basic example
package main

import (
	"github.com/enorith/container"
	"github.com/enorith/http"
	"github.com/enorith/http/content"
	"github.com/enorith/http/contracts"
	"github.com/enorith/http/router"
	"log"
	"fmt"
)

type FooRequest struct {
	content.Request
    // input injection and validation
	Foo string `input:"foo" validate:"required"`
	File contracts.UploadFile `file:"file"`
}

func main() {
	s := http.NewServer(func() *container.Container {
		return container.New() // runtime IoC container
	}, true)
	
	e := s.Serve(":10800", func(ro *router.Wrapper, k *http.Kernel) {
		//k.OutputLog = true
		ro.HandleGet("/", func(r contracts.RequestContract) contracts.ResponseContract {

			return content.TextResponse("ok", 200)
		})

		// request injection
		ro.Get("/foo", func(r FooRequest) contracts.ResponseContract {

			return content.TextResponse("input foo: " + r.Foo, 200)
		})

		// param injection
		ro.Get("/:id", func(id content.ParamUint64) string {

			return fmt.Sprintf("input id: %d", id)
		})
	})

	if e != nil {
		log.Fatalf("serve error: %v", e)
	}
}
Middleware
package foo

import (
	"github.com/enorith/http"
	"github.com/enorith/http/contracts"
	"github.com/enorith/http/router"
)

type FooMiddleware struct {

}

func (FooMiddleware) Handle(r contracts.RequestContract, next http.PipeHandler) contracts.ResponseContract {
	//TODO: before request handled

	resp := next(r)

	//TOD: after request handled
	return resp
}


func Handler(ro *router.Wrapper, k *http.Kernel) {
	k.SetMiddleware([]http.RequestMiddleware{ // global middleware
		FooMiddleware{},
	})

	k.SetMiddlewareGroup(map[string][]http.RequestMiddleware{
		"foo.mid": {FooMiddleware{}}
	})

	ro.Get("foo", fun() string { return "bar"}).Middleware("foo.mid")
}
Working with Container
package main

import (
	"reflect"

	"github.com/enorith/container"
	"github.com/enorith/http"
	"github.com/enorith/http/contracts"
	"github.com/enorith/http/router"
)

type FooStruct struct {
	Bar string
}

type FooService struct {
	Foo FooStruct // construct injection
}


func (fs FooService) GetBar() string {
	return fs.Foo.Bar
}

func main() {
	srv := http.NewServer(func(request contracts.RequestContract) container.Interface {
		con := container.New()

		con.BindFunc(FooStruct{}, func(c container.Interface) (reflect.Value, error) { // bind instance
			return reflect.ValueOf(FooStruct{Bar: "baz"}), nil
		}, false)
		return con
	}, true)

	srv.Serve(":8000", func(rw *router.Wrapper, k *http.Kernel) {


		rw.Get("foo", func(fs FooService) string { // parameter injection
			return fs.GetBar()
		})
	})
}

TODO

  • Get client ip behand proxy
  • Validation (incomplete)
  • Logging

Documentation

Index

Constants

View Source
const (
	HandlerFastHttp handlerType = iota
	HandlerNetHttp
)
View Source
const DefaultConcurrency = 256 * 1024
View Source
const Version = "v0.0.23"

Variables

View Source
var (
	ReadTimeout  = time.Second * 30
	WriteTimeout = time.Second * 30
	IdleTimeout  = time.Second * 30
)

Functions

func GetFsHandler

func GetFsHandler(root string, stripSlashes int) fasthttp.RequestHandler

func MiddlewareChain added in v0.0.10

func MiddlewareChain(mid ...RequestMiddleware) middlewareChain

Types

type ContainerRegister added in v0.0.10

type ContainerRegister func(request contracts.RequestContract) container.Interface

type Kernel

type Kernel struct {
	RequestCurrency    int
	MaxRequestBodySize int
	OutputLog          bool
	Handler            handlerType
	// contains filtered or unexported fields
}

func NewKernel

func NewKernel(cr ContainerRegister, debug bool) *Kernel

func (*Kernel) FastHttpHandler

func (k *Kernel) FastHttpHandler(ctx *fasthttp.RequestCtx)

func (*Kernel) Handle

func (*Kernel) IsKeepAlive

func (k *Kernel) IsKeepAlive() bool

func (*Kernel) KeepAlive

func (k *Kernel) KeepAlive(b ...bool) *Kernel

func (*Kernel) SendRequestToRouter

func (k *Kernel) SendRequestToRouter(r contracts.RequestContract) contracts.ResponseContract

func (*Kernel) ServeHTTP

func (k *Kernel) ServeHTTP(w http.ResponseWriter, r *http.Request)

func (*Kernel) SetErrorHandler

func (k *Kernel) SetErrorHandler(handler errors.ErrorHandler)

func (*Kernel) SetMiddleware

func (k *Kernel) SetMiddleware(ms []RequestMiddleware)

func (*Kernel) SetMiddlewareGroup

func (k *Kernel) SetMiddlewareGroup(middlewareGroup map[string][]RequestMiddleware)

func (*Kernel) Use

func (k *Kernel) Use(m RequestMiddleware) *Kernel

func (*Kernel) Wrapper

func (k *Kernel) Wrapper() *router.Wrapper

type KernelRequestResolver

type KernelRequestResolver struct {
}

func (KernelRequestResolver) ResolveRequest

func (rr KernelRequestResolver) ResolveRequest(r contracts.RequestContract, runtime container.Interface)

type MiddlewareGroup

type MiddlewareGroup map[string][]RequestMiddleware

type PipeFunc

PipeFunc request middleware function

type PipeHandler

PipeHandler destination handler

type Pipeline

type Pipeline struct {
	// contains filtered or unexported fields
}

Pipeline is request pipeline prepare for request middleware

func (*Pipeline) Send

Send request to pipeline

func (*Pipeline) Then

Then final destination

func (*Pipeline) Through

func (p *Pipeline) Through(pipe interface{}) *Pipeline

Through middleware

func (*Pipeline) ThroughFunc

func (p *Pipeline) ThroughFunc(pipe PipeFunc) *Pipeline

ThroughFunc through middleware function

func (*Pipeline) ThroughMiddleware

func (p *Pipeline) ThroughMiddleware(pipe RequestMiddleware) *Pipeline

ThroughMiddleware through middleware struct

type RequestInjector

type RequestInjector struct {
	// contains filtered or unexported fields
}

RequestInjector inject request object, with validation

func (*RequestInjector) Injection

func (r *RequestInjector) Injection(abs interface{}, value reflect.Value) (reflect.Value, error)

func (*RequestInjector) When

func (r *RequestInjector) When(abs interface{}) bool

type RequestMiddleware

type RequestMiddleware interface {
	Handle(r contracts.RequestContract, next PipeHandler) contracts.ResponseContract
}

RequestMiddleware request middleware

type RequestResolver added in v0.0.10

type RequestResolver interface {
	ResolveRequest(r contracts.RequestContract, container container.Interface)
}

type RouterRegister

type RouterRegister func(rw *router.Wrapper, k *Kernel)

type Server

type Server struct {
	// contains filtered or unexported fields
}

func NewServer

func NewServer(cr ContainerRegister, debug bool) *Server

func (*Server) GetFastHttpServer

func (s *Server) GetFastHttpServer(kernel *Kernel) *fasthttp.Server

func (*Server) Serve

func (s *Server) Serve(addr string, register RouterRegister)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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