Skip to content

Instantly share code, notes, and snippets.

@weatherglass
Created July 27, 2017 20:10
Show Gist options
  • Save weatherglass/229b8cb375dbd23468f0ed4bde0e377a to your computer and use it in GitHub Desktop.
Save weatherglass/229b8cb375dbd23468f0ed4bde0e377a to your computer and use it in GitHub Desktop.
// Compare to code at https://blog.merovius.de/2017/06/18/how-not-to-use-an-http-router.html
package main
import (
"log"
"net/http"
"github.com/go-util/router"
)
func GetUserProfile(res http.ResponseWriter, req *http.Request) {
id := req.URL.Query().Get("id")
res.Write([]byte("Profile for user id: " + id))
return
}
func GetUserAccount(res http.ResponseWriter, req *http.Request) {
id := req.URL.Query().Get("id")
res.Write([]byte("Account for user id: " + id))
return
}
func NewUser(res http.ResponseWriter, req *http.Request) {
res.Write([]byte("Creating a new user..."))
return
}
func main() {
mux := router.NewRouter()
if err := mux.Get("/user/:id:[0-9]+/profile", GetUserProfile); err != nil {
log.Fatal(err)
}
if err := mux.Get("/user/:id:[0-9]+/account", GetUserAccount); err != nil {
log.Fatal(err)
}
if err := mux.Post("/user", NewUser); err != nil {
log.Fatal(err)
}
log.Println(http.ListenAndServe("127.0.0.1:8000", mux))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment