Skip to content

Commit df5062f

Browse files
author
Dorian Stoll
committedMar 19, 2017
Improve memory consumption
1 parent 49bede6 commit df5062f

15 files changed

+156
-182
lines changed
 

‎src/SpaceDock/middleware/accessrestriction.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ import (
1313
"SpaceDock/objects"
1414
"SpaceDock/utils"
1515
"encoding/json"
16+
"github.com/spf13/cast"
1617
"gopkg.in/kataras/iris.v6"
1718
"log"
18-
"github.com/spf13/cast"
1919
)
2020

2121
/*

‎src/SpaceDock/middleware/sessionauth.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ func LoginRequired(ctx *iris.Context) {
1818
userID, err := ctx.Session().GetInt("SessionID")
1919
if err == nil {
2020
var user objects.User
21-
err = user.GetById(userID)
21+
err = user.GetById(cast.ToUint(userID))
2222
if !(err == nil && user.IsAuthenticated()) {
2323
ctx.SetStatusCode(iris.StatusUnauthorized)
2424
return
@@ -43,10 +43,10 @@ func LogoutUser(ctx *iris.Context) {
4343
func CurrentUser(ctx *iris.Context) *objects.User {
4444
userID, err := ctx.Session().GetInt("SessionID")
4545
if err == nil {
46-
var user objects.User
46+
user := &objects.User{}
4747
err = user.GetById(cast.ToUint(userID))
48-
if err != nil {
49-
return &user
48+
if err == nil {
49+
return user
5050
} else {
5151
return nil
5252
}

‎src/SpaceDock/objects/ability.go

-20
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ package objects
1010

1111
import (
1212
"SpaceDock"
13-
"SpaceDock/utils"
14-
"errors"
1513
)
1614

1715
type Ability struct {
@@ -31,22 +29,4 @@ func (s *Ability) AfterFind() {
3129
if isRoot {
3230
SpaceDock.DBRecursion = 0
3331
}
34-
}
35-
36-
func (ability *Ability) GetById(id interface{}) error {
37-
SpaceDock.Database.First(&ability, id)
38-
if ability.Name != "" {
39-
return errors.New("Invalid ability ID")
40-
}
41-
return nil
42-
}
43-
44-
func (ability Ability) Format() map[string]interface{} {
45-
return map[string]interface{} {
46-
"id": ability.ID,
47-
"name": ability.Name,
48-
"created": ability.CreatedAt,
49-
"updated": ability.UpdatedAt,
50-
"meta": utils.LoadJSON(ability.Meta),
51-
}
5232
}

‎src/SpaceDock/objects/meta.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ type Model struct {
2222
Meta string `gorm:"size:4096" json:"meta"`
2323
}
2424

25-
func (meta Model) GetValue(key string) (error,interface{}) {
25+
func (meta *Model) GetValue(key string) (error,interface{}) {
2626
var temp map[string]interface{}
2727
err := json.Unmarshal([]byte(meta.Meta), &temp)
2828
if err != nil {

‎src/SpaceDock/objects/role.go

+15-24
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212
"SpaceDock"
1313
"SpaceDock/utils"
1414
"encoding/json"
15-
"errors"
1615
)
1716

1817
type Role struct {
@@ -36,50 +35,42 @@ func (s *Role) AfterFind() {
3635
}
3736
}
3837

39-
func (role *Role) GetById(id interface{}) error {
40-
SpaceDock.Database.First(&role, id)
41-
if role.Name != "" {
42-
return errors.New("Invalid role ID")
43-
}
44-
return nil
45-
}
46-
47-
func (role Role) AddAbility(name string) Ability {
48-
ability := Ability {}
49-
SpaceDock.Database.Where("name = ?", name).First(&ability)
38+
func (role *Role) AddAbility(name string) *Ability {
39+
ability := &Ability {}
40+
SpaceDock.Database.Where("name = ?", name).First(ability)
5041
if ability.Name == "" {
5142
ability.Name = name
5243
ability.Meta = "{}"
53-
SpaceDock.Database.Save(&ability)
44+
SpaceDock.Database.Save(ability)
5445
}
55-
role.Abilities = append(role.Abilities, ability)
46+
role.Abilities = append(role.Abilities, *ability)
5647
SpaceDock.Database.Save(role)
5748
return ability
5849
}
5950

60-
func (role Role) RemoveAbility(name string) {
61-
ability := Ability {}
62-
SpaceDock.Database.Where("name = ?", name).First(&ability)
51+
func (role *Role) RemoveAbility(name string) {
52+
ability := &Ability {}
53+
SpaceDock.Database.Where("name = ?", name).First(ability)
6354
if ability.Name == "" {
6455
return
6556
}
66-
if e,i := utils.ArrayContains(&ability, role.Abilities); e {
57+
if e,i := utils.ArrayContains(ability, role.Abilities); e {
6758
role.Abilities = append(role.Abilities[:i], role.Abilities[i + 1:]...)
68-
SpaceDock.Database.Save(&role)
59+
SpaceDock.Database.Save(role)
6960
}
7061
}
7162

72-
func (role Role) HasAbility(name string) bool {
73-
ability := Ability {}
74-
SpaceDock.Database.Where("name = ?", name).First(&ability)
63+
func (role *Role) HasAbility(name string) bool {
64+
ability := &Ability {}
65+
SpaceDock.Database.Where("name = ?", name).First(ability)
7566
if ability.Name == "" {
7667
return false
7768
}
78-
e,_ := utils.ArrayContains(&ability, &role.Abilities)
69+
e,_ := utils.ArrayContains(ability, &(role.Abilities))
7970
return e
8071
}
8172

82-
func (role Role) GetParams(ability string, param string) []string {
73+
func (role *Role) GetParams(ability string, param string) []string {
8374
var temp map[string]map[string][]string
8475
err := json.Unmarshal([]byte(role.Params), &temp)
8576
if err != nil {

‎src/SpaceDock/objects/user.go

+21-25
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ func (user *User) SetPassword(password string) {
7070

7171
/* Login Interface */
7272

73-
func (user User) IsAuthenticated() bool {
73+
func (user *User) IsAuthenticated() bool {
7474
return user.authed
7575
}
7676

@@ -82,57 +82,53 @@ func (user *User) Logout() {
8282
user.authed = false
8383
}
8484

85-
func (user User) UniqueId() interface{} {
86-
return user.ID
87-
}
88-
89-
func (user *User) GetById(id interface{}) error {
90-
SpaceDock.Database.First(&user, id)
91-
if user.Username != "" {
85+
func (user *User) GetById(id uint) error {
86+
SpaceDock.Database.Where("id = ?", id).First(user)
87+
if user.ID != id {
9288
return errors.New("Invalid user ID")
9389
}
9490
return nil
9591
}
9692

9793
/* Login Interface End */
9894

99-
func (user User) AddRole(name string) Role {
100-
role := Role {}
101-
SpaceDock.Database.Where("name = ?", name).First(&role)
95+
func (user *User) AddRole(name string) *Role {
96+
role := &Role {}
97+
SpaceDock.Database.Where("name = ?", name).First(role)
10298
if role.Name == "" {
10399
role.Name = name
104100
role.Params = "{}"
105101
role.Meta = "{}"
106-
SpaceDock.Database.Save(&role)
102+
SpaceDock.Database.Save(role)
107103
}
108-
user.Roles = append(user.Roles, role)
109-
SpaceDock.Database.Save(&user)
104+
user.Roles = append(user.Roles, *role)
105+
SpaceDock.Database.Save(user)
110106
return role
111107
}
112108

113-
func (user User) RemoveRole(name string) {
114-
role := Role{}
115-
SpaceDock.Database.Where("name = ?", name).First(&role)
109+
func (user *User) RemoveRole(name string) {
110+
role := &Role{}
111+
SpaceDock.Database.Where("name = ?", name).First(role)
116112
if role.Name == "" {
117113
return
118114
}
119-
if e,i := utils.ArrayContains(&role, user.Roles); e {
115+
if e,i := utils.ArrayContains(role, user.Roles); e {
120116
user.Roles = append(user.Roles[:i], user.Roles[i + 1:]...)
121-
SpaceDock.Database.Save(&user)
117+
SpaceDock.Database.Save(user)
122118
}
123119
}
124120

125-
func (user User) HasRole(name string) bool {
126-
role := Role {}
127-
SpaceDock.Database.Where("name = ?", name).First(&role)
121+
func (user *User) HasRole(name string) bool {
122+
role := &Role {}
123+
SpaceDock.Database.Where("name = ?", name).First(role)
128124
if role.Name == "" {
129125
return false
130126
}
131-
e,_ := utils.ArrayContains(&role, &user.Roles)
127+
e,_ := utils.ArrayContains(role, &(user.Roles))
132128
return e
133129
}
134130

135-
func (user User) GetAbilities() []string {
131+
func (user *User) GetAbilities() []string {
136132
count := 0
137133
for _,element := range user.Roles {
138134
count = count + len(element.Abilities)
@@ -148,7 +144,7 @@ func (user User) GetAbilities() []string {
148144
return value
149145
}
150146

151-
func (user User) Format(admin bool) map[string]interface{} {
147+
func (user *User) Format(admin bool) map[string]interface{} {
152148
if (admin) {
153149
roles := user.Roles
154150
names := make([]string, len(roles))

‎src/SpaceDock/routes/access.go

+25-25
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ func AccessRegister() {
6262
Abilities: access-view
6363
*/
6464
func list_roles(ctx *iris.Context) {
65-
var roles []objects.Role
65+
roles := []objects.Role{}
6666
SpaceDock.Database.Find(&roles)
6767
output := make([]map[string]interface{}, len(roles))
6868
for i,element := range roles {
@@ -97,15 +97,15 @@ func assign_role(ctx *iris.Context) {
9797
rolename := cast.ToString(utils.GetJSON(ctx,"rolename"))
9898

9999
// Try to get the user
100-
var user objects.User
101-
SpaceDock.Database.Where("id = ?", userid).First(&user)
100+
user := &objects.User{}
101+
SpaceDock.Database.Where("id = ?", userid).First(user)
102102
if user.ID != userid {
103103
utils.WriteJSON(ctx, iris.StatusBadRequest, utils.Error("The userid is invalid.").Code(2145))
104104
}
105105

106106
// User is valid, assign the new role
107107
role := user.AddRole(rolename)
108-
SpaceDock.Database.Save(&role).Save(&user)
108+
SpaceDock.Database.Save(role).Save(user)
109109
utils.WriteJSON(ctx, iris.StatusOK, iris.Map{"error": false})
110110
}
111111

@@ -123,8 +123,8 @@ func remove_role(ctx *iris.Context) {
123123
rolename := cast.ToString(utils.GetJSON(ctx,"rolename"))
124124

125125
// Try to get the user
126-
var user objects.User
127-
SpaceDock.Database.Where("id = ?", userid).First(&user)
126+
user := &objects.User{}
127+
SpaceDock.Database.Where("id = ?", userid).First(user)
128128
if user.ID != userid {
129129
utils.WriteJSON(ctx, iris.StatusBadRequest, utils.Error("The userid is invalid.").Code(2145))
130130
return
@@ -136,7 +136,7 @@ func remove_role(ctx *iris.Context) {
136136

137137
// Everything is valid, remove the role
138138
user.RemoveRole(rolename)
139-
SpaceDock.Database.Save(&user)
139+
SpaceDock.Database.Save(user)
140140
utils.WriteJSON(ctx, iris.StatusOK, iris.Map{"error": false})
141141
}
142142

@@ -172,16 +172,16 @@ func assign_ability(ctx *iris.Context) {
172172
abname := cast.ToString(utils.GetJSON(ctx,"abname"))
173173

174174
// Try to get the role
175-
var role objects.Role
176-
SpaceDock.Database.Where("name = ?", rolename).First(&role)
175+
role := &objects.Role{}
176+
SpaceDock.Database.Where("name = ?", rolename).First(role)
177177
if role.Name != rolename {
178178
utils.WriteJSON(ctx, iris.StatusBadRequest, utils.Error("The role does not exist. Please add it to a user to create it internally.").Code(3030))
179179
return
180180
}
181181

182182
// Role is valid, assign the new ability
183183
ability := role.AddAbility(abname)
184-
SpaceDock.Database.Save(&role).Save(&ability)
184+
SpaceDock.Database.Save(role).Save(ability)
185185
utils.WriteJSON(ctx, iris.StatusOK, iris.Map{"error": false})
186186
}
187187

@@ -197,10 +197,10 @@ func remove_ability(ctx *iris.Context) {
197197
abname := cast.ToString(utils.GetJSON(ctx,"abname"))
198198

199199
// Try to get the role
200-
var role objects.Role
201-
SpaceDock.Database.Where("name = ?", rolename).First(&role)
202-
var ability objects.Ability
203-
SpaceDock.Database.Where("name = ?", abname).First(&ability)
200+
role := &objects.Role{}
201+
SpaceDock.Database.Where("name = ?", rolename).First(role)
202+
ability := &objects.Ability{}
203+
SpaceDock.Database.Where("name = ?", abname).First(ability)
204204
errors := []string{}
205205
codes := []int{}
206206
if role.Name != rolename {
@@ -224,7 +224,7 @@ func remove_ability(ctx *iris.Context) {
224224

225225
// Remove the ability
226226
role.RemoveAbility(abname)
227-
SpaceDock.Database.Save(&role)
227+
SpaceDock.Database.Save(role)
228228
utils.WriteJSON(ctx, iris.StatusOK, iris.Map{"error": false})
229229
}
230230

@@ -242,10 +242,10 @@ func add_param(ctx *iris.Context) {
242242
value := cast.ToString(utils.GetJSON(ctx,"value"))
243243

244244
// Try to get the role
245-
var role objects.Role
246-
SpaceDock.Database.Where("name = ?", rolename).First(&role)
247-
var ability objects.Ability
248-
SpaceDock.Database.Where("name = ?", abname).First(&ability)
245+
role := &objects.Role{}
246+
SpaceDock.Database.Where("name = ?", rolename).First(role)
247+
ability := &objects.Ability{}
248+
SpaceDock.Database.Where("name = ?", abname).First(ability)
249249
errors := []string{}
250250
codes := []int{}
251251
if role.Name != rolename {
@@ -263,7 +263,7 @@ func add_param(ctx *iris.Context) {
263263

264264
// Both objects are valid, check if they are linked
265265
role.AddParam(abname, param, value)
266-
SpaceDock.Database.Save(&role)
266+
SpaceDock.Database.Save(role)
267267
utils.WriteJSON(ctx, iris.StatusOK, iris.Map{"error": false})
268268
}
269269

@@ -281,10 +281,10 @@ func remove_param(ctx *iris.Context) {
281281
value := cast.ToString(utils.GetJSON(ctx,"value"))
282282

283283
// Try to get the role
284-
var role objects.Role
285-
SpaceDock.Database.Where("name = ?", rolename).First(&role)
286-
var ability objects.Ability
287-
SpaceDock.Database.Where("name = ?", abname).First(&ability)
284+
role := &objects.Role{}
285+
SpaceDock.Database.Where("name = ?", rolename).First(role)
286+
ability := &objects.Ability{}
287+
SpaceDock.Database.Where("name = ?", abname).First(ability)
288288
errors := []string{}
289289
codes := []int{}
290290
if role.Name != rolename {
@@ -302,6 +302,6 @@ func remove_param(ctx *iris.Context) {
302302

303303
// Both objects are valid, check if the param exists
304304
role.RemoveParam(abname, param, value)
305-
SpaceDock.Database.Save(&role)
305+
SpaceDock.Database.Save(role)
306306
utils.WriteJSON(ctx, iris.StatusOK, iris.Map{"error": false})
307307
}

‎src/SpaceDock/routes/accounts.go

+12-12
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func confirm(ctx *iris.Context) {
4141
confirmation := ctx.GetString("confirmation")
4242

4343
// Try to get a valid user account
44-
var user *objects.User
44+
user := &objects.User{}
4545
SpaceDock.Database.Where("confirmation = ?", confirmation).First(user)
4646
if user.Confirmation != confirmation {
4747
utils.WriteJSON(ctx, iris.StatusBadRequest, utils.Error("User does not exist or it is already confirmed. Did you mistype the confirmation?").Code(2165))
@@ -59,8 +59,8 @@ func confirm(ctx *iris.Context) {
5959
role.AddParam("user-edit", "userid", strconv.Itoa(int(user.ID)))
6060
role.AddParam("mods-add", "gameshort", ".*")
6161
role.AddParam("packs-add", "gameshort", ".*")
62-
SpaceDock.Database.Save(&role)
63-
SpaceDock.Database.Save(&user)
62+
SpaceDock.Database.Save(role)
63+
SpaceDock.Database.Save(user)
6464

6565
// Follow Mod
6666

@@ -85,8 +85,8 @@ func login(ctx *iris.Context) {
8585
utils.WriteJSON(ctx, iris.StatusBadRequest, utils.Error("You are already logged in").Code(3060))
8686
return
8787
}
88-
var user objects.User
89-
SpaceDock.Database.Where("username = ?", username).First(&user)
88+
user := &objects.User{}
89+
SpaceDock.Database.Where("username = ?", username).First(user)
9090
if user.Username != username {
9191
utils.WriteJSON(ctx, iris.StatusBadRequest, utils.Error("Username or password is incorrect").Code(2175))
9292
return
@@ -99,7 +99,7 @@ func login(ctx *iris.Context) {
9999
utils.WriteJSON(ctx, iris.StatusBadRequest, utils.Error("User is not confirmed").Code(3055))
100100
return
101101
}
102-
middleware.LoginUser(ctx, &user)
102+
middleware.LoginUser(ctx, user)
103103
utils.WriteJSON(ctx, iris.StatusOK, iris.Map{"error": false, "count": 1, "data": user.Format(true)})
104104
}
105105

@@ -131,15 +131,15 @@ func reset(ctx *iris.Context) {
131131
utils.WriteJSON(ctx, iris.StatusBadRequest, utils.Error("No email address").Code(2520))
132132
return
133133
}
134-
var user objects.User
135-
SpaceDock.Database.Where("email = ?", email).First(&user)
134+
user := &objects.User{}
135+
SpaceDock.Database.Where("email = ?", email).First(user)
136136
if user.Email != email {
137137
utils.WriteJSON(ctx, iris.StatusBadRequest, utils.Error("No user for provided email address").Code(2115))
138138
return
139139
}
140140
user.PasswordReset,_ = utils.RandomHex(20)
141141
user.PasswordResetExpiry = time.Now().Add(time.Hour * 24)
142-
SpaceDock.Database.Save(&user)
142+
SpaceDock.Database.Save(user)
143143
utils.SendReset(user.Username, user.PasswordReset, user.Email)
144144
utils.WriteJSON(ctx, iris.StatusOK, iris.Map{"error": false})
145145
}
@@ -151,8 +151,8 @@ func reset(ctx *iris.Context) {
151151
func reset_confirm(ctx *iris.Context) {
152152
username := ctx.GetString("username")
153153
confirmation := ctx.GetString("confirmation")
154-
var user objects.User
155-
SpaceDock.Database.Where("username = ?", username).First(&user)
154+
user := &objects.User{}
155+
SpaceDock.Database.Where("username = ?", username).First(user)
156156
if user.Username != username {
157157
utils.WriteJSON(ctx, iris.StatusBadRequest, utils.Error("Username is incorrect").Code(2170))
158158
return
@@ -174,7 +174,7 @@ func reset_confirm(ctx *iris.Context) {
174174
user.SetPassword(password)
175175
user.PasswordReset = ""
176176
user.PasswordResetExpiry = time.Now()
177-
SpaceDock.Database.Save(&user)
177+
SpaceDock.Database.Save(user)
178178
if middleware.CurrentUser(ctx) != nil {
179179
middleware.LogoutUser(ctx)
180180
}

‎src/SpaceDock/routes/admin.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,14 @@ func impersonate(ctx *iris.Context) {
4545
utils.WriteJSON(ctx, iris.StatusBadRequest, utils.Error("The userid is invalid").Code(2145))
4646
return
4747
}
48-
var user objects.User
49-
SpaceDock.Database.Where("id = ?", userid).First(&user)
48+
user := &objects.User{}
49+
SpaceDock.Database.Where("id = ?", userid).First(user)
5050
if user.ID != userid {
5151
utils.WriteJSON(ctx, iris.StatusBadRequest, utils.Error("The userid is invalid").Code(2145))
5252
return
5353
}
5454
middleware.LogoutUser(ctx)
55-
middleware.LoginUser(ctx, &user)
55+
middleware.LoginUser(ctx, user)
5656
utils.WriteJSON(ctx, iris.StatusOK, iris.Map{"error": false})
5757
}
5858

@@ -68,8 +68,8 @@ func manual_confirmation(ctx *iris.Context) {
6868
utils.WriteJSON(ctx, iris.StatusBadRequest, utils.Error("The userid is invalid").Code(2145))
6969
return
7070
}
71-
var user objects.User
72-
SpaceDock.Database.Where("id = ?", userid).First(&user)
71+
user := &objects.User{}
72+
SpaceDock.Database.Where("id = ?", userid).First(user)
7373
if user.ID != userid {
7474
utils.WriteJSON(ctx, iris.StatusBadRequest, utils.Error("The userid is invalid").Code(2145))
7575
return
@@ -85,7 +85,7 @@ func manual_confirmation(ctx *iris.Context) {
8585
role.AddParam("user-edit", "userid", strconv.Itoa(int(user.ID)))
8686
role.AddParam("mods-add", "gameshort", ".*")
8787
role.AddParam("packs-add", "gameshort", ".*")
88-
SpaceDock.Database.Save(&role)
89-
SpaceDock.Database.Save(&user)
88+
SpaceDock.Database.Save(role)
89+
SpaceDock.Database.Save(user)
9090
utils.WriteJSON(ctx, iris.StatusOK, iris.Map{"error": false})
9191
}

‎src/SpaceDock/routes/game.go

+31-31
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ func GameRegister() {
5959
Description: Displays a list of all games in the database.
6060
*/
6161
func list_games(ctx *iris.Context) {
62-
var games []objects.Game
62+
games := []objects.Game{}
6363
includeInactive := ctx.URLParam("includeInactive")
6464
val, err := strconv.ParseBool(includeInactive)
6565
if (err != nil) && val {
@@ -81,8 +81,8 @@ func list_games(ctx *iris.Context) {
8181
*/
8282
func show_game(ctx *iris.Context) {
8383
gameshort := ctx.GetString("gameshort")
84-
var game objects.Game
85-
SpaceDock.Database.Where("short = ?", gameshort).First(&game)
84+
game := &objects.Game{}
85+
SpaceDock.Database.Where("short = ?", gameshort).First(game)
8686
if game.Short != gameshort {
8787
utils.WriteJSON(ctx, iris.StatusNotFound, utils.Error("The game does not exist.").Code(2125))
8888
return
@@ -98,15 +98,15 @@ func show_game(ctx *iris.Context) {
9898
*/
9999
func edit_game(ctx *iris.Context) {
100100
gameshort := ctx.GetString("gameshort")
101-
var game objects.Game
102-
SpaceDock.Database.Where("short = ?", gameshort).First(&game)
101+
game := &objects.Game{}
102+
SpaceDock.Database.Where("short = ?", gameshort).First(game)
103103
if game.Short != gameshort {
104104
utils.WriteJSON(ctx, iris.StatusNotFound, utils.Error("The game does not exist.").Code(2125))
105105
return
106106
}
107107

108108
// Edit the game
109-
code := utils.EditObject(&game, utils.GetFullJSON(ctx))
109+
code := utils.EditObject(game, utils.GetFullJSON(ctx))
110110
if code == 3 {
111111
utils.WriteJSON(ctx, iris.StatusBadRequest, utils.Error("The value you submitted is invalid").Code(2180))
112112
return
@@ -117,7 +117,7 @@ func edit_game(ctx *iris.Context) {
117117
utils.WriteJSON(ctx, iris.StatusBadRequest, utils.Error("You tried to edit a value that is marked as read-only.").Code(3095))
118118
return
119119
}
120-
SpaceDock.Database.Save(&game)
120+
SpaceDock.Database.Save(game)
121121
utils.WriteJSON(ctx, iris.StatusOK, iris.Map{"error": false, "count": 1, "data": utils.ToMap(game)})
122122
}
123123

@@ -136,8 +136,8 @@ func add_game(ctx *iris.Context) {
136136
codes := []int{}
137137

138138
// Check if the publisher ID is valid
139-
var publisher objects.Publisher
140-
SpaceDock.Database.Where("id = ?", pubid).First(&publisher)
139+
publisher := objects.Publisher{}
140+
SpaceDock.Database.Where("id = ?", pubid).First(publisher)
141141
if publisher.ID != pubid {
142142
errors = append(errors, "The pubid is invalid.")
143143
codes = append(codes, 2110)
@@ -152,13 +152,13 @@ func add_game(ctx *iris.Context) {
152152
}
153153

154154
// Check if the game already exists
155-
var game *objects.Game
155+
game := &objects.Game{}
156156
SpaceDock.Database.Where("short = ?", short).First(game)
157157
if game.Short == short {
158158
errors = append(errors, "The gameshort already exists.")
159159
codes = append(codes, 2015)
160160
}
161-
SpaceDock.Database.Where("name = ?", name).First(&game)
161+
SpaceDock.Database.Where("name = ?", name).First(game)
162162
if game.Name == name {
163163
errors = append(errors, "The game name already exists.")
164164
codes = append(codes, 2020)
@@ -173,7 +173,7 @@ func add_game(ctx *iris.Context) {
173173
// Make a new game
174174
game = objects.NewGame(name, publisher, short)
175175
SpaceDock.Database.Save(game)
176-
utils.WriteJSON(ctx, iris.StatusOK, iris.Map{"error": false, "count": 1, "data": utils.ToMap(*game)})
176+
utils.WriteJSON(ctx, iris.StatusOK, iris.Map{"error": false, "count": 1, "data": utils.ToMap(game)})
177177
}
178178

179179
/*
@@ -186,7 +186,7 @@ func remove_game(ctx *iris.Context) {
186186
short := cast.ToString(utils.GetJSON(ctx, "short"))
187187

188188
// Check if the game exists
189-
var game *objects.Game
189+
game := &objects.Game{}
190190
SpaceDock.Database.Where("short = ?", short).First(game)
191191
if game.Short != short {
192192
utils.WriteJSON(ctx, iris.StatusNotFound, utils.Error("The game does not exist.").Code(2125))
@@ -205,8 +205,8 @@ func remove_game(ctx *iris.Context) {
205205
*/
206206
func game_versions(ctx *iris.Context) {
207207
gameshort := ctx.GetString("gameshort")
208-
var game objects.Game
209-
SpaceDock.Database.Where("short = ?", gameshort).First(&game)
208+
game := &objects.Game{}
209+
SpaceDock.Database.Where("short = ?", gameshort).First(game)
210210
if game.Short != gameshort {
211211
utils.WriteJSON(ctx, iris.StatusNotFound, utils.Error("The game does not exist.").Code(2125))
212212
return
@@ -228,8 +228,8 @@ func game_versions(ctx *iris.Context) {
228228
*/
229229
func game_version_add(ctx *iris.Context) {
230230
gameshort := ctx.GetString("gameshort")
231-
var game objects.Game
232-
SpaceDock.Database.Where("short = ?", gameshort).First(&game)
231+
game := &objects.Game{}
232+
SpaceDock.Database.Where("short = ?", gameshort).First(game)
233233
if game.Short != gameshort {
234234
utils.WriteJSON(ctx, iris.StatusNotFound, utils.Error("The game does not exist.").Code(2125))
235235
return
@@ -240,7 +240,7 @@ func game_version_add(ctx *iris.Context) {
240240
is_beta := cast.ToBool(utils.GetJSON(ctx, "is_beta"))
241241

242242
// Create a new version
243-
version := objects.NewGameVersion(friendly_version, game, is_beta)
243+
version := objects.NewGameVersion(friendly_version, *game, is_beta)
244244
SpaceDock.Database.Save(version)
245245

246246
// Format the output
@@ -255,16 +255,16 @@ func game_version_add(ctx *iris.Context) {
255255
*/
256256
func game_version_remove(ctx *iris.Context) {
257257
gameshort := ctx.GetString("gameshort")
258-
var game objects.Game
259-
SpaceDock.Database.Where("short = ?", gameshort).First(&game)
258+
game := &objects.Game{}
259+
SpaceDock.Database.Where("short = ?", gameshort).First(game)
260260
if game.Short != gameshort {
261261
utils.WriteJSON(ctx, iris.StatusNotFound, utils.Error("The game does not exist.").Code(2125))
262262
return
263263
}
264264

265265
// Get game version
266266
friendly_version := cast.ToString(utils.GetJSON(ctx, "friendly_version"))
267-
var version *objects.GameVersion
267+
version := &objects.GameVersion{}
268268
SpaceDock.Database.Where("friendly_version = ?", friendly_version).Where("game_id = ?", game.ID).First(version)
269269
if version.FriendlyVersion != friendly_version {
270270
utils.WriteJSON(ctx, iris.StatusNotFound, utils.Error("This version name does not exist.").Code(2185))
@@ -285,17 +285,17 @@ func game_version_remove(ctx *iris.Context) {
285285
*/
286286
func game_version_show(ctx *iris.Context) {
287287
gameshort := ctx.GetString("gameshort")
288-
var game objects.Game
289-
SpaceDock.Database.Where("short = ?", gameshort).First(&game)
288+
game := &objects.Game{}
289+
SpaceDock.Database.Where("short = ?", gameshort).First(game)
290290
if game.Short != gameshort {
291291
utils.WriteJSON(ctx, iris.StatusNotFound, utils.Error("The game does not exist.").Code(2125))
292292
return
293293
}
294294

295295
// Get game version
296296
friendly_version := ctx.GetString("friendly_version")
297-
var version objects.GameVersion
298-
SpaceDock.Database.Where("friendly_version = ?", friendly_version).Where("game_id = ?", game.ID).First(&version)
297+
version := &objects.GameVersion{}
298+
SpaceDock.Database.Where("friendly_version = ?", friendly_version).Where("game_id = ?", game.ID).First(version)
299299
if version.FriendlyVersion != friendly_version {
300300
utils.WriteJSON(ctx, iris.StatusNotFound, utils.Error("This version name does not exist.").Code(2185))
301301
return
@@ -313,24 +313,24 @@ func game_version_show(ctx *iris.Context) {
313313
*/
314314
func game_version_edit(ctx *iris.Context) {
315315
gameshort := ctx.GetString("gameshort")
316-
var game objects.Game
317-
SpaceDock.Database.Where("short = ?", gameshort).First(&game)
316+
game := &objects.Game{}
317+
SpaceDock.Database.Where("short = ?", gameshort).First(game)
318318
if game.Short != gameshort {
319319
utils.WriteJSON(ctx, iris.StatusNotFound, utils.Error("The game does not exist.").Code(2125))
320320
return
321321
}
322322

323323
// Get game version
324324
friendly_version := ctx.GetString("friendly_version")
325-
var version objects.GameVersion
326-
SpaceDock.Database.Where("friendly_version = ?", friendly_version).Where("game_id = ?", game.ID).First(&version)
325+
version := &objects.GameVersion{}
326+
SpaceDock.Database.Where("friendly_version = ?", friendly_version).Where("game_id = ?", game.ID).First(version)
327327
if version.FriendlyVersion != friendly_version {
328328
utils.WriteJSON(ctx, iris.StatusNotFound, utils.Error("This version name does not exist.").Code(2185))
329329
return
330330
}
331331

332332
// Edit the game version
333-
code := utils.EditObject(&version, utils.GetFullJSON(ctx))
333+
code := utils.EditObject(version, utils.GetFullJSON(ctx))
334334
if code == 3 {
335335
utils.WriteJSON(ctx, iris.StatusBadRequest, utils.Error("The value you submitted is invalid").Code(2180))
336336
return
@@ -341,6 +341,6 @@ func game_version_edit(ctx *iris.Context) {
341341
utils.WriteJSON(ctx, iris.StatusBadRequest, utils.Error("You tried to edit a value that is marked as read-only.").Code(3095))
342342
return
343343
}
344-
SpaceDock.Database.Save(&version)
344+
SpaceDock.Database.Save(version)
345345
utils.WriteJSON(ctx, iris.StatusOK, iris.Map{"error": false, "count": 1, "data": utils.ToMap(version)})
346346
}

‎src/SpaceDock/routes/init.go

+7
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"gopkg.in/kataras/iris.v6"
1717
"gopkg.in/kataras/iris.v6/middleware/logger"
1818
"log"
19+
"runtime"
1920
)
2021

2122
/*
@@ -75,4 +76,10 @@ func MiddlewareRegister() {
7576
AllowCredentials: true,
7677
}))
7778
}
79+
80+
// Force garbage collection
81+
SpaceDock.App.Use(iris.HandlerFunc(func (ctx *iris.Context) {
82+
runtime.GC()
83+
ctx.Next()
84+
}))
7885
}

‎src/SpaceDock/routes/publisher.go

+14-14
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ func publishers_info(ctx *iris.Context) {
6161
pubid := cast.ToUint(ctx.GetString("pubid"))
6262

6363
// Get the publisher
64-
var pub objects.Publisher
65-
SpaceDock.Database.Where("id = ?", pubid).First(&pub)
64+
pub := &objects.Publisher{}
65+
SpaceDock.Database.Where("id = ?", pubid).First(pub)
6666
if pub.ID != pubid {
6767
utils.WriteJSON(ctx, iris.StatusNotFound, utils.Error("The publisher ID is invalid").Code(2110))
6868
return
@@ -82,15 +82,15 @@ func edit_publisher(ctx *iris.Context) {
8282
pubid := cast.ToUint(ctx.GetString("pubid"))
8383

8484
// Get the publisher
85-
var pub objects.Publisher
86-
SpaceDock.Database.Where("id = ?", pubid).First(&pub)
85+
pub := &objects.Publisher{}
86+
SpaceDock.Database.Where("id = ?", pubid).First(pub)
8787
if pub.ID != pubid {
8888
utils.WriteJSON(ctx, iris.StatusNotFound, utils.Error("The publisher ID is invalid").Code(2110))
8989
return
9090
}
9191

9292
// Edit the publisher
93-
code := utils.EditObject(&pub, utils.GetFullJSON(ctx))
93+
code := utils.EditObject(pub, utils.GetFullJSON(ctx))
9494
if code == 3 {
9595
utils.WriteJSON(ctx, iris.StatusBadRequest, utils.Error("The value you submitted is invalid").Code(2180))
9696
return
@@ -101,7 +101,7 @@ func edit_publisher(ctx *iris.Context) {
101101
utils.WriteJSON(ctx, iris.StatusBadRequest, utils.Error("You tried to edit a value that is marked as read-only.").Code(3095))
102102
return
103103
}
104-
SpaceDock.Database.Save(&pub)
104+
SpaceDock.Database.Save(pub)
105105
utils.WriteJSON(ctx, iris.StatusOK, iris.Map{"error": false, "count": 1, "data": utils.ToMap(pub)})
106106
}
107107

@@ -115,16 +115,16 @@ func add_publisher(ctx *iris.Context) {
115115
name := cast.ToString(utils.GetJSON(ctx, "name"))
116116

117117
// Get the publisher
118-
var pub_ objects.Publisher
119-
SpaceDock.Database.Where("name = ?", name).First(&pub_)
120-
if pub_.Name == name {
118+
pub := &objects.Publisher{}
119+
SpaceDock.Database.Where("name = ?", name).First(pub)
120+
if pub.Name == name {
121121
utils.WriteJSON(ctx, iris.StatusBadRequest, utils.Error("A publisher with this name already exists.").Code(2000))
122122
return
123123
}
124124

125125
// Add the publisher
126-
pub := objects.NewPublisher(name)
127-
SpaceDock.Database.Save(&pub)
126+
pub = objects.NewPublisher(name)
127+
SpaceDock.Database.Save(pub)
128128
utils.WriteJSON(ctx, iris.StatusOK, iris.Map{"error": false, "count": 1, "data": utils.ToMap(pub)})
129129
}
130130

@@ -138,14 +138,14 @@ func remove_publisher(ctx *iris.Context) {
138138
pubid := cast.ToUint(ctx.GetString("pubid"))
139139

140140
// Get the publisher
141-
var pub objects.Publisher
142-
SpaceDock.Database.Where("id = ?", pubid).First(&pub)
141+
pub := &objects.Publisher{}
142+
SpaceDock.Database.Where("id = ?", pubid).First(pub)
143143
if pub.ID != pubid {
144144
utils.WriteJSON(ctx, iris.StatusNotFound, utils.Error("The publisher ID is invalid").Code(2110))
145145
return
146146
}
147147

148148
// Delete the publisher
149-
SpaceDock.Database.Delete(&pub)
149+
SpaceDock.Database.Delete(pub)
150150
utils.WriteJSON(ctx, iris.StatusOK, iris.Map{"error": false})
151151
}

‎src/SpaceDock/routes/tokens.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ func edit_token(ctx *iris.Context) {
5959
ips := cast.ToStringSlice(utils.GetJSON(ctx, "ips"))
6060

6161
// Get the token
62-
var token objects.Token
63-
SpaceDock.Database.Where("id = ?", tokenid).First(&token)
62+
token := &objects.Token{}
63+
SpaceDock.Database.Where("id = ?", tokenid).First(token)
6464
if token.ID != tokenid {
6565
utils.WriteJSON(ctx, iris.StatusNotFound, utils.Error("The token ID is invalid").Code(2131))
6666
return
@@ -72,7 +72,7 @@ func edit_token(ctx *iris.Context) {
7272

7373
// Edit the token
7474
token.SetValue("ips", ips)
75-
SpaceDock.Database.Save(&token)
75+
SpaceDock.Database.Save(token)
7676
utils.WriteJSON(ctx, iris.StatusOK, iris.Map{"error": false, "count": 1, "data": utils.ToMap(token)})
7777
}
7878

@@ -86,14 +86,14 @@ func revoke_token(ctx *iris.Context) {
8686
tokenid := cast.ToUint(utils.GetJSON(ctx, "tokenid"))
8787

8888
// Get the token
89-
var token objects.Token
90-
SpaceDock.Database.Where("id = ?", tokenid).First(&token)
89+
token := &objects.Token{}
90+
SpaceDock.Database.Where("id = ?", tokenid).First(token)
9191
if token.ID != tokenid {
9292
utils.WriteJSON(ctx, iris.StatusNotFound, utils.Error("The token ID is invalid").Code(2131))
9393
return
9494
}
9595

9696
// Delete the token
97-
SpaceDock.Database.Delete(&token)
97+
SpaceDock.Database.Delete(token)
9898
utils.WriteJSON(ctx, iris.StatusOK, iris.Map{"error": false})
9999
}

‎src/SpaceDock/routes/user.go

+10-10
Original file line numberDiff line numberDiff line change
@@ -215,24 +215,24 @@ func checkEmailForRegistration(email string) string {
215215
func show_user(ctx *iris.Context) {
216216
userid_ := ctx.GetString("userid")
217217
userid := uint(0)
218-
var user objects.User
218+
user := &objects.User{}
219219
if userid_ == "current" {
220220
if middleware.CurrentUser(ctx) == nil {
221221
utils.WriteJSON(ctx, iris.StatusForbidden, utils.Error("You need to be logged in to access this page").Code(1035))
222222
return
223223
}
224-
user = *middleware.CurrentUser(ctx)
224+
user = middleware.CurrentUser(ctx)
225225
userid = user.ID
226226
} else {
227227
userid = cast.ToUint(userid_)
228-
SpaceDock.Database.Where("id = ?", userid).First(&user)
228+
SpaceDock.Database.Where("id = ?", userid).First(user)
229229
}
230230
if user.ID != userid {
231231
utils.WriteJSON(ctx, iris.StatusNotFound, utils.Error("The userid is invalid.").Code(2145))
232232
return
233233
}
234234
output := map[string]interface{} {}
235-
if middleware.IsCurrentUser(ctx, &user) || middleware.UserHasPermission(ctx, "view-users-full", false, []string{}) == 0 {
235+
if middleware.IsCurrentUser(ctx, user) || middleware.UserHasPermission(ctx, "view-users-full", false, []string{}) == 0 {
236236
output = user.Format(true)
237237
} else if user.Public {
238238
output = user.Format(false)
@@ -251,15 +251,15 @@ func show_user(ctx *iris.Context) {
251251
*/
252252
func edit_user(ctx *iris.Context) {
253253
userid := cast.ToUint(ctx.GetString("userid"))
254-
var user objects.User
255-
SpaceDock.Database.Where("id = ?", userid).First(&user)
254+
user := &objects.User{}
255+
SpaceDock.Database.Where("id = ?", userid).First(user)
256256
if user.ID != userid {
257257
utils.WriteJSON(ctx, iris.StatusNotFound, utils.Error("The userid is invalid.").Code(2145))
258258
return
259259
}
260260

261261
// Everything is ok, edit the user
262-
code := utils.EditObject(&user, utils.GetFullJSON(ctx))
262+
code := utils.EditObject(user, utils.GetFullJSON(ctx))
263263
if code == 3 {
264264
utils.WriteJSON(ctx, iris.StatusBadRequest, utils.Error("The value you submitted is invalid").Code(2180))
265265
return
@@ -289,8 +289,8 @@ func update_user_media(ctx *iris.Context) {
289289
utils.WriteJSON(ctx, iris.StatusInternalServerError, utils.Error(err.Error()).Code(2153))
290290
return
291291
}
292-
var user objects.User
293-
SpaceDock.Database.Where("id = ?", userid).First(&user)
292+
user := &objects.User{}
293+
SpaceDock.Database.Where("id = ?", userid).First(user)
294294
if user.ID != userid {
295295
utils.WriteJSON(ctx, iris.StatusNotFound, utils.Error("The userid is invalid.").Code(2145))
296296
return
@@ -323,7 +323,7 @@ func update_user_media(ctx *iris.Context) {
323323
data.Close()
324324

325325
// Edit the user object
326-
code := utils.EditObject(&user, iris.Map{"meta": iris.Map{mediatype:filepath.Join(base_path, filename)}})
326+
code := utils.EditObject(user, iris.Map{"meta": iris.Map{mediatype:filepath.Join(base_path, filename)}})
327327
if code == 3 {
328328
utils.WriteJSON(ctx, iris.StatusBadRequest, utils.Error("The value you submitted is invalid").Code(2180))
329329
return

‎src/SpaceDock/utils/string.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,10 @@ func DumpJSON(data interface{}) string {
4343

4444
func ToMap(data interface{}) map[string]interface{} {
4545
m := LoadJSON(DumpJSON(data))
46-
m["meta"] = LoadJSON((m["meta"].(string)))
46+
m["meta"] = LoadJSON(m["meta"].(string))
4747
for _,element := range structs.Fields(data) {
4848
if element.Tag("spacedock") == "json" {
49-
m[element.Tag("json")] = LoadJSON((m[element.Tag("json")].(string)))
49+
m[element.Tag("json")] = LoadJSON(m[element.Tag("json")].(string))
5050
}
5151
}
5252
return m

0 commit comments

Comments
 (0)
Please sign in to comment.