package controllers import ( "context" "net/http" "time" "code.crute.us/mcrute/cloud-identity-broker/app/models" glecho "code.crute.us/mcrute/golib/echo" "code.crute.us/mcrute/golib/echo/controller" "github.com/labstack/echo/v4" ) type jsonListUser struct { Username string `bson:"_id" json:"key_id"` IsAdmin bool `json:"is_admin"` IsService bool `json:"is_service"` SelfLink string `json:"self"` Deleted *time.Time `json:"deleted,omitempty"` } type APIUserListHandler struct { store models.UserStore } func NewAPIUserListHandler(s models.UserStore) echo.HandlerFunc { al := &APIUserListHandler{store: s} h := &controller.ContentTypeNegotiatingHandler{ DefaultHandler: al.Handle, Handlers: map[string]echo.HandlerFunc{ contentTypeV2: al.Handle, }, } return h.Handle } func (h *APIUserListHandler) Handle(c echo.Context) error { users, err := h.store.List(context.Background()) if err != nil { return echo.ErrInternalServerError } out := map[string]*jsonListUser{} for _, v := range users { out[v.Username] = &jsonListUser{ Username: v.Username, IsAdmin: v.IsAdmin, IsService: v.IsService, SelfLink: glecho.URLFor(c, "/api/user", v.Username).String(), Deleted: v.Deleted, } } return c.JSON(http.StatusOK, out) }