Skip to content

Commit 3639b5d

Browse files
committed
Lint fix
1 parent 4cb0969 commit 3639b5d

File tree

7 files changed

+72
-45
lines changed

7 files changed

+72
-45
lines changed

services/community/.go-version

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
1.23.6

services/community/api/auth/token.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,11 @@ func ExtractTokenID(r *http.Request, db *gorm.DB) (uint32, error) {
7171
log.Println(err)
7272
return 0, err
7373
}
74-
defer resp.Body.Close()
74+
defer func() {
75+
if err := resp.Body.Close(); err != nil {
76+
log.Println("Error closing response body:", err)
77+
}
78+
}()
7579

7680
tokenValid := resp.StatusCode == 200
7781
token, _, err := new(jwt.Parser).ParseUnverified(tokenString, jwt.MapClaims{})

services/community/api/controllers/coupon_controller.go

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,16 @@ import (
2525
"go.mongodb.org/mongo-driver/bson"
2626
)
2727

28-
//AddNewCoupon Coupon add coupon in database
29-
//@params ResponseWriter, Request
30-
//Server have database connection
28+
// AddNewCoupon Coupon add coupon in database
29+
// @params ResponseWriter, Request
30+
// Server have database connection
3131
func (s *Server) AddNewCoupon(w http.ResponseWriter, r *http.Request) {
3232
body, err := io.ReadAll(r.Body)
33-
defer r.Body.Close()
33+
defer func() {
34+
if err := r.Body.Close(); err != nil {
35+
log.Println("Error closing request body:", err)
36+
}
37+
}()
3438
if err != nil {
3539
responses.ERROR(w, http.StatusBadRequest, err)
3640
return
@@ -52,20 +56,24 @@ func (s *Server) AddNewCoupon(w http.ResponseWriter, r *http.Request) {
5256

5357
}
5458

55-
//ValidateCoupon Coupon check coupon in database, if coupon code is valid it returns
56-
//@return
57-
//@params ResponseWriter, Request
58-
//Server have database connection
59+
// ValidateCoupon Coupon check coupon in database, if coupon code is valid it returns
60+
// @return
61+
// @params ResponseWriter, Request
62+
// Server have database connection
5963
func (s *Server) ValidateCoupon(w http.ResponseWriter, r *http.Request) {
6064

6165
//coupon := models.CouponBody{}
6266
var bsonMap bson.M
6367

6468
body, err := io.ReadAll(r.Body)
65-
defer r.Body.Close()
69+
defer func() {
70+
if err := r.Body.Close(); err != nil {
71+
log.Println("Error closing request body:", err)
72+
}
73+
}()
6674
if err != nil {
6775
responses.ERROR(w, http.StatusBadRequest, err)
68-
log.Println("No payload for ValidateCoupon", body, err)
76+
log.Println("No payload for ValidateCoupon", string(body), err)
6977
return
7078
}
7179
err = json.Unmarshal(body, &bsonMap)

services/community/api/controllers/post_controller.go

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package controllers
1717
import (
1818
"encoding/json"
1919
"io"
20+
"log"
2021
"net/http"
2122
"strconv"
2223

@@ -25,14 +26,18 @@ import (
2526
"github.com/gorilla/mux"
2627
)
2728

28-
//AddNewPost add post in database,
29-
//@return HTTP Status
30-
//@params ResponseWriter, Request
31-
//Server have database connection
29+
// AddNewPost add post in database,
30+
// @return HTTP Status
31+
// @params ResponseWriter, Request
32+
// Server have database connection
3233
func (s *Server) AddNewPost(w http.ResponseWriter, r *http.Request) {
3334

3435
body, err := io.ReadAll(r.Body)
35-
defer r.Body.Close()
36+
defer func() {
37+
if err := r.Body.Close(); err != nil {
38+
log.Println("Error closing request body:", err)
39+
}
40+
}()
3641
if err != nil {
3742
responses.ERROR(w, http.StatusBadRequest, err)
3843
return
@@ -54,10 +59,10 @@ func (s *Server) AddNewPost(w http.ResponseWriter, r *http.Request) {
5459
responses.JSON(w, http.StatusOK, savedPost)
5560
}
5661

57-
//GetPostByID fetch the post by ID,
58-
//@return HTTP Status
59-
//@params ResponseWriter, Request
60-
//Server have database connection
62+
// GetPostByID fetch the post by ID,
63+
// @return HTTP Status
64+
// @params ResponseWriter, Request
65+
// Server have database connection
6166
func (s *Server) GetPostByID(w http.ResponseWriter, r *http.Request) {
6267

6368
vars := mux.Vars(r)
@@ -71,9 +76,7 @@ func (s *Server) GetPostByID(w http.ResponseWriter, r *http.Request) {
7176

7277
}
7378

74-
75-
76-
//GetPost Vulnerabilities
79+
// GetPost Vulnerabilities
7780
func (s *Server) GetPost(w http.ResponseWriter, r *http.Request) {
7881
//post := models.Post{}
7982
limit_param := r.URL.Query().Get("limit")
@@ -107,15 +110,19 @@ func (s *Server) GetPost(w http.ResponseWriter, r *http.Request) {
107110
responses.JSON(w, http.StatusOK, posts)
108111
}
109112

110-
//Comment will add comment in perticular post,
111-
//@return HTTP Post Object
112-
//@params ResponseWriter, Request
113-
//Server have database connection
113+
// Comment will add comment in perticular post,
114+
// @return HTTP Post Object
115+
// @params ResponseWriter, Request
116+
// Server have database connection
114117
func (s *Server) Comment(w http.ResponseWriter, r *http.Request) {
115118

116119
vars := mux.Vars(r)
117120
body, err := io.ReadAll(r.Body)
118-
defer r.Body.Close()
121+
defer func() {
122+
if err := r.Body.Close(); err != nil {
123+
log.Println("Error closing request body:", err)
124+
}
125+
}()
119126
if err != nil {
120127
responses.ERROR(w, http.StatusBadRequest, err)
121128
return

services/community/api/models/post.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,18 @@ import (
2828
"go.mongodb.org/mongo-driver/mongo/options"
2929
)
3030

31-
//Post Field
31+
// Post Field
3232
type Post struct {
3333
ID string `gorm:"primary_key;auto_increment" json:"id"`
3434
Title string `gorm:"size:255;not null;unique" json:"title"`
3535
Content string `gorm:"size:255;not null;" json:"content"`
36-
Author Author `json:"author"`
36+
Author Author `json:"author"`
3737
Comments []Comments `json:"comments"`
3838
AuthorID uint64 `sql:"type:int REFERENCES users(id)" json:"authorid"`
3939
CreatedAt time.Time
4040
}
4141

42-
//Prepare initialize data
42+
// Prepare initialize data
4343
func (post *Post) Prepare() {
4444
post.ID = shortuuid.New()
4545
post.Title = html.EscapeString(strings.TrimSpace(post.Title))
@@ -51,13 +51,13 @@ func (post *Post) Prepare() {
5151
}
5252

5353
type PostsResponse struct {
54-
Posts []Post `json:"posts"`
54+
Posts []Post `json:"posts"`
5555
NextOffset *int64 `json:"next_offset"`
5656
PrevOffset *int64 `json:"previous_offset"`
57-
Total int `json:"total"`
57+
Total int `json:"total"`
5858
}
5959

60-
//Validate data of post
60+
// Validate data of post
6161
func (post *Post) Validate() error {
6262

6363
if post.Title == "" {
@@ -72,7 +72,7 @@ func (post *Post) Validate() error {
7272
return nil
7373
}
7474

75-
//Prepare initialize Field
75+
// Prepare initialize Field
7676
func Prepare() Author {
7777
var u Author
7878
u.Nickname = nickname
@@ -83,7 +83,7 @@ func Prepare() Author {
8383
return u
8484
}
8585

86-
//SavePost persits data into database
86+
// SavePost persits data into database
8787
func SavePost(client *mongo.Client, post Post) (Post, error) {
8888

8989
collection := client.Database("crapi").Collection("post")
@@ -95,7 +95,7 @@ func SavePost(client *mongo.Client, post Post) (Post, error) {
9595
return post, err
9696
}
9797

98-
//GetPostByID fetch post by postId
98+
// GetPostByID fetch post by postId
9999
func GetPostByID(client *mongo.Client, ID string) (Post, error) {
100100
var post Post
101101

@@ -111,10 +111,10 @@ func GetPostByID(client *mongo.Client, ID string) (Post, error) {
111111

112112
}
113113

114-
//FindAllPost return all recent post
114+
// FindAllPost return all recent post
115115
func FindAllPost(client *mongo.Client, offset int64, limit int64) (PostsResponse, error) {
116116
postList := []Post{}
117-
var postsResponse PostsResponse = PostsResponse{}
117+
postsResponse := PostsResponse{}
118118
options := options.Find()
119119
options.SetSort(bson.D{{Key: "_id", Value: -1}})
120120
options.SetLimit(limit)
@@ -143,11 +143,11 @@ func FindAllPost(client *mongo.Client, offset int64, limit int64) (PostsResponse
143143
log.Println("Error in counting posts: ", err1)
144144
return postsResponse, err1
145145
}
146-
if offset - limit >= 0 {
146+
if offset-limit >= 0 {
147147
tempOffset := offset - limit
148148
postsResponse.PrevOffset = &tempOffset
149149
}
150-
if offset + limit < count {
150+
if offset+limit < count {
151151
tempOffset := offset + limit
152152
postsResponse.NextOffset = &tempOffset
153153
}

services/community/api/responses/json.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,22 @@ package responses
1717
import (
1818
"encoding/json"
1919
"fmt"
20+
"log"
2021
"net/http"
2122
)
2223

23-
//JSON return response in json
24+
// JSON return response in json
2425
func JSON(w http.ResponseWriter, statusCode int, data interface{}) {
2526
w.WriteHeader(statusCode)
2627
err := json.NewEncoder(w).Encode(data)
2728
if err != nil {
28-
fmt.Fprintf(w, "%s", err.Error())
29+
if _, writeErr := fmt.Fprintf(w, "%s", err.Error()); writeErr != nil {
30+
log.Println("Error writing error response:", writeErr)
31+
}
2932
}
3033
}
3134

32-
//ERROR return
35+
// ERROR return
3336
func ERROR(w http.ResponseWriter, statusCode int, err error) {
3437
if err != nil {
3538
JSON(w, statusCode, struct {

services/community/api/server.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,11 @@ func identityServiceHealthCheck() {
5959
attempts++
6060
continue
6161
}
62-
defer resp.Body.Close()
62+
defer func() {
63+
if err := resp.Body.Close(); err != nil {
64+
log.Println("Error closing response body:", err)
65+
}
66+
}()
6367
if resp.StatusCode != http.StatusOK {
6468
log.Printf("Identity service is not healthy: %v", resp.Status)
6569
log.Printf("Retrying in 5 seconds...")

0 commit comments

Comments
 (0)