Skip to content

Commit 80176b3

Browse files
authored
Merge pull request #18 from chris-ramon/golangci-lint
`{bin,domain,pkg}`: Adds golangci-lint & fixes.
2 parents 5a8c62d + 54c67dc commit 80176b3

File tree

4 files changed

+36
-8
lines changed

4 files changed

+36
-8
lines changed

bin/lint.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/bin/bash
2+
3+
if ! golangci-lint version &> /dev/null
4+
then
5+
echo "golangci-lint not found, installing it"
6+
go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest
7+
fi
8+
9+
golangci-lint run ./...

domain/auth/handler.go

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package auth
33
import (
44
"context"
55
"encoding/json"
6-
"io/ioutil"
6+
"io"
77
"log"
88
"net/http"
99

@@ -22,7 +22,11 @@ type handlers struct {
2222

2323
func (h *handlers) GetPing() http.HandlerFunc {
2424
return func(w http.ResponseWriter, r *http.Request) {
25-
w.Write([]byte("ok"))
25+
if _, err := w.Write([]byte("ok")); err != nil {
26+
log.Printf("failed to write to reponse writer: %v", err)
27+
w.WriteHeader(http.StatusInternalServerError)
28+
return
29+
}
2630
}
2731
}
2832

@@ -43,13 +47,17 @@ func (h *handlers) GetCurrentUser() http.HandlerFunc {
4347
return
4448
}
4549

46-
w.Write([]byte(u.Username))
50+
if _, err := w.Write([]byte(u.Username)); err != nil {
51+
log.Printf("failed to write to reponse writer: %v", err)
52+
w.WriteHeader(http.StatusInternalServerError)
53+
return
54+
}
4755
}
4856
}
4957

5058
func (h *handlers) PostSignIn() http.HandlerFunc {
5159
return func(w http.ResponseWriter, r *http.Request) {
52-
b, err := ioutil.ReadAll(r.Body)
60+
b, err := io.ReadAll(r.Body)
5361
if err != nil {
5462
log.Printf("failed to read request body: %v", err)
5563
http.Error(w, "failed to read request body", http.StatusInternalServerError)
@@ -74,7 +82,11 @@ func (h *handlers) PostSignIn() http.HandlerFunc {
7482
return
7583
}
7684

77-
w.Write([]byte(u.Username))
85+
if _, err := w.Write([]byte(u.Username)); err != nil {
86+
log.Printf("failed to write to reponse writer: %v", err)
87+
w.WriteHeader(http.StatusInternalServerError)
88+
return
89+
}
7890

7991
}
8092
}

domain/users/handler.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package users
33
import (
44
"context"
55
"encoding/json"
6+
"log"
67
"net/http"
78

89
"github.com/admin-golang/admin/dataloader"
@@ -48,7 +49,11 @@ func (h *handlers) GetUsers() http.HandlerFunc {
4849
return
4950
}
5051

51-
w.Write(resp)
52+
if _, err := w.Write(resp); err != nil {
53+
log.Printf("failed to write to reponse writer: %v", err)
54+
w.WriteHeader(http.StatusInternalServerError)
55+
return
56+
}
5257
}
5358
}
5459

pkg/ctxutil/ctxutil.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@ import (
77
"strings"
88
)
99

10-
const AuthHeaderName = "Authorization"
10+
type AuthHeaderNameType string
11+
12+
const AuthHeaderName AuthHeaderNameType = "Authorization"
1113

1214
func WithAuthHeader(ctx context.Context, header http.Header) context.Context {
13-
return context.WithValue(ctx, AuthHeaderName, header.Get(AuthHeaderName))
15+
return context.WithValue(ctx, AuthHeaderName, header.Get(string(AuthHeaderName)))
1416
}
1517

1618
func AuthHeaderValueFromCtx(ctx context.Context) (string, error) {

0 commit comments

Comments
 (0)