https://github.com/writeas/impart
Utilities for a JSON-based API.
https://github.com/writeas/impart
api-rest writeas
Last synced: 3 months ago
JSON representation
Utilities for a JSON-based API.
- Host: GitHub
- URL: https://github.com/writeas/impart
- Owner: writeas
- License: mit
- Created: 2015-08-27T18:30:31.000Z (almost 10 years ago)
- Default Branch: master
- Last Pushed: 2020-03-28T17:29:43.000Z (about 5 years ago)
- Last Synced: 2025-01-12T22:43:21.255Z (5 months ago)
- Topics: api-rest, writeas
- Language: Go
- Size: 8.79 KB
- Stars: 1
- Watchers: 6
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
impart
====== [](http://webchat.freenode.net/?channels=writeas) [](https://discuss.write.as/c/development)
**impart** is a library for the final layer between the API and the consumer. It's used in the latest [Write.as](https://write.as) and [HTMLhouse](https://html.house) APIs.
We're still in the early stages of development, so there may be breaking changes.
## Example use
```go
package mainimport (
"fmt"
"github.com/writeas/impart"
"net/http"
)type handlerFunc func(w http.ResponseWriter, r *http.Request) error
func main() {
http.HandleFunc("/", handle(index))
http.ListenAndServe("127.0.0.1:8080", nil)
}func index(w http.ResponseWriter, r *http.Request) error {
fmt.Fprintf(w, "Hello world!")return nil
}func handle(f handlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
handleError(w, r, func() error {
// Do authentication...// Handle the request
err := f(w, r)// Log the request and result...
return err
}())
}
}func handleError(w http.ResponseWriter, r *http.Request, err error) {
if err == nil {
return
}if err, ok := err.(impart.HTTPError); ok {
impart.WriteError(w, err)
return
}impart.WriteError(w, impart.HTTPError{http.StatusInternalServerError, "Internal server error :("})
}
```