Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Invoke webhooks on game start and phase start #196

Merged
merged 7 commits into from
Jun 17, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add DiscordBotToken to datastore
  • Loading branch information
= committed Jun 12, 2024
commit 1764495c81580066120cea225f6e84353a834df4
41 changes: 41 additions & 0 deletions auth/auth.go
Original file line number Diff line number Diff line change
@@ -59,6 +59,7 @@ const (
redirectURLKind = "RedirectURL"
superusersKind = "Superusers"
discordBotCredentialsKind = "DiscordBotCredentials"
discordBotTokenKind = "DiscordBotTokenKind"
prodKey = "prod"
)

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

RedirectURLResource *Resource
@@ -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)
}
@@ -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,
6 changes: 6 additions & 0 deletions game/handler.go
Original file line number Diff line number Diff line change
@@ -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 {
@@ -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
}

Loading