Giter VIP home page Giter VIP logo

pat's Introduction

pat (formerly pat.go) - A Sinatra style pattern muxer for Go's net/http library

GoDoc

INSTALL

$ go get github.com/bmizerany/pat

USE

package main

import (
	"io"
	"net/http"
	"github.com/bmizerany/pat"
	"log"
)

// hello world, the web server
func HelloServer(w http.ResponseWriter, req *http.Request) {
	io.WriteString(w, "hello, "+req.URL.Query().Get(":name")+"!\n")
}

func main() {
	m := pat.New()
	m.Get("/hello/:name", http.HandlerFunc(HelloServer))

	// Register this pat with the default serve mux so that other packages
	// may also be exported. (i.e. /debug/pprof/*)
	http.Handle("/", m)
	err := http.ListenAndServe(":12345", nil)
	if err != nil {
		log.Fatal("ListenAndServe: ", err)
	}
}

It's that simple.

For more information, see: http://godoc.org/github.com/bmizerany/pat

CONTRIBUTORS

  • Alexis Svinartchouk (@zvin)
  • Blake Mizerany (@bmizerany)
  • Brian Ketelsen (@bketelsen)
  • Bryan Matsuo (@bmatsuo)
  • Caleb Spare (@cespare)
  • Evan Shaw (@edsrzf)
  • Gary Burd (@garyburd)
  • George Rogers (@georgerogers42)
  • Keith Rarick (@kr)
  • Matt Williams (@mattyw)
  • Mike Stipicevic (@wickedchicken)
  • Nick Saika (@nesv)
  • Timothy Cyrus (@tcyrus)
  • binqin (@binku87)

LICENSE

Copyright (C) 2012 by Keith Rarick, Blake Mizerany

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

pat's People

Contributors

amattn avatar binku87 avatar bketelsen avatar bmatsuo avatar bmizerany avatar cespare avatar garyburd avatar georgerogers42 avatar kr avatar kyledj avatar mattyw avatar nesv avatar tcyrus avatar wickedchicken avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

pat's Issues

please tag and version this project

Hello,

Can you please tag and version this project?

I am the Debian Maintainer for pat and versioning would help Debian keep up with development.

remove bmizerany/assert

The tests still use bmizerany/assert. I haven't used or felt the need to use anything like it not longer after releasing it. :) Pat can easily live without it. If anyone wants to remove the dependency, that would be rad.

Is this project still alive?

I see that it hasn't been updated in a while. Is this project still maintained, or is just so solid that it's done until a future version of Go requires it to change?

question about the top path

i think the pattern /:name/
should match
_localhost:12345/abc/_
and
_localhost:12345/abc_

according the example provided in the code:

// Example pattern with one capture:
// /hello/:name
// Will match:
// /hello/blake
// /hello/keith
// Will not match:
// /hello/blake/
// /hello/blake/foo
// /foo
// /foo/bar
//
// Example 2:
// /hello/:name/
// Will match:
// /hello/blake/
// /hello/keith/foo
// /hello/blake
// /hello/keith
// Will not match:
// /foo
// /foo/bar

What do you think?
But I found the behavior is not the same as I supposed.

The fact is that,
I wanna to the pattern /:name/ match
_localhost:12345/abc_
but when i type the URL in Chrome,
it will turn to
_localhost:12345/:name_

see the example

Broken redirects with trailing slashes in patterns

If there is a trailing slash in a pattern then redirects don't work as expected. Take the example server from the documentation, but add a slash to the pattern:

package main

import (
    "io"
    "net/http"
    "github.com/bmizerany/pat"
    "log"
)

// hello world, the web server
func HelloServer(w http.ResponseWriter, req *http.Request) {
    io.WriteString(w, "hello, "+req.URL.Query().Get(":name")+"!\n")
}

func main() {
    m := pat.New()
    m.Get("/hello/:name/", http.HandlerFunc(HelloServer))

    http.Handle("/", m)
    err := http.ListenAndServe(":12345", nil)
    if err != nil {
        log.Fatal("ListenAndServe: ", err)
    }
}

If we request /hello/bob/ everything works as expected:

$ curl http://localhost:12345/hello/bob/
hello, bob!

But if we request a url without a trailing slash like /hello/bob we get a broken redirect to /hello/:name/

$ curl -si http://localhost:12345/hello/bob | grep Location
Location: /hello/:name/

It looks like the code is using the pattern as the redirect target, instead of the requested url.

The docs on this are also unclear, they say "A pattern ending with a slash will get an implicit redirect to it's non-slash version." - but the redirect appears to be from the non-slash version.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.