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
// https://github.com/zekroTJA/shinpuru/blob/b0900fdb90d4b69a7d8e77ac73c12da25e552a0d/internal/core/webserver/v1/controllers/guilds.go#L32-L36 | |
func (c *GuildsController) Setup(container di.Container, router fiber.Router) { | |
c.session = container.Get(static.DiDiscordSession).(*discordgo.Session) | |
c.cfg = container.Get(static.DiConfig).(*config.Config) | |
c.db = container.Get(static.DiDatabase).(database.Database) | |
c.pmw = container.Get(static.DiPermissionMiddleware).(*middleware.PermissionsMiddleware) | |
// ... |
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
// https://github.com/zekroTJA/shinpuru/blob/d52c1c18f8a994a6a70c9a991617ab3dd4456010/internal/core/webserver/webserver.go#L70-L75 | |
// New creates a new instance of WebServer consuming the passed | |
// database provider, storage provider, discordgo session, command | |
// handler, life cycle timer and configuration. | |
func New(db database.Database, st storage.Storage, s *discordgo.Session, | |
cmd shireikan.Handler, lct *lctimer.LifeCycleTimer, config *config.Config, | |
pmw *middleware.PermissionsMiddleware, ota *onetimeauth.OneTimeAuth) (ws *WebServer, err error) { | |
// ... |
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" | |
type DBService interface { | |
GetUser(email string) string | |
} | |
type EMailService interface { | |
SendMail(address, content string) | |
} | |
type MyDBService struct{} | |
func (db *MyDBService) GetUser(email string) string { | |
return "john.doe" | |
} | |
type MyEMailService struct{} | |
func (e *MyEMailService) SendMail(address, content string) { | |
fmt.Printf("Send mail to '%s' with content '%s'\n", address, content) | |
} | |
type MyAPIService struct { | |
db DBService | |
mail EMailService | |
} | |
func NewMyAPIService(db DBService, mail EMailService) *MyAPIService { | |
return &MyAPIService{db, mail} | |
} | |
func (api *MyAPIService) ResetPassword(address string) { | |
username := api.db.GetUser(address) | |
if username != "" { | |
api.mail.SendMail(address, | |
fmt.Sprintf("Password reset for user '%s': ...", username)) | |
} | |
} | |
func main() { | |
db := new(MyDBService) | |
mail := new(MyEMailService) | |
api := NewMyAPIService(db, mail) | |
api.ResetPassword("jhon.doe@example.com") | |
} |
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" | |
type MyDBService struct{} | |
func (db *MyDBService) GetUser(email string) string { | |
return "john.doe" | |
} | |
type MyEMailService struct{} | |
func (e *MyEMailService) SendMail(address, content string) { | |
fmt.Printf("Send mail to '%s' with content '%s'\n", address, content) | |
} | |
type MyAPIService struct { | |
db *MyDBService | |
mail *MyEMailService | |
} | |
func NewMyAPIService(db *MyDBService, mail *MyEMailService) *MyAPIService { | |
return &MyAPIService{db, mail} | |
} | |
func (api *MyAPIService) ResetPassword(address string) { | |
username := api.db.GetUser(address) | |
if username != "" { | |
api.mail.SendMail(address, | |
fmt.Sprintf("Password reset for user '%s': ...", username)) | |
} | |
} | |
func main() { | |
db := new(MyDBService) | |
mail := new(MyEMailService) | |
api := NewMyAPIService(db, mail) | |
api.ResetPassword("jhon.doe@example.com") | |
} |
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
// https://github.com/zekroTJA/shinpuru/blob/b0900fdb90d4b69a7d8e77ac73c12da25e552a0d/cmd/shinpuru/main.go | |
func main() { | |
// Parse command line flags | |
flag.Parse() | |
// Initialize dependency injection builder | |
diBuilder, _ := di.NewBuilder() | |
// ... | |
// Initialize config | |
diBuilder.Add(di.Def{ | |
Name: static.DiConfig, | |
Build: func(ctn di.Container) (interface{}, error) { | |
return inits.InitConfig(*flagConfigLocation, ctn), nil | |
}, | |
}) | |
// Initialize database middleware and shutdown routine | |
diBuilder.Add(di.Def{ | |
Name: static.DiDatabase, | |
Build: func(ctn di.Container) (interface{}, error) { | |
return inits.InitDatabase(ctn), nil | |
}, | |
Close: func(obj interface{}) error { | |
database := obj.(database.Database) | |
util.Log.Info("Shutting down database connection...") | |
database.Close() | |
return nil | |
}, | |
}) | |
// ... | |
// Initialize permissions command handler middleware | |
diBuilder.Add(di.Def{ | |
Name: static.DiPermissionMiddleware, | |
Build: func(ctn di.Container) (interface{}, error) { | |
return inits.InitPermissionMiddleware(ctn), nil | |
}, | |
}) | |
// ... | |
// Initialize discord bot session and shutdown routine | |
diBuilder.Add(di.Def{ | |
Name: static.DiDiscordSession, | |
Build: func(ctn di.Container) (interface{}, error) { | |
return inits.InitDiscordBotSession(ctn), nil | |
}, | |
Close: func(obj interface{}) error { | |
session := obj.(*discordgo.Session) | |
util.Log.Info("Shutting down bot session...") | |
session.Close() | |
return nil | |
}, | |
}) | |
// ... | |
// Build dependency injection container | |
ctn := diBuilder.Build() | |
// ... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment