http

package module
v0.0.7 Latest Latest
Warning

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

Go to latest
Published: Aug 11, 2021 License: MIT Imports: 25 Imported by: 2

README

Http component for Enorith

Basic usage

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)
	}
}

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.7"

Variables

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

Functions

func GetFsHandler

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

Types

type Kernel

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

func NewKernel

func NewKernel(cr router.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 RouterRegister

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

type Server

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

func NewServer

func NewServer(cr router.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