Skip to content

Commit

Permalink
Fix id being empty for all entities, tidy go modules, switch to encod…
Browse files Browse the repository at this point in the history
…ing/json
  • Loading branch information
IceeMC committed Aug 19, 2020
1 parent 1a41376 commit 5f047b4
Show file tree
Hide file tree
Showing 11 changed files with 103 additions and 45 deletions.
22 changes: 20 additions & 2 deletions entities/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"`
Expand Down Expand Up @@ -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
Expand All @@ -103,18 +107,25 @@ 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)
}
return nil, bot
}
} else {
err, bot := mongoLookupBot(id)
bot.MongoID = ""
if err != nil {
if err == mongo.ErrNoDocuments {
return err, nil
Expand All @@ -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)
}
Expand All @@ -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
}
Expand Down
7 changes: 3 additions & 4 deletions entities/cachePopulator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 {
Expand Down
7 changes: 4 additions & 3 deletions entities/response.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package entities

import (
"encoding/json"
"errors"
"github.com/discordextremelist/api/util"
"net/http"
Expand Down Expand Up @@ -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) {
Expand All @@ -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
Expand Down Expand Up @@ -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)
}
21 changes: 19 additions & 2 deletions entities/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"`
Expand Down Expand Up @@ -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
Expand All @@ -68,21 +71,28 @@ 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
}
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)
}
return nil, server
}
} else {
err, server := mongoLookupServer(id)
server.MongoID = ""
if err != nil {
if err == mongo.ErrNoDocuments {
return err, nil
Expand All @@ -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
}
Expand All @@ -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
}
Expand Down
25 changes: 21 additions & 4 deletions entities/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"`
Expand Down Expand Up @@ -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
Expand All @@ -92,23 +95,30 @@ 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
}
log.Errorf("Fallback for MongoDB failed for LookupTemplate(%s): %v", id, err.Error())
return LookupError, nil
} else {
return nil, bot
return nil, template
}
}
}
Expand All @@ -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
}
Expand All @@ -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
}
Expand Down
21 changes: 19 additions & 2 deletions entities/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"`
Expand Down Expand Up @@ -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
Expand All @@ -150,18 +153,25 @@ 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)
}
return nil, user
}
} else {
err, user := mongoLookupUser(id)
user.MongoID = ""
if err != nil {
if err == mongo.ErrNoDocuments {
return err, nil
Expand All @@ -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
}
Expand All @@ -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
}
Expand Down
3 changes: 0 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading

0 comments on commit 5f047b4

Please sign in to comment.