Created
June 21, 2019 11:24
-
-
Save chris001177/86028358341be9481b265f16baac4777 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 config | |
import ( | |
"time" | |
) | |
// Here we declare an interface representing our local | |
// needs from the configuration db, namely, | |
// the `AllPermissions` method. | |
type PermissionDB interface { | |
AllPermissions() map[string][]string // maps from role to its permissions | |
} | |
// Then we export a service than will provide the | |
// permissions from memory, to use it, another package | |
// will have to declare a local interface. | |
type PermissionStorage struct { | |
permissions map[string][]string | |
} | |
func NewPermissionStorage(db PermissionDB) *PermissionStorage { | |
s := &PermissionStorage{} | |
s.permissions = db.AllPermissions() | |
go func() { | |
for { | |
time.Sleep(time.Hour) | |
s.permissions = db.AllPermissions() | |
} | |
}() | |
return s | |
} | |
func (s *PermissionStorage) RolePermissions(role string) []string { | |
return s.permissions[role] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment