
Research
Security News
The Growing Risk of Malicious Browser Extensions
Socket researchers uncover how browser extensions in trusted stores are used to hijack sessions, redirect traffic, and manipulate user behavior.
github.com/deluan/rest
This package provides a simple REST controller compatible with the JSON Server API "dialect". This package enables the creation of backends for the great React-admin framework using pure Go, but can be used in other scenarios where you need a simple REST server for your data.
To use it, you will need to provide an implementation of the Repository interface and a function to create
such repository (the constructor). For a simple implementation of an in-memory repository, see
/examples/sample_repository.go
.
The controller was created to be used with Gorilla Pat, as it requires URL params to be parsed and set as query params. You can easily adapt it to work with other routers and frameworks using a custom middleware.
The functionality is provided by a set of handlers named after the REST verbs they handle: Get()
, GetAll()
, Put()
,
Post()
and Delete()
. Each of these functions receive a constructor for your repository, and an optional
implementation of the Logger interface (compatible with Logrus). If no Logger is
specified, the functions falls back to the default Go log package.
Example using Gorilla Pat:
func NewThingsRepository(ctx context) rest.Repository {
return &ThingsRepository{ctx: ctx}
}
func main() {
router := pat.New()
router.Get("/thing/{id}", rest.Get(NewThingsRepository))
router.Get("/thing", rest.GetAll(NewThingsRepository))
router.Post("/thing", rest.Post(NewThingsRepository))
router.Put("/thing/{id}", rest.Put(NewThingsRepository))
router.Delete("/thing/{id}", rest.Delete(NewThingsRepository))
http.Handle("/", router)
log.Print("Listening on 127.0.0.1:8000...")
log.Fatal(http.ListenAndServe(":8000", nil))
}
Example using chi router:
func main() {
router := chi.NewRouter()
router.Route("/thing", func(r chi.Router) {
r.Get("/", rest.GetAll(NewThingsRepository))
r.Post("/", rest.Post(NewThingsRepository))
r.Route("/{id:[0-9]+}", func(r chi.Router) {
r.With(urlParams).Get("/", rest.Get(NewThingsRepository))
r.With(urlParams).Put("/", rest.Put(NewThingsRepository))
r.With(urlParams).Delete("/", rest.Delete(NewThingsRepository))
})
})
http.Handle("/", router)
log.Print("Listening on 127.0.0.1:8000...")
log.Fatal(http.ListenAndServe(":8000", nil))
}
// Middleware to convert Chi URL params (from Context) to query params, as expected by our REST package
func urlParams(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := chi.RouteContext(r.Context())
parts := make([]string, 0)
for i, key := range ctx.URLParams.Keys {
value := ctx.URLParams.Values[i]
if key == "*" {
continue
}
parts = append(parts, url.QueryEscape(":"+key)+"="+url.QueryEscape(value))
}
q := strings.Join(parts, "&")
if r.URL.RawQuery == "" {
r.URL.RawQuery = q
} else {
r.URL.RawQuery += "&" + q
}
next.ServeHTTP(w, r)
})
}
Add an issue if you need examples for other routers/frameworks
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.
Research
Security News
Socket researchers uncover how browser extensions in trusted stores are used to hijack sessions, redirect traffic, and manipulate user behavior.
Research
Security News
An in-depth analysis of credential stealers, crypto drainers, cryptojackers, and clipboard hijackers abusing open source package registries to compromise Web3 development environments.
Security News
pnpm 10.12.1 introduces a global virtual store for faster installs and new options for managing dependencies with version catalogs.