From 5f047b436646e05c5e1dfc38480e5b6d2a670f96 Mon Sep 17 00:00:00 2001 From: John Burke Date: Wed, 19 Aug 2020 02:34:44 -0700 Subject: [PATCH] Fix id being empty for all entities, tidy go modules, switch to encoding/json --- entities/bot.go | 22 ++++++++++++++++++++-- entities/cachePopulator.go | 7 +++---- entities/response.go | 7 ++++--- entities/server.go | 21 +++++++++++++++++++-- entities/template.go | 25 +++++++++++++++++++++---- entities/user.go | 21 +++++++++++++++++++-- go.mod | 3 --- go.sum | 20 ++++++-------------- ratelimit/ratelimit.go | 13 +++++++------ routes/bots.go | 7 ++++--- util/util.go | 2 -- 11 files changed, 103 insertions(+), 45 deletions(-) diff --git a/entities/bot.go b/entities/bot.go index 2a07c60..d0b2569 100644 --- a/entities/bot.go +++ b/entities/bot.go @@ -2,6 +2,7 @@ package entities import ( "context" + "encoding/json" "github.com/discordextremelist/api/util" log "github.com/sirupsen/logrus" "go.mongodb.org/mongo-driver/bson" @@ -35,9 +36,11 @@ type WidgetBot struct { } type Bot struct { + MongoID string `json:"_id,omitempty"` ID string `bson:"_id" json:"id"` Name string `json:"name"` Prefix string `json:"prefix"` + Library string `json:"library"` Tags []string `json:"tags"` VanityURL string `json:"vanityUrl"` ServerCount int `json:"serverCount"` @@ -89,6 +92,7 @@ func LookupBot(id string, clean bool) (error, *Bot) { if err == nil { if redisBot == "" { err, bot := mongoLookupBot(id) + bot.MongoID = "" if err != nil { if err == mongo.ErrNoDocuments { return err, nil @@ -103,11 +107,17 @@ func LookupBot(id string, clean bool) (error, *Bot) { } } bot := &Bot{} - err = util.Json.UnmarshalFromString(redisBot, &bot) + err = json.Unmarshal([]byte(redisBot), &bot) if err != nil { log.Errorf("Json parsing failed for LookupBot(%s): %v", id, err.Error()) return LookupError, nil } else { + if bot.ID == "" { + bot.ID = bot.MongoID + bot.MongoID = "" + } else { + bot.MongoID = "" + } if clean { bot = CleanupBot(fakeRank, bot) } @@ -115,6 +125,7 @@ func LookupBot(id string, clean bool) (error, *Bot) { } } else { err, bot := mongoLookupBot(id) + bot.MongoID = "" if err != nil { if err == mongo.ErrNoDocuments { return err, nil @@ -136,10 +147,16 @@ func GetAllBots(clean bool) (error, []Bot) { var actual []Bot for _, str := range redisBots { bot := Bot{} - err = util.Json.UnmarshalFromString(str, &bot) + err = json.Unmarshal([]byte(str), &bot) if err != nil { continue } + if bot.ID == "" { + bot.ID = bot.MongoID + bot.MongoID = "" + } else { + bot.MongoID = "" + } if clean { bot = *CleanupBot(fakeRank, &bot) } @@ -156,6 +173,7 @@ func GetAllBots(clean bool) (error, []Bot) { for cursor.Next(context.TODO()) { bot := Bot{} err = cursor.Decode(&bot) + bot.MongoID = "" if err != nil { continue } diff --git a/entities/cachePopulator.go b/entities/cachePopulator.go index 6f4d1af..ca6b8e7 100644 --- a/entities/cachePopulator.go +++ b/entities/cachePopulator.go @@ -2,6 +2,7 @@ package entities import ( "context" + "encoding/json" "github.com/discordextremelist/api/util" "github.com/sirupsen/logrus" "go.mongodb.org/mongo-driver/bson" @@ -25,21 +26,19 @@ func PopulateDevCache() { for cursor.Next(context.TODO()) { var entity bson.M err = cursor.Decode(&entity) - entity["id"] = entity["_id"].(string) mongoEntities = append(mongoEntities, entity) } _ = cursor.Close(context.TODO()) var toSet []string for _, entity := range mongoEntities { id := entity["_id"].(string) - delete(entity, "_id") - marshaled, err := util.Json.MarshalToString(&entity) + marshaled, err := json.Marshal(&entity) if err != nil { logrus.Errorf("Failed to marshal an entity for cache %s (col: %s, err: %s)", id, col, err.Error()) continue } toSet = append(toSet, id) - toSet = append(toSet, marshaled) + toSet = append(toSet, string(marshaled)) } err = util.Database.Redis.HMSet(context.TODO(), col, toSet).Err() if err != nil { diff --git a/entities/response.go b/entities/response.go index 8c59687..58d3340 100644 --- a/entities/response.go +++ b/entities/response.go @@ -1,6 +1,7 @@ package entities import ( + "encoding/json" "errors" "github.com/discordextremelist/api/util" "net/http" @@ -96,7 +97,7 @@ var ( func WriteJson(status int, writer http.ResponseWriter, v interface{}) { writer.Header().Set("Content-Type", "application/json") writer.WriteHeader(status) - util.Json.NewEncoder(writer).Encode(v) + json.NewEncoder(writer).Encode(v) } func WriteErrorResponse(w http.ResponseWriter, err error) { @@ -122,7 +123,7 @@ func WriteTemplateResponse(w http.ResponseWriter, template *ServerTemplate) { func WriteNotImplementedResponse(w http.ResponseWriter) { w.Header().Set("Content-Type", "application/json") w.WriteHeader(501) - util.Json.NewEncoder(w).Encode(notImplemented) + json.NewEncoder(w).Encode(notImplemented) } // DELAPI_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx-000000000000000000 @@ -164,5 +165,5 @@ func NotFound(w http.ResponseWriter, _ *http.Request) { func BadAuth(w http.ResponseWriter, _ *http.Request) { w.Header().Set("Content-Type", "application/json") w.WriteHeader(403) - util.Json.NewEncoder(w).Encode(NoAuthError) + json.NewEncoder(w).Encode(NoAuthError) } diff --git a/entities/server.go b/entities/server.go index 81a36e5..0915dc6 100644 --- a/entities/server.go +++ b/entities/server.go @@ -2,6 +2,7 @@ package entities import ( "context" + "encoding/json" "github.com/discordextremelist/api/util" log "github.com/sirupsen/logrus" "go.mongodb.org/mongo-driver/bson" @@ -15,6 +16,7 @@ type ServerLinks struct { } type Server struct { + MongoID string `json:"_id,omitempty"` ID string `bson:"_id" json:"id"` InviteCode string `json:"inviteCode,omitempty"` Name string `json:"name"` @@ -54,6 +56,7 @@ func LookupServer(id string, clean bool) (error, *Server) { if err == nil { if redisServer == "" { err, server := mongoLookupServer(id) + server.MongoID = "" if err != nil { if err == mongo.ErrNoDocuments { return err, nil @@ -68,7 +71,7 @@ func LookupServer(id string, clean bool) (error, *Server) { } } server := &Server{} - err = util.Json.UnmarshalFromString(redisServer, &server) + err = json.Unmarshal([]byte(redisServer), &server) if err != nil { if err == mongo.ErrNoDocuments { return err, nil @@ -76,6 +79,12 @@ func LookupServer(id string, clean bool) (error, *Server) { log.Errorf("Json parsing failed for LookupServer(%s): %v", id, err.Error()) return LookupError, nil } else { + if server.ID == "" { + server.ID = server.MongoID + server.MongoID = "" + } else { + server.MongoID = "" + } if clean { server = CleanupServer(fakeRank, server) } @@ -83,6 +92,7 @@ func LookupServer(id string, clean bool) (error, *Server) { } } else { err, server := mongoLookupServer(id) + server.MongoID = "" if err != nil { if err == mongo.ErrNoDocuments { return err, nil @@ -104,7 +114,13 @@ func GetAllServers(clean bool) (error, []Server) { var actual []Server for _, str := range redisServers { server := Server{} - err = util.Json.UnmarshalFromString(str, &server) + err = json.Unmarshal([]byte(str), &server) + if server.ID == "" { + server.ID = server.MongoID + server.MongoID = "" + } else { + server.MongoID = "" + } if err != nil { continue } @@ -124,6 +140,7 @@ func GetAllServers(clean bool) (error, []Server) { for cursor.Next(context.TODO()) { server := Server{} err = cursor.Decode(&server) + server.MongoID = "" if err != nil { continue } diff --git a/entities/template.go b/entities/template.go index a88aa16..feb08d3 100644 --- a/entities/template.go +++ b/entities/template.go @@ -2,6 +2,7 @@ package entities import ( "context" + "encoding/json" "github.com/discordextremelist/api/util" log "github.com/sirupsen/logrus" "go.mongodb.org/mongo-driver/bson" @@ -44,6 +45,7 @@ type GuildChannel struct { } type ServerTemplate struct { + MongoID string `json:"_id,omitempty"` ID string `bson:"_id" json:"id"` Name string `json:"name"` Region string `json:"region"` @@ -81,6 +83,7 @@ func LookupTemplate(id string) (error, *ServerTemplate) { if err == nil { if redisTemplate == "" { err, template := mongoLookupTemplate(id) + template.MongoID = "" if err != nil { if err == mongo.ErrNoDocuments { return err, nil @@ -92,15 +95,22 @@ func LookupTemplate(id string) (error, *ServerTemplate) { } } template := &ServerTemplate{} - err = util.Json.UnmarshalFromString(redisTemplate, &template) + err = json.Unmarshal([]byte(redisTemplate), &template) if err != nil { log.Errorf("Json parsing failed for LookupTemplate(%s): %v", id, err.Error()) return LookupError, nil } else { + if template.ID == "" { + template.ID = template.MongoID + template.MongoID = "" + } else { + template.MongoID = "" + } return nil, template } } else { - err, bot := mongoLookupTemplate(id) + err, template := mongoLookupTemplate(id) + template.MongoID = "" if err != nil { if err == mongo.ErrNoDocuments { return err, nil @@ -108,7 +118,7 @@ func LookupTemplate(id string) (error, *ServerTemplate) { log.Errorf("Fallback for MongoDB failed for LookupTemplate(%s): %v", id, err.Error()) return LookupError, nil } else { - return nil, bot + return nil, template } } } @@ -119,7 +129,13 @@ func GetAllTemplates() (error, []ServerTemplate) { var actual []ServerTemplate for _, str := range redisTemplates { template := ServerTemplate{} - err = util.Json.UnmarshalFromString(str, &template) + err = json.Unmarshal([]byte(str), &template) + if template.ID == "" { + template.ID = template.MongoID + template.MongoID = "" + } else { + template.MongoID = "" + } if err != nil { continue } @@ -136,6 +152,7 @@ func GetAllTemplates() (error, []ServerTemplate) { for cursor.Next(context.TODO()) { template := ServerTemplate{} err = cursor.Decode(&template) + template.MongoID = "" if err != nil { continue } diff --git a/entities/user.go b/entities/user.go index 54d1e13..d598804 100644 --- a/entities/user.go +++ b/entities/user.go @@ -2,6 +2,7 @@ package entities import ( "context" + "encoding/json" "github.com/discordextremelist/api/util" log "github.com/sirupsen/logrus" "go.mongodb.org/mongo-driver/bson" @@ -87,6 +88,7 @@ type UserProfileLinks struct { } type User struct { + MongoID string `json:"_id,omitempty"` ID string `bson:"_id" json:"id"` Token string `json:"token,omitempty"` Name string `json:"name"` @@ -136,6 +138,7 @@ func LookupUser(id string, clean bool) (error, *User) { if err == nil { if redisUser == "" { err, user := mongoLookupUser(id) + user.MongoID = "" if err != nil { if err == mongo.ErrNoDocuments { return err, nil @@ -150,11 +153,17 @@ func LookupUser(id string, clean bool) (error, *User) { } } user := &User{} - err = util.Json.UnmarshalFromString(redisUser, &user) + err = json.Unmarshal([]byte(redisUser), &user) if err != nil { log.Errorf("Json parsing failed for LookupUser(%s): %v", id, err.Error()) return LookupError, nil } else { + if user.ID == "" { + user.ID = user.MongoID + user.MongoID = "" + } else { + user.MongoID = "" + } if clean { user = CleanupUser(fakeRank, user) } @@ -162,6 +171,7 @@ func LookupUser(id string, clean bool) (error, *User) { } } else { err, user := mongoLookupUser(id) + user.MongoID = "" if err != nil { if err == mongo.ErrNoDocuments { return err, nil @@ -183,7 +193,13 @@ func GetAllUsers(clean bool) (error, []User) { var actual []User for _, str := range redisUsers { user := User{} - err = util.Json.UnmarshalFromString(str, &user) + err = json.Unmarshal([]byte(str), &user) + if user.ID == "" { + user.ID = user.MongoID + user.MongoID = "" + } else { + user.MongoID = "" + } if err != nil { continue } @@ -203,6 +219,7 @@ func GetAllUsers(clean bool) (error, []User) { for cursor.Next(context.TODO()) { user := User{} err = cursor.Decode(&user) + user.MongoID = "" if err != nil { continue } diff --git a/go.mod b/go.mod index c1cc238..4160700 100644 --- a/go.mod +++ b/go.mod @@ -6,10 +6,7 @@ require ( github.com/go-chi/chi v4.1.2+incompatible github.com/go-redis/redis/v8 v8.0.0-beta.7 github.com/joho/godotenv v1.3.0 - github.com/json-iterator/go v1.1.10 github.com/klauspost/compress v1.10.5 // indirect - github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect - github.com/modern-go/reflect2 v1.0.1 // indirect github.com/sirupsen/logrus v1.6.0 go.mongodb.org/mongo-driver v1.4.0 go.opentelemetry.io/otel v0.10.0 // indirect diff --git a/go.sum b/go.sum index db6ef12..b1dbde5 100644 --- a/go.sum +++ b/go.sum @@ -24,9 +24,6 @@ github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMo github.com/go-chi/chi v4.1.2+incompatible h1:fGFk2Gmi/YKXk0OmGfBh0WgmN3XB8lVnEyNz34tQRec= github.com/go-chi/chi v4.1.2+incompatible/go.mod h1:eB3wogJHnLi3x/kFX2A+IbTBlXxmMeXJVKy9tTv1XzQ= github.com/go-gl/glfw/v3.3/glfw v0.0.0-20200222043503-6f7a984d4dc4/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8= -github.com/go-redis/redis v6.15.9+incompatible h1:K0pv1D7EQUjfyoMql+r/jZqCLizCGKFlFgcHWWmHQjg= -github.com/go-redis/redis/v7 v7.4.0 h1:7obg6wUoj05T0EpY0o8B59S9w5yeMWql7sw2kwNW1x4= -github.com/go-redis/redis/v7 v7.4.0/go.mod h1:JDNMw23GTyLNC4GZu9njt15ctBQVn7xjRfnwdHj/Dcg= github.com/go-redis/redis/v8 v8.0.0-beta.7 h1:4HiY+qfsyz8OUr9zyAP2T1CJ0SFRY4mKFvm9TEznuv8= github.com/go-redis/redis/v8 v8.0.0-beta.7/go.mod h1:FGJAWDWFht1sQ4qxyJHZZbVyvnVcKQN0E3u5/5lRz+g= github.com/go-sql-driver/mysql v1.5.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg= @@ -67,6 +64,7 @@ github.com/golang/protobuf v1.4.0-rc.2/go.mod h1:LlEzMj4AhA7rCAGe4KMBDvJI+AwstrU github.com/golang/protobuf v1.4.0-rc.4.0.20200313231945-b860323f09d0/go.mod h1:WU3c8KckQ9AFe+yFwt9sWVRKCVIyN9cPHBJSNnbL67w= github.com/golang/protobuf v1.4.0 h1:oOuy+ugB+P/kBdUnG5QaMXSIyJ1q38wWSojYCb3z5VQ= github.com/golang/protobuf v1.4.0/go.mod h1:jodUvKwWbYaEsadDk5Fwe5c77LiNKVO9IDvqG2KuDX0= +github.com/golang/protobuf v1.4.2 h1:+Z5KGCizgyZCbGh1KZqA0fcLLkwbsjIzS4aV2v7wJX0= github.com/golang/protobuf v1.4.2/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI= github.com/golang/snappy v0.0.1 h1:Qgr9rKW7uDUkrbSmQeiDsGa8SjGyCOGtuasMWwvp2P4= github.com/golang/snappy v0.0.1/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= @@ -76,6 +74,7 @@ github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMyw github.com/google/go-cmp v0.4.0 h1:xsAVV57WRhGj6kEIi8ReJzQlHHqcBYCElAvkovg3B/4= github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.5.1 h1:JFrFEBb2xKufg6XkJsJr+WbKb4FQlURi5RUcBveYu9k= github.com/google/go-cmp v0.5.1/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI= @@ -85,8 +84,6 @@ github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af h1:pmfjZENx5i github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k= github.com/joho/godotenv v1.3.0 h1:Zjp+RcGpHhGlrMbJzXTrZZPrWj+1vfm90La1wgB6Bhc= github.com/joho/godotenv v1.3.0/go.mod h1:7hK45KPybAkOC6peb+G5yklZfMxEjkZhHbwpqxOKXbg= -github.com/json-iterator/go v1.1.10 h1:Kz6Cvnvv2wGdaG/V8yMvfkmNiXq9Ya2KUv4rouJJr68= -github.com/json-iterator/go v1.1.10/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= github.com/karrick/godirwalk v1.8.0/go.mod h1:H5KPZjojv4lE+QYImBI8xVtrBRgYrIVsaRPx4tDPEn4= github.com/karrick/godirwalk v1.10.3/go.mod h1:RoGL9dQei4vP9ilrpETWE8CLOZ1kiN0LhBygSwrAsHA= github.com/klauspost/compress v1.9.5 h1:U+CaK85mrNNb4k8BNOfgJtJ/gr6kswUCFj6miSzVC6M= @@ -105,14 +102,6 @@ github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/markbates/oncer v0.0.0-20181203154359-bf2de49a0be2/go.mod h1:Ld9puTsIW75CHf65OeIOkyKbteujpZVXDpWK6YGZbxE= github.com/markbates/safe v1.0.1/go.mod h1:nAqgmRi7cY2nqMc92/bSEeQA+R4OheNU2T1kNSCBdG0= -github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc= -github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= -github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg= -github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= -github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742 h1:Esafd1046DLDQ0W1YjYsBW+p8U2u7vzgW2SQVmlNazg= -github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= -github.com/modern-go/reflect2 v1.0.1 h1:9f412s+6RmYXLWZSEzVVgPGK7C2PphHj5RJrvfx9AWI= -github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe/go.mod h1:wL8QJuTMNUDYhXwkmfOly8iTdp5TEcJFWZD2D7SIkUc= github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= github.com/onsi/ginkgo v1.10.1 h1:q/mM8GF/n0shIN8SaAZ0V+jnLPzen6WIVZdiwrRlMlo= @@ -146,6 +135,7 @@ github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXf github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= +github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0= github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/tidwall/pretty v1.0.0 h1:HsD+QiTn7sK6flMKIvNmpqz1qrpP3Ps6jOKIKMooyg4= github.com/tidwall/pretty v1.0.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk= @@ -238,6 +228,7 @@ google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9Ywl google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc= google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55/go.mod h1:DMBHOl98Agz4BDEuKkezgsaosCRResVns1a3J2ZsMNc= +google.golang.org/genproto v0.0.0-20191009194640-548a555dbc03 h1:4HYDjxeNXAOTv3o1N2tjo8UUSlhQgAD52FVkwxnWgM8= google.golang.org/genproto v0.0.0-20191009194640-548a555dbc03/go.mod h1:n3cpQtvxv34hfy77yVDNjmbRyujviMdxYliBSkLhpCc= google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c= google.golang.org/grpc v1.23.0/go.mod h1:Y5yQAOtifL1yxbo5wqy6BxZv8vAUGQwXBOALyacEbxg= @@ -252,6 +243,7 @@ google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQ google.golang.org/protobuf v1.20.1-0.20200309200217-e05f789c0967/go.mod h1:A+miEFZTKqfCUM6K7xSMQL9OKL/b6hQv+e19PK+JZNE= google.golang.org/protobuf v1.21.0 h1:qdOKuR/EIArgaWNjetjgTzgVTAZ+S/WXVrq9HW9zimw= google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzikPIcrTAo= +google.golang.org/protobuf v1.23.0 h1:4MY060fB1DLGMB/7MBTLnwQUY6+F09GEiz6SsrNqyzM= google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= @@ -264,10 +256,10 @@ gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkep gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw= gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= -gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.7/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10= gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= diff --git a/ratelimit/ratelimit.go b/ratelimit/ratelimit.go index b7e8339..870318e 100644 --- a/ratelimit/ratelimit.go +++ b/ratelimit/ratelimit.go @@ -2,6 +2,7 @@ package ratelimit import ( "context" + "encoding/json" "github.com/discordextremelist/api/entities" "github.com/discordextremelist/api/util" log "github.com/sirupsen/logrus" @@ -90,7 +91,7 @@ func (r *Ratelimiter) cacheAll() int { } for key, val := range results { ratelimit := &Ratelimit{} - _ = util.Json.UnmarshalFromString(val, ratelimit) + _ = json.Unmarshal([]byte(val), ratelimit) r.overwrite(key, *ratelimit) } return len(results) @@ -115,8 +116,8 @@ func (r *Ratelimiter) cacheRatelimit(key string, ratelimit Ratelimit) { return } if util.Database.IsRedisOpen() { - str, _ := util.Json.MarshalToString(&ratelimit) - util.Database.Redis.HMSet(context.TODO(), r.RPrefix, key, str) + byteArr, _ := json.Marshal(&ratelimit) + util.Database.Redis.HMSet(context.TODO(), r.RPrefix, key, string(byteArr)) } } @@ -207,9 +208,9 @@ func (r *Ratelimiter) Ratelimit(next http.Handler) http.Handler { headers.Set("Content-Type", "application/json") writer.WriteHeader(http.StatusForbidden) if !ratelimit.TempBan { - util.Json.NewEncoder(writer).Encode(entities.PermBannedError) + json.NewEncoder(writer).Encode(entities.PermBannedError) } else { - util.Json.NewEncoder(writer).Encode(entities.TempBannedError) + json.NewEncoder(writer).Encode(entities.TempBannedError) } return } @@ -218,7 +219,7 @@ func (r *Ratelimiter) Ratelimit(next http.Handler) http.Handler { headers.Set("Content-Type", "application/json") headers.Set("Retry-After", strconv.FormatInt(r.NextReset.Sub(time.Now()).Milliseconds(), 10)) writer.WriteHeader(http.StatusTooManyRequests) - util.Json.NewEncoder(writer).Encode(entities.RatelimitedError) + json.NewEncoder(writer).Encode(entities.RatelimitedError) return } headers.Set("X-RateLimit-Limit", strconv.Itoa(r.Limit)) diff --git a/routes/bots.go b/routes/bots.go index 27f553e..4f368ae 100644 --- a/routes/bots.go +++ b/routes/bots.go @@ -2,6 +2,7 @@ package routes import ( "context" + "encoding/json" "github.com/discordextremelist/api/entities" "github.com/discordextremelist/api/ratelimit" "github.com/discordextremelist/api/util" @@ -67,7 +68,7 @@ func UpdateStats(w http.ResponseWriter, r *http.Request) { return } var body StatsRequest - err = util.Json.Unmarshal(bytes, &body) + err = json.Unmarshal(bytes, &body) if err != nil { entities.WriteErrorResponse(w, entities.ReadFailed) return @@ -98,12 +99,12 @@ func UpdateStats(w http.ResponseWriter, r *http.Request) { } else { set["shardCount"] = bot.ShardCount } - marshaled, err := util.Json.MarshalToString(bot) + marshaled, err := json.Marshal(bot) if err != nil { entities.WriteErrorResponse(w, err) return } - err = util.Database.Redis.HMSet(context.TODO(), "bots", bot.ID, marshaled).Err() + err = util.Database.Redis.HMSet(context.TODO(), "bots", bot.ID, string(marshaled)).Err() if err != nil { entities.WriteErrorResponse(w, err) return diff --git a/util/util.go b/util/util.go index a3bbdc4..6e43d95 100644 --- a/util/util.go +++ b/util/util.go @@ -3,13 +3,11 @@ package util import ( "github.com/discordextremelist/api/database" "github.com/go-chi/chi" - "github.com/json-iterator/go" ) var ( IPWhitelist []string Database = database.NewManager() - Json = jsoniter.ConfigFastest Router chi.Router Dev bool )