Skip to content

Commit 1b28bee

Browse files
author
Vic Shóstak
committed
Refactoring
1 parent a3e6e66 commit 1b28bee

File tree

14 files changed

+906
-107
lines changed

14 files changed

+906
-107
lines changed

Dockerfile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,4 @@ COPY --from=builder ["/build/apiserver", "/build/.env", "/"]
2323
EXPOSE 5000
2424

2525
# Command to run when starting the container.
26-
ENV CONFIG_PATH=/.env
2726
ENTRYPOINT ["/apiserver"]

Makefile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ migrate.force:
3030
migrate -path $(MIGRATIONS_FOLDER) -database "$(DATABASE_URL)" force $(version)
3131

3232
docker.build:
33-
docker build -t fiber-go-template .
33+
docker build -t net_http-go-template .
3434

3535
docker.run: docker.net_http docker.postgres
3636

@@ -48,7 +48,9 @@ docker.postgres:
4848
docker run --rm -d \
4949
--name dev-postgres \
5050
--network dev-network \
51+
-e POSTGRES_USER=postgres \
5152
-e POSTGRES_PASSWORD=password \
53+
-e POSTGRES_DB=postgres \
5254
-v ${HOME}/dev-postgres/data/:/var/lib/postgresql/data \
5355
-p 5432:5432 \
5456
postgres

app/controllers/user_controller.go

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func GetUsers(w http.ResponseWriter, r *http.Request) {
3434
"msg": err.Error(),
3535
})
3636
w.WriteHeader(http.StatusInternalServerError)
37-
w.Write([]byte(payload))
37+
_, _ = w.Write([]byte(payload))
3838
}
3939

4040
// Get all users.
@@ -48,7 +48,7 @@ func GetUsers(w http.ResponseWriter, r *http.Request) {
4848
"users": nil,
4949
})
5050
w.WriteHeader(http.StatusNotFound)
51-
w.Write([]byte(payload))
51+
_, _ = w.Write([]byte(payload))
5252
}
5353

5454
payload, _ := json.Marshal(map[string]interface{}{
@@ -58,7 +58,7 @@ func GetUsers(w http.ResponseWriter, r *http.Request) {
5858
"users": users,
5959
})
6060
w.WriteHeader(http.StatusOK)
61-
w.Write([]byte(payload))
61+
_, _ = w.Write([]byte(payload))
6262
}
6363

6464
// GetUser func gets one user by given ID or 404 error.
@@ -83,7 +83,7 @@ func GetUser(w http.ResponseWriter, r *http.Request) {
8383
"msg": err.Error(),
8484
})
8585
w.WriteHeader(http.StatusInternalServerError)
86-
w.Write([]byte(payload))
86+
_, _ = w.Write([]byte(payload))
8787
}
8888

8989
// Create database connection.
@@ -95,7 +95,7 @@ func GetUser(w http.ResponseWriter, r *http.Request) {
9595
"msg": err.Error(),
9696
})
9797
w.WriteHeader(http.StatusInternalServerError)
98-
w.Write([]byte(payload))
98+
_, _ = w.Write([]byte(payload))
9999
}
100100

101101
// Get user by ID.
@@ -108,7 +108,7 @@ func GetUser(w http.ResponseWriter, r *http.Request) {
108108
"user": nil,
109109
})
110110
w.WriteHeader(http.StatusNotFound)
111-
w.Write([]byte(payload))
111+
_, _ = w.Write([]byte(payload))
112112
}
113113

114114
payload, _ := json.Marshal(map[string]interface{}{
@@ -117,7 +117,7 @@ func GetUser(w http.ResponseWriter, r *http.Request) {
117117
"user": user,
118118
})
119119
w.WriteHeader(http.StatusOK)
120-
w.Write([]byte(payload))
120+
_, _ = w.Write([]byte(payload))
121121
}
122122

123123
// CreateUser func for creates a new user.
@@ -157,7 +157,7 @@ func CreateUser(w http.ResponseWriter, r *http.Request) {
157157
"msg": err.Error(),
158158
})
159159
w.WriteHeader(http.StatusInternalServerError)
160-
w.Write([]byte(payload))
160+
_, _ = w.Write([]byte(payload))
161161
}
162162

163163
// Only user with `user:create` credential can create a new user profile.
@@ -173,7 +173,7 @@ func CreateUser(w http.ResponseWriter, r *http.Request) {
173173
"msg": utils.ValidatorErrors(err),
174174
})
175175
w.WriteHeader(http.StatusInternalServerError)
176-
w.Write([]byte(payload))
176+
_, _ = w.Write([]byte(payload))
177177
}
178178

179179
// Create database connection.
@@ -185,7 +185,7 @@ func CreateUser(w http.ResponseWriter, r *http.Request) {
185185
"msg": err.Error(),
186186
})
187187
w.WriteHeader(http.StatusInternalServerError)
188-
w.Write([]byte(payload))
188+
_, _ = w.Write([]byte(payload))
189189
}
190190

191191
// Set initialized default data for user:
@@ -203,7 +203,7 @@ func CreateUser(w http.ResponseWriter, r *http.Request) {
203203
"msg": err.Error(),
204204
})
205205
w.WriteHeader(http.StatusInternalServerError)
206-
w.Write([]byte(payload))
206+
_, _ = w.Write([]byte(payload))
207207
}
208208
} else {
209209
// Return status 500 and database connection error.
@@ -213,7 +213,7 @@ func CreateUser(w http.ResponseWriter, r *http.Request) {
213213
"user": nil,
214214
})
215215
w.WriteHeader(http.StatusForbidden)
216-
w.Write([]byte(payload))
216+
_, _ = w.Write([]byte(payload))
217217
}
218218

219219
payload, _ := json.Marshal(map[string]interface{}{
@@ -222,7 +222,7 @@ func CreateUser(w http.ResponseWriter, r *http.Request) {
222222
"user": user,
223223
})
224224
w.WriteHeader(http.StatusOK)
225-
w.Write([]byte(payload))
225+
_, _ = w.Write([]byte(payload))
226226
}
227227

228228
// UpdateUser func for updates user by given ID.
@@ -262,7 +262,7 @@ func UpdateUser(w http.ResponseWriter, r *http.Request) {
262262
"msg": err.Error(),
263263
})
264264
w.WriteHeader(http.StatusInternalServerError)
265-
w.Write([]byte(payload))
265+
_, _ = w.Write([]byte(payload))
266266
}
267267

268268
// Create database connection.
@@ -274,7 +274,7 @@ func UpdateUser(w http.ResponseWriter, r *http.Request) {
274274
"msg": err.Error(),
275275
})
276276
w.WriteHeader(http.StatusInternalServerError)
277-
w.Write([]byte(payload))
277+
_, _ = w.Write([]byte(payload))
278278
}
279279

280280
// Checking, if user with given ID is exists.
@@ -285,7 +285,7 @@ func UpdateUser(w http.ResponseWriter, r *http.Request) {
285285
"msg": "user not found",
286286
})
287287
w.WriteHeader(http.StatusInternalServerError)
288-
w.Write([]byte(payload))
288+
_, _ = w.Write([]byte(payload))
289289
}
290290

291291
// Only user with `user:update` credential can update user profile.
@@ -301,7 +301,7 @@ func UpdateUser(w http.ResponseWriter, r *http.Request) {
301301
"msg": utils.ValidatorErrors(err),
302302
})
303303
w.WriteHeader(http.StatusInternalServerError)
304-
w.Write([]byte(payload))
304+
_, _ = w.Write([]byte(payload))
305305
}
306306

307307
// Set user data to update:
@@ -315,7 +315,7 @@ func UpdateUser(w http.ResponseWriter, r *http.Request) {
315315
"msg": err.Error(),
316316
})
317317
w.WriteHeader(http.StatusInternalServerError)
318-
w.Write([]byte(payload))
318+
_, _ = w.Write([]byte(payload))
319319
}
320320
} else {
321321
// Return status 403 and permission denied error.
@@ -325,7 +325,7 @@ func UpdateUser(w http.ResponseWriter, r *http.Request) {
325325
"user": nil,
326326
})
327327
w.WriteHeader(http.StatusForbidden)
328-
w.Write([]byte(payload))
328+
_, _ = w.Write([]byte(payload))
329329
}
330330

331331
payload, _ := json.Marshal(map[string]interface{}{
@@ -334,7 +334,7 @@ func UpdateUser(w http.ResponseWriter, r *http.Request) {
334334
"user": user,
335335
})
336336
w.WriteHeader(http.StatusOK)
337-
w.Write([]byte(payload))
337+
_, _ = w.Write([]byte(payload))
338338
}
339339

340340
// DeleteUser func for deletes user by given ID.
@@ -346,7 +346,7 @@ func UpdateUser(w http.ResponseWriter, r *http.Request) {
346346
// @Param id body string true "User ID"
347347
// @Success 200 {string} string "ok"
348348
// @Router /api/private/user [delete]
349-
func DeleteUser(w http.ResponseWriter, r *http.Request) error {
349+
func DeleteUser(w http.ResponseWriter, r *http.Request) {
350350
// Define content type.
351351
w.Header().Set("Content-Type", "application/json")
352352

@@ -374,7 +374,7 @@ func DeleteUser(w http.ResponseWriter, r *http.Request) error {
374374
"msg": err.Error(),
375375
})
376376
w.WriteHeader(http.StatusInternalServerError)
377-
w.Write([]byte(payload))
377+
_, _ = w.Write([]byte(payload))
378378
}
379379

380380
// Create database connection.
@@ -386,7 +386,7 @@ func DeleteUser(w http.ResponseWriter, r *http.Request) error {
386386
"msg": err.Error(),
387387
})
388388
w.WriteHeader(http.StatusInternalServerError)
389-
w.Write([]byte(payload))
389+
_, _ = w.Write([]byte(payload))
390390
}
391391

392392
// Checking, if user with given ID is exists.
@@ -397,7 +397,7 @@ func DeleteUser(w http.ResponseWriter, r *http.Request) error {
397397
"msg": "user not found",
398398
})
399399
w.WriteHeader(http.StatusNotFound)
400-
w.Write([]byte(payload))
400+
_, _ = w.Write([]byte(payload))
401401
}
402402

403403
// Only user with `user:delete` credential can delete user profile.
@@ -410,7 +410,7 @@ func DeleteUser(w http.ResponseWriter, r *http.Request) error {
410410
"msg": err.Error(),
411411
})
412412
w.WriteHeader(http.StatusInternalServerError)
413-
w.Write([]byte(payload))
413+
_, _ = w.Write([]byte(payload))
414414
}
415415
} else {
416416
// Return status 403 and permission denied error.
@@ -419,12 +419,13 @@ func DeleteUser(w http.ResponseWriter, r *http.Request) error {
419419
"msg": "permission denied, check credentials or expiration time of your token",
420420
})
421421
w.WriteHeader(http.StatusForbidden)
422-
w.Write([]byte(payload))
422+
_, _ = w.Write([]byte(payload))
423423
}
424424

425-
w.WriteHeader(http.StatusOK)
426-
return json.NewEncoder(w).Encode(map[string]interface{}{
425+
payload, _ := json.Marshal(map[string]interface{}{
427426
"error": false,
428427
"msg": nil,
429428
})
429+
w.WriteHeader(http.StatusOK)
430+
_, _ = w.Write([]byte(payload))
430431
}

0 commit comments

Comments
 (0)