Skip to content

Commit 40e4012

Browse files
authored
Merge pull request #17 from equestrian2296/master
Feat: Added token manager (redis cache)
2 parents 4d6c00f + 6498c59 commit 40e4012

File tree

4 files changed

+75
-14
lines changed

4 files changed

+75
-14
lines changed

cmd/api/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ func main() {
2020
logger.InitLogger()
2121
utils.LoadConfig()
2222
utils.InitCache()
23+
utils.InitTokenCache()
2324
utils.InitDB()
2425
validator.InitValidator()
2526

pkg/controllers/submission.go

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -49,20 +49,13 @@ func SubmitCode(c echo.Context) error {
4949
fmt.Println("CreateBatchSubmission error:", err)
5050
return c.JSON(http.StatusInternalServerError, map[string]string{"error": "Failed to create batch submission"})
5151
}
52-
53-
sub := utils.SubmissionInput{
54-
ID: submissionID,
55-
QuestionID: req.QuestionID,
56-
LanguageID: req.LanguageID,
57-
SourceCode: req.SourceCode,
58-
UserID: req.UserID,
59-
}
60-
if err := utils.SaveSubmission(sub); err != nil {
61-
return c.JSON(http.StatusInternalServerError, map[string]string{"error": "Failed to save submission record"})
62-
}
52+
for _, token := range tokens {
53+
if err := utils.TokenCache.Set(utils.Ctx, token, submissionID, 0).Err(); err != nil {
54+
fmt.Printf("Failed to cache token %s: %v\n", token, err)
55+
}
56+
}
6357

6458
return c.JSON(http.StatusOK, map[string]interface{}{
6559
"submission_id": submissionID,
66-
"tokens": tokens,
6760
})
68-
}
61+
}

pkg/utils/submission.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"fmt"
66
"github.com/CodeChefVIT/cookoff-10.0-be/pkg/db"
77
"github.com/google/uuid"
8-
8+
99
)
1010

1111
type SubmissionInput struct {
@@ -16,6 +16,7 @@ type SubmissionInput struct {
1616
UserID string
1717
}
1818
// place holder code -- renove when queue is setup
19+
1920
func SaveSubmission(sub SubmissionInput) error {
2021
if Queries == nil {
2122
return fmt.Errorf("DB Queries not initialized")

pkg/utils/tokenCache.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package utils
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"os"
7+
8+
"github.com/redis/go-redis/v9"
9+
)
10+
11+
var (
12+
TokenCache *redis.Client
13+
Ctx = context.Background()
14+
)
15+
16+
func InitTokenCache() {
17+
redisHost := os.Getenv("REDIS_HOST")
18+
if redisHost == "" {
19+
redisHost = "localhost"
20+
}
21+
22+
redisPort := os.Getenv("REDIS_PORT")
23+
if redisPort == "" {
24+
redisPort = "6379"
25+
}
26+
27+
TokenCache = redis.NewClient(&redis.Options{
28+
Addr: fmt.Sprintf("%s:%s", redisHost, redisPort),
29+
DB: 1,
30+
})
31+
32+
if err := TokenCache.Ping(Ctx).Err(); err != nil {
33+
panic(fmt.Sprintf("failed to connect to Redis token cache: %v", err))
34+
}
35+
}
36+
37+
func StoreToken(token string, submissionID string) error {
38+
if err := TokenCache.Set(Ctx, token, submissionID, 0).Err(); err != nil {
39+
return err
40+
}
41+
return TokenCache.SAdd(Ctx, fmt.Sprintf("submission_tokens:%s", submissionID), token).Err()
42+
}
43+
// get sub
44+
func GetSubmissionIDByToken(token string) (string, error) {
45+
submissionID, err := TokenCache.Get(Ctx, token).Result()
46+
if err == redis.Nil {
47+
return "", fmt.Errorf("token not found")
48+
}
49+
return submissionID, err
50+
}
51+
//can be used to delete all tokens
52+
func DeleteTokensBySubmissionID(submissionID string) error {
53+
setKey := fmt.Sprintf("submission_tokens:%s", submissionID)
54+
tokens, err := TokenCache.SMembers(Ctx, setKey).Result()
55+
if err != nil {
56+
return err
57+
}
58+
59+
if len(tokens) > 0 {
60+
if err := TokenCache.Del(Ctx, tokens...).Err(); err != nil {
61+
return err
62+
}
63+
}
64+
65+
return TokenCache.Del(Ctx, setKey).Err()
66+
}

0 commit comments

Comments
 (0)