Security News
Introducing the Socket Python SDK
The initial version of the Socket Python SDK is now on PyPI, enabling developers to more easily interact with the Socket REST API in Python projects.
github.com/enorith/http
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)
}
}
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")
}
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()
})
})
}
FAQs
Unknown package
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
The initial version of the Socket Python SDK is now on PyPI, enabling developers to more easily interact with the Socket REST API in Python projects.
Security News
Floating dependency ranges in npm can introduce instability and security risks into your project by allowing unverified or incompatible versions to be installed automatically, leading to unpredictable behavior and potential conflicts.
Security News
A new Rust RFC proposes "Trusted Publishing" for Crates.io, introducing short-lived access tokens via OIDC to improve security and reduce risks associated with long-lived API tokens.