Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/johnpooch/start-game' into johnp…
Browse files Browse the repository at this point in the history
…ooch/add-webhooks-to-game
  • Loading branch information
= committed Jun 13, 2024
2 parents f889f34 + 1764495 commit 8b62594
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
41 changes: 41 additions & 0 deletions auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ const (
redirectURLKind = "RedirectURL"
superusersKind = "Superusers"
discordBotCredentialsKind = "DiscordBotCredentials"
discordBotTokenKind = "DiscordBotTokenKind"
prodKey = "prod"
)

Expand All @@ -75,6 +76,8 @@ var (
prodSuperusersLock = sync.RWMutex{}
prodDiscordBotCredentials *DiscordBotCredentials
prodDiscordBotCredentialsLock = sync.RWMutex{}
prodDiscordBotToken *DiscordBotToken
prodDiscordBotTokenLock = sync.RWMutex{}
router *mux.Router

RedirectURLResource *Resource
Expand All @@ -85,6 +88,10 @@ type DiscordBotCredentials struct {
Password string
}

type DiscordBotToken struct {
Token string
}

func getDiscordBotCredentialsKey(ctx context.Context) *datastore.Key {
return datastore.NewKey(ctx, discordBotCredentialsKind, prodKey, 0, nil)
}
Expand Down Expand Up @@ -119,6 +126,40 @@ func getDiscordBotCredentials(ctx context.Context) (*DiscordBotCredentials, erro
return prodDiscordBotCredentials, nil
}

func getDiscordBotTokenKey(ctx context.Context) *datastore.Key {
return datastore.NewKey(ctx, discordBotTokenKind, prodKey, 0, nil)
}

func SetDiscordBotToken(ctx context.Context, discordBotToken *DiscordBotToken) error {
return datastore.RunInTransaction(ctx, func(ctx context.Context) error {
currentDiscordBotToken := &DiscordBotToken{}
if err := datastore.Get(ctx, getDiscordBotTokenKey(ctx), currentDiscordBotToken); err == nil {
return HTTPErr{"DiscordBotToken already configured", http.StatusBadRequest}
}
if _, err := datastore.Put(ctx, getDiscordBotTokenKey(ctx), discordBotToken); err != nil {
return err
}
return nil
}, &datastore.TransactionOptions{XG: false})
}

func GetDiscordBotToken(ctx context.Context) (*DiscordBotToken, error) {
prodDiscordBotTokenLock.RLock()
if prodDiscordBotToken != nil {
defer prodDiscordBotTokenLock.RUnlock()
return prodDiscordBotToken, nil
}
prodDiscordBotTokenLock.RUnlock()
prodDiscordBotTokenLock.Lock()
defer prodDiscordBotTokenLock.Unlock()
foundDiscordBotToken := &DiscordBotToken{}
if err := datastore.Get(ctx, getDiscordBotTokenKey(ctx), foundDiscordBotToken); err != nil {
return nil, err
}
prodDiscordBotToken = foundDiscordBotToken
return prodDiscordBotToken, nil
}

func init() {
RedirectURLResource = &Resource{
Delete: deleteRedirectURL,
Expand Down
6 changes: 6 additions & 0 deletions game/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -660,6 +660,7 @@ type configuration struct {
SendGrid *auth.SendGrid
Superusers *auth.Superusers
DiscordBotCredentials *auth.DiscordBotCredentials
DiscordBotToken *auth.DiscordBotToken
}

func handleConfigure(w ResponseWriter, r Request) error {
Expand Down Expand Up @@ -694,6 +695,11 @@ func handleConfigure(w ResponseWriter, r Request) error {
return err
}
}
if conf.DiscordBotToken != nil {
if err := auth.SetDiscordBotToken(ctx, conf.DiscordBotToken); err != nil {
return err
}
}
return nil
}

Expand Down

0 comments on commit 8b62594

Please sign in to comment.