Skip to content

Commit 8e9349c

Browse files
committed
Updated linting errors and workflow file
1 parent f6e2186 commit 8e9349c

File tree

4 files changed

+44
-18
lines changed

4 files changed

+44
-18
lines changed

.github/workflows/workflow.yml

+18-7
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ on:
99
- main
1010

1111
jobs:
12-
build:
12+
lint:
1313
runs-on: ubuntu-latest
1414
steps:
1515
- uses: actions/checkout@v4
@@ -24,10 +24,13 @@ jobs:
2424
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
2525
restore-keys: |
2626
${{ runner.os }}-go-
27-
- name: Build
28-
run: go build -v ./...
27+
- name: Lint
28+
uses: golangci/golangci-lint-action@v3
29+
with:
30+
version: latest
2931

30-
lint:
32+
build:
33+
needs: lint
3134
runs-on: ubuntu-latest
3235
steps:
3336
- uses: actions/checkout@v4
@@ -42,12 +45,16 @@ jobs:
4245
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
4346
restore-keys: |
4447
${{ runner.os }}-go-
45-
- name: Lint
46-
uses: golangci/golangci-lint-action@v3
48+
- name: Build
49+
run: go build -v ./...
50+
- name: Upload build artifact
51+
uses: actions/upload-artifact@v3
4752
with:
48-
version: latest
53+
name: app-binary
54+
path: ./your-app-binary-name
4955

5056
test:
57+
needs: build
5158
runs-on: ubuntu-latest
5259
services:
5360
postgres:
@@ -77,6 +84,10 @@ jobs:
7784
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
7885
restore-keys: |
7986
${{ runner.os }}-go-
87+
- name: Download build artifact
88+
uses: actions/download-artifact@v3
89+
with:
90+
name: app-binary
8091
- name: Install golang-migrate
8192
run: go install -tags 'postgres' github.com/golang-migrate/migrate/v4/cmd/migrate@latest
8293
- name: Run Migrations

api/routes.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,11 @@ func SetupRoutes() *chi.Mux {
3030

3131
// Health check
3232
r.Get("/health", func(w http.ResponseWriter, r *http.Request) {
33-
common.WriteJSON(w, http.StatusOK, common.Envelope{"message": "Health Check OK"}, nil)
33+
err := common.WriteJSON(w, http.StatusOK, common.Envelope{"message": "Health Check OK"}, nil)
34+
if err != nil {
35+
common.ServerErrorResponse(w, r, err)
36+
return
37+
}
3438
})
3539

3640
// API routes

internal/book/handler.go

+20-8
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,11 @@ func (h *BookHandler) CreateBook(w http.ResponseWriter, r *http.Request) {
6767
CreatedAt: createdBook.CreatedAt,
6868
}
6969

70-
common.WriteJSON(w, http.StatusCreated, common.Envelope{"book": resp}, nil)
71-
70+
err = common.WriteJSON(w, http.StatusCreated, common.Envelope{"book": resp}, nil)
71+
if err != nil {
72+
common.ServerErrorResponse(w, r, err)
73+
return
74+
}
7275
}
7376

7477
// GetBookById godoc
@@ -110,8 +113,11 @@ func (h *BookHandler) GetBookById(w http.ResponseWriter, r *http.Request) {
110113
UpdatedAt: book.UpdatedAt,
111114
}
112115

113-
common.WriteJSON(w, http.StatusOK, common.Envelope{"book": resp}, nil)
114-
116+
err = common.WriteJSON(w, http.StatusOK, common.Envelope{"book": resp}, nil)
117+
if err != nil {
118+
common.ServerErrorResponse(w, r, err)
119+
return
120+
}
115121
}
116122

117123
// UpdateBook godoc
@@ -177,8 +183,11 @@ func (h *BookHandler) UpdateBook(w http.ResponseWriter, r *http.Request) {
177183
CreatedAt: book.CreatedAt,
178184
}
179185

180-
common.WriteJSON(w, http.StatusOK, common.Envelope{"book": resp}, nil)
181-
186+
err = common.WriteJSON(w, http.StatusOK, common.Envelope{"book": resp}, nil)
187+
if err != nil {
188+
common.ServerErrorResponse(w, r, err)
189+
return
190+
}
182191
}
183192

184193
// DeleteBook godoc
@@ -210,6 +219,9 @@ func (h *BookHandler) DeleteBook(w http.ResponseWriter, r *http.Request) {
210219
return
211220
}
212221

213-
common.WriteJSON(w, http.StatusOK, common.Envelope{"message": "Successfully deleted book"}, nil)
214-
222+
err = common.WriteJSON(w, http.StatusOK, common.Envelope{"message": "Successfully deleted book"}, nil)
223+
if err != nil {
224+
common.ServerErrorResponse(w, r, err)
225+
return
226+
}
215227
}

pkg/common/helpers.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,7 @@ func WriteJSON(w http.ResponseWriter, status int, data Envelope, headers http.He
3636

3737
w.Header().Set("Content-Type", "application/json")
3838
w.WriteHeader(status)
39-
w.Write(js)
40-
39+
_, _ = w.Write(js)
4140
return nil
4241
}
4342

0 commit comments

Comments
 (0)