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 main | |
import ( | |
"net/http" | |
"time" | |
) | |
// As noted above, since we plan to only have one instance | |
// for those 3 services, we'll declare a singleton instance, | |
// and make sure we only use them to access those services. |
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 main | |
// We use interfaces as the types of our database instances | |
// to make it possible to write tests and use mock implementations. | |
type userDB interface { | |
userRoleByID(id string) string | |
} | |
// Note the naming `someConfigDB`. In actual cases we use | |
// some DB implementation and name our structs accordingly. |
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 main | |
import ( | |
"fmt" | |
"net/http" | |
"strings" | |
) | |
func UserPermissionsByID(w http.ResponseWriter, r *http.Request) { | |
id := r.URL.Query()["id"][0] |
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 main | |
// Note how the main package is the only one importing | |
// packages other than the definition package. | |
import ( | |
"github.com/myproject/config" | |
"github.com/myproject/database" | |
"github.com/myproject/definition" | |
"github.com/myproject/handler" | |
"net/http" |
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 definition | |
// Note that in this approach both the singleton instance | |
// and its interface type are declared in the definition | |
// package. Make sure this package does not contain any | |
// logic, otherwise it might need to import other packages | |
// and its neutral nature is compromised. | |
var ( | |
UserDBInstance UserDB | |
ConfigDBInstance ConfigDB |
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 definition | |
var RolePermissions map[string][]string |
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 database | |
type SomeUserDB struct{} | |
func (db *SomeUserDB) UserRoleByID(id string) string { | |
// implementation | |
} |
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 database | |
type SomeConfigDB struct{} | |
func (db *SomeConfigDB) AllPermissions() map[string][]string { | |
// implementation | |
} |
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 ( | |
"github.com/myproject/definition" | |
"time" | |
) | |
// Since the definition package must not contain any logic, | |
// managing configuration is implemented in a config package. | |
func InitPermissions() { |
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" | |
"github.com/myproject/definition" | |
"net/http" | |
"strings" | |
) | |
func UserPermissionsByID(w http.ResponseWriter, r *http.Request) { |
OlderNewer