package main
import (
"fmt"
"github.com/gorilla/pat"
"net/http"
"runtime"
)
var r *pat.Router
type handler func(http.ResponseWriter, *http.Request) error
func (h handler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
err := h(w, req)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
func index(w http.ResponseWriter, req *http.Request) (err error) {
fmt.Fprint(w, "Bonjour")
return
}
func hello(w http.ResponseWriter, req *http.Request) (err error) {
fmt.Fprint(w, "hello")
return
}
func main() {
runtime.GOMAXPROCS(runtime.NumCPU())
r = pat.New()
r.Add("GET", "/hello", handler(hello)).Name("hello")
r.Add("GET", "/", handler(index)).Name("index")
if err := http.ListenAndServe(":8000", r); err != nil {
panic(err)
}
}