Created
June 21, 2019 11:26
-
-
Save chris001177/9879cf60be17ceaa224bcb07fb671eea to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package handler | |
import ( | |
"fmt" | |
"net/http" | |
"strings" | |
) | |
// Declaring our local needs from the user db instance, | |
type UserDB interface { | |
UserRoleByID(id string) string | |
} | |
// ... and our local needs from the in memory permission storage. | |
type PermissionStorage interface { | |
RolePermissions(role string) []string | |
} | |
// Lastly, our handler cannot be purely functional, | |
// since it requires references to non singleton instances. | |
type UserPermissionsByID struct { | |
UserDB UserDB | |
PermissionsStorage PermissionStorage | |
} | |
func (u *UserPermissionsByID) ServeHTTP(w http.ResponseWriter, r *http.Request) { | |
id := r.URL.Query()["id"][0] | |
role := u.UserDB.UserRoleByID(id) | |
permissions := u.PermissionsStorage.RolePermissions(role) | |
fmt.Fprint(w, strings.Join(permissions, ", ")) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment