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

create a func to refresh unlimited tokens #27

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
33 changes: 32 additions & 1 deletion config/config.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package config

import (
"encoding/json"
"log"
"os"
"time"

"github.com/caarlos0/env/v6"
)
Expand All @@ -13,7 +16,7 @@ var Config = struct {
CorsEnable bool `env:"CORS_ENABLE"`
HeartbeatInterval int `env:"HEARTBEAT_INTERVAL" envDefault:"10"`
RPSLimit int `env:"RPS_LIMIT" envDefault:"1"`
RateLimitsByPassToken []string `env:"RATE_LIMITS_BY_PASS_TOKEN"`
RateLimitsByPassToken []string `env:"RATE_LIMITS_BY_PASS_TOKEN"` // TODO: remove this env, read from file
ConnectionsLimit int `env:"CONNECTIONS_LIMIT" envDefault:"50"`
SelfSignedTLS bool `env:"SELF_SIGNED_TLS" envDefault:"false"`
}{}
Expand All @@ -22,4 +25,32 @@ func LoadConfig() {
if err := env.Parse(&Config); err != nil {
log.Fatalf("config parsing failed: %v\n", err)
}

go refreshUnlimitedTokens()
}

func refreshUnlimitedTokens() {
type UnlimitedTokens struct {
Tokens []string `json:"tokens"`
}
refresh := func() []string {
file, err := os.ReadFile("config/unlimited_tokens.json")
if err != nil {
log.Printf("failed to read unlimited tokens file: %v", err)
return nil
}
var tokens UnlimitedTokens
if err = json.Unmarshal(file, &tokens); err != nil {
log.Printf("failed to convert unlimited tokens: %v", err)
return nil
}
return tokens.Tokens
}
for {
tokens := refresh()
if tokens != nil {
Config.RateLimitsByPassToken = tokens
}
time.Sleep(time.Minute * 5)
}
}
3 changes: 3 additions & 0 deletions config/unlimited_tokens.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"tokens": []
}