Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/w0rp/lax
An HTTP REST library for Go
https://github.com/w0rp/lax
Last synced: about 1 month ago
JSON representation
An HTTP REST library for Go
- Host: GitHub
- URL: https://github.com/w0rp/lax
- Owner: w0rp
- License: mit
- Created: 2021-05-31T11:24:45.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2021-06-18T09:07:08.000Z (over 3 years ago)
- Last Synced: 2024-10-14T05:27:26.370Z (3 months ago)
- Language: Go
- Size: 6.84 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# lax
Lax - A library for building simple REST APIs in Go.
```
^ ^
("\(-_-)/")
)( )(
((...) (...))
```A library by w0rp for implementing HTTP request views for REST APIs, with
hopefully very few lines of code. The library doesn't do all that much for you,
it just makes it easier to write endpoints for a REST API, without wrapping
the entire standard library in slower abstractions.## Example Usage
This library is very experimental. Here is some example usage as of the time
this README was written.```go
package mainimport (
"log"
"net/http"
"github.com/w0rp/lax"
)// Declare a type to marhsal/unmarshal
type Comment struct {
Text string `json:"text"`
}// Just store an Array of comments in memory, as an example.
var commentList []Comment = make([]Comment, 0, 20)var commentListHandler http.HandlerFunc = lax.Wrap(lax.View{
// A simple GET implementation that returns the data.
Get: func(request *lax.Request) interface{} {
return commentList
},
// A simple POST implementation that lets a user submit a new Comment.
Post: func(request *lax.Request) interface{} {
comment := Comment{}if err := request.JSON(&comment); err != nil {
// Return a 400 error response with the error message.
return lax.MakeBadRequestResponse(err)
}if (len(comment.Text) == 0 || len(comment.Text) > 500) {
// Return a 400 error response pointing out a problem.
//
// Comes out as [
// {
// "path": "text",
// "problem": "missing or invalid text"
// }
// ]
return lax.MakeErrorListResponse(
lax.Issue("text", "missing or invalid text"),
)
}commentList = append(commentList, comment)
return &comment
},
})func main() {
// Serve the endpoint like any other with the standard library.
//
// You can mix and match the probably slower Lax views with more efficient
// request handlers for very hot endpoints.
http.HandleFunc("/comment", commentListHandler)log.Fatal(http.ListenAndServe(":8080", nil))
}
```