Add endpoint to retrieve user by userId

This commit is contained in:
Marcos Lilljedahl
2017-10-16 19:43:56 -03:00
parent 1b0d363ffe
commit 4850a7b496
3 changed files with 25 additions and 1 deletions

View File

@@ -80,6 +80,7 @@ func Register(extend HandlerExtender) {
}).Methods("GET")
r.HandleFunc("/users/me", LoggedInUser).Methods("GET")
r.HandleFunc("/users/{userId}", GetUser).Methods("GET")
r.HandleFunc("/oauth/providers", ListProviders).Methods("GET")
r.HandleFunc("/oauth/providers/{provider}/login", Login).Methods("GET")
r.HandleFunc("/oauth/providers/{provider}/callback", LoginCallback).Methods("GET")

23
handlers/user.go Normal file
View File

@@ -0,0 +1,23 @@
package handlers
import (
"encoding/json"
"log"
"net/http"
"github.com/gorilla/mux"
)
func GetUser(rw http.ResponseWriter, req *http.Request) {
vars := mux.Vars(req)
userId := vars["userId"]
u, err := core.UserGet(userId)
if err != nil {
log.Println(err)
rw.WriteHeader(http.StatusInternalServerError)
return
}
json.NewEncoder(rw).Encode(u)
}

View File

@@ -351,7 +351,7 @@ func (store *storage) UserPut(user *types.User) error {
store.db.UsersByProvider[fmt.Sprintf("%s_%s", user.Provider, user.ProviderUserId)] = user.Id
store.db.Users[user.Id] = user
return nil
return store.save()
}
func (store *storage) UserGet(id string) (*types.User, error) {
store.rw.Lock()