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
Foo string `input:"foo" validate:"required"`
File contracts.UploadFile `file:"file"`
}
func main() {
s := http.NewServer(func() *container.Container {
return container.New()
}, true)
e := s.Serve(":10800", func(ro *router.Wrapper, k *http.Kernel) {
ro.HandleGet("/", func(r contracts.RequestContract) contracts.ResponseContract {
return content.TextResponse("ok", 200)
})
ro.Get("/foo", func(r FooRequest) contracts.ResponseContract {
return content.TextResponse("input foo: " + r.Foo, 200)
})
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 {
resp := next(r)
return resp
}
func Handler(ro *router.Wrapper, k *http.Kernel) {
k.SetMiddleware([]http.RequestMiddleware{
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
}
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) {
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 {
return fs.GetBar()
})
})
}
TODO