main
v2.0.0+incompatible
Documentation
pkg.go.dev ↗REST ==== This example demonstrates a HTTP REST web service with some fixture data. Follow along the example and patterns.
Also check routes.json for the generated docs from passing the -routes flag
Boot the server: ---------------- $ go run main.go
Client requests: ---------------- $ curl http://localhost:3333/ root.
$ curl http://localhost:3333/articles [{"id":"1","title":"Hi"},{"id":"2","title":"sup"}]
$ curl http://localhost:3333/articles/1 {"id":"1","title":"Hi"}
$ curl -X DELETE http://localhost:3333/articles/1 {"id":"1","title":"Hi"}
$ curl http://localhost:3333/articles/1 "Not Found"
$ curl -X POST -d '{"id":"will-be-omitted","title":"awesomeness"}' http://localhost:3333/articles {"id":"97","title":"awesomeness"}
$ curl http://localhost:3333/articles/97 {"id":"97","title":"awesomeness"}
$ curl http://localhost:3333/articles [{"id":"2","title":"sup"},{"id":"97","title":"awesomeness"}]
Functions
AdminOnly
func AdminOnly(next http.Handler) http.Handler
AdminOnly middleware restricts access to just administrators.
ArticleCtx
func ArticleCtx(next http.Handler) http.Handler
ArticleCtx middleware is used to load an Article object from the URL parameters passed through as the request. In case the Article could not be found, we stop here and return a 404.
CreateArticle
func CreateArticle(w http.ResponseWriter, r *http.Request)
CreateArticle persists the posted Article and returns it back to the client as an acknowledgement.
DeleteArticle
func DeleteArticle(w http.ResponseWriter, r *http.Request)
DeleteArticle removes an existing Article from our persistent store.
GetArticle
func GetArticle(w http.ResponseWriter, r *http.Request)
GetArticle returns the specific Article. You'll notice it just fetches the Article right off the context, as its understood that if we made it this far, the Article must be on the context. In case its not due to a bug, then it will panic, and our Recoverer will save us.
ListArticles
func ListArticles(w http.ResponseWriter, r *http.Request)
ListArticles returns an array of Articles.
SearchArticles
func SearchArticles(w http.ResponseWriter, r *http.Request)
SearchArticles searches the Articles data for a matching article. It's just a stub, but you get the idea.
UpdateArticle
func UpdateArticle(w http.ResponseWriter, r *http.Request)
UpdateArticle updates an existing Article in our persistent store.
Types
Article
type Article struct {
ID string `json:"id"`
Title string `json:"title"`
}