https://github.com/southclaws/pocket
A neat little web library to help you write cleaner HTTP request handlers!
https://github.com/southclaws/pocket
go-http go-web http http-handlers reflection
Last synced: 10 months ago
JSON representation
A neat little web library to help you write cleaner HTTP request handlers!
- Host: GitHub
- URL: https://github.com/southclaws/pocket
- Owner: Southclaws
- Created: 2019-08-09T17:56:22.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2019-09-05T17:49:39.000Z (over 6 years ago)
- Last Synced: 2025-03-29T16:51:13.484Z (11 months ago)
- Topics: go-http, go-web, http, http-handlers, reflection
- Language: Go
- Size: 48.8 KB
- Stars: 8
- Watchers: 3
- Forks: 1
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Pocket Gopher!
> Pocket gophers, commonly referred to as just gophers, are burrowing rodents of the family Geomyidae.
[](https://github.com/Southclaws/pocket/actions)
**(Work In Progress)**
Pocket is a neat little web library to help you write cleaner HTTP request handlers!
It's inspired by how a few other languages provide handler APIs, mainly [Rocket](https://rocket.rs/) (hence the name!)
## Example
Here's a quick example:
```go
func handler(props struct {
UserID string `param:"user_id"`
BodyJSON J
}) pocket.Responder {
// Do stuff with `props`!
// simply return an error or a responder! no more writing to w!
}
```
## Why?
Go has a great ecosystem for HTTP middleware thanks to the standard Request/ResponseWriter interface. This pattern is
great, until it comes to writing your actual business logic handlers. The Request/ResponseWriter is just too low level
for doing quick things like getting query parameters or grabbing some JSON.
Sure, you can write some helpers for this but then if you have a few projects, you end up copying/importing and calling
this boilerplate all over the place. Doing it declaratively is much nicer!
Note: This package makes generous use of reflection and, while all efforts are made to cache unnecessary reflection
calls and perform most of at handler generation-time, this reflection _may_ impact extremely high-traffic web servers.
There are currently no benchmarks but this is something I plan to work on in future.
## Some Ideas
_A scratchpad for ideas for this library!_
- `func(string)`/`func() string` signatures for quick and easy text requests/responses.
- `func(T)`/`func(T)` signatures for quick JSON endpoints.
- nice default for `func(...) error` signatures
- Provide some configuration for the handler generator:
- mapping from `error` types to statuses, for those `func(...) error` sigs.
- wrap JSON responses in some default body
- generate index/sitemap/documentation based on signatures