This is simple notes about using opensource gorilla/mux to implement API service
#Define the router
1 2 3 4 5 6 7 8 9 10 11 12 13
router := mux.NewRouter()
//try to get a product by its unique id router.HandleFunc("/product/{id}", getProduct).Methods("GET")
//post a new product, POST method is allowed only router.HandleFunc("/product", newProduct).Methods("POST")
//a more general api example router.HandleFunc("/api/health", func(w http.ResponseWriter, r *http.Request) { // an example API handler json.NewEncoder(w).Encode(map[string]bool{"ok": true}) })
funcgetProduct(w http.ResponseWriter, r *http.Request) {
//this is about how to get router variables //in this example it is 'id' //vars['id'] vars := mux.Vars(r) w.WriteHeader(http.StatusOK) fmt.Println(vars) //extra variabes /product/1/filter=last5mins r.ParseForm() filterRule := r.FormValue("filter") fmt.Println(filterRule) }
type Product struct { Name string Price int Vendor string }
funcnewProduct(w http.ResponseWriter, r *http.Request) { var p Product //this is about parse the POST body request into a //golang struct if err := json.NewDecoder(r.Body).Decode(&p); err != nil { w.WriteHeader(http.StatusBadRequest) w.Write([]byte(err.Error())) return } fmt.Println(p.Name, p.Price) w.WriteHeader(http.StatusOK) w.Write([]byte("ok")) }
// Middleware function, which will be called for each request func (amw *authenticationMiddleware) Middleware(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { token := r.Header.Get("X-Session-Token")
if user, found := amw.tokenUsers[token]; found { // We found the token in our map log.Printf("Authenticated user %s\n", user) // Pass down the request to the next middleware (or final handler) next.ServeHTTP(w, r) } else { // Write an error and stop the handler chain http.Error(w, "Forbidden", http.StatusForbidden) } }) }
it support multiple middlewares
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
func loggingMiddleware(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { // Do stuff here log.Println("middleware is called:", r.RequestURI) // Call the next handler, which can be another middleware in the chain, or the final handler. next.ServeHTTP(w, r) }) }