• Stars
    star
    153
  • Rank 243,368 (Top 5 %)
  • Language
    Go
  • License
    BSD 3-Clause "New...
  • Created about 12 years ago
  • Updated 11 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Package gorilla/pat is a pretty simple HTTP router for Go.

pat

testing codecov godoc sourcegraph

Gorilla Logo

How to use?

pat is pretty simple. The documentation lives here.

Install

With a properly configured Go toolchain:

go get github.com/gorilla/pat

Example

Here's an example of a RESTful api:

package main

import (
	"log"
	"net/http"

	"github.com/gorilla/pat"
)

func homeHandler(wr http.ResponseWriter, req *http.Request) {
	wr.WriteHeader(http.StatusOK)
	wr.Write([]byte("Yay! We're home, Jim!"))
}

func getAllTheThings(wr http.ResponseWriter, req *http.Request) {
	wr.WriteHeader(http.StatusOK)
	wr.Write([]byte("Look, Jim! Get all the things!"))
}

func putOneThing(wr http.ResponseWriter, req *http.Request) {
	wr.WriteHeader(http.StatusOK)
	wr.Write([]byte("Look, Jim! Put one thing!"))
}

func deleteOneThing(wr http.ResponseWriter, req *http.Request) {
	wr.WriteHeader(http.StatusOK)
	wr.Write([]byte("Look, Jim! Delete one thing!"))
}

func main() {
	router := pat.New()

	router.Get("/things", getAllTheThings)
	router.Put("/things/{id}", putOneThing)
	router.Delete("/things/{id}", deleteOneThing)
	router.Get("/", homeHandler)

	http.Handle("/", router)

	log.Print("Listening on 127.0.0.1:8000...")
	log.Fatal(http.ListenAndServe(":8000", nil))
}

Notice how the routes descend? That's because Pat will take the first route that matches. For your own testing, take the line router.Get("/", homeHandler) and put it above the other routes and run the example. When you try to curl any of the routes, you'll only get what the homeHandler returns. Design your routes carefully.

More Repositories

1

websocket

Package gorilla/websocket is a fast, well-tested and widely used WebSocket implementation for Go.
Go
21,832
star
2

mux

Package gorilla/mux is a powerful HTTP router and URL matcher for building Go web servers with 🦍
Go
20,537
star
3

sessions

Package gorilla/sessions provides cookie and filesystem sessions and infrastructure for custom session backends.
Go
2,829
star
4

handlers

Package gorilla/handlers is a collection of useful middleware for Go HTTP services & web applications πŸ›ƒ
Go
1,640
star
5

schema

Package gorilla/schema fills a struct with form values.
Go
1,351
star
6

csrf

Package gorilla/csrf provides Cross Site Request Forgery (CSRF) prevention middleware for Go web applications & services πŸ”’
Go
1,024
star
7

feeds

Package gorilla/feeds is a golang rss/atom generator library
Go
732
star
8

securecookie

Package gorilla/securecookie encodes and decodes authenticated and optionally encrypted cookie values for Go web applications.
Go
682
star
9

rpc

Package gorilla/rpc is a golang foundation for RPC over HTTP services.
Go
581
star
10

context

Package gorilla/context is a golang registry for global request variables.
Go
431
star
11

http

Package gorilla/http is an alternative HTTP client implementation for Go.
Go
267
star
12

css

Package gorilla/css is a CSS3 tokenizer.
Go
86
star
13

muxy

Package gorilla/muxy takes gorilla/mux to the next level
Go
74
star
14

gorilla.github.io

Gorilla web toolkit's website.
HTML
59
star
15

reverse

Package gorilla/reverse is a set of utilities to create request routers.
Go
53
star
16

i18n

Package gorilla/i18n groups packages related to internationalization
Go
49
star
17

template

A fork of the standard template packages.
Go
46
star
18

.github

The .github repository for the @gorilla organization.
9
star