github.com/fanux/shipyard@v0.0.0-20161009071005-6515ce223235/controller/api/roles.go (about) 1 package api 2 3 import ( 4 "encoding/json" 5 "net/http" 6 7 "github.com/gorilla/mux" 8 ) 9 10 func (a *Api) roles(w http.ResponseWriter, r *http.Request) { 11 w.Header().Set("content-type", "application/json") 12 13 roles, err := a.manager.Roles() 14 if err != nil { 15 http.Error(w, err.Error(), http.StatusInternalServerError) 16 return 17 } 18 if err := json.NewEncoder(w).Encode(roles); err != nil { 19 http.Error(w, err.Error(), http.StatusInternalServerError) 20 return 21 } 22 } 23 24 func (a *Api) role(w http.ResponseWriter, r *http.Request) { 25 w.Header().Set("content-type", "application/json") 26 27 vars := mux.Vars(r) 28 name := vars["name"] 29 role, err := a.manager.Role(name) 30 if err != nil { 31 http.Error(w, err.Error(), http.StatusInternalServerError) 32 return 33 } 34 if err := json.NewEncoder(w).Encode(role); err != nil { 35 http.Error(w, err.Error(), http.StatusInternalServerError) 36 return 37 } 38 }