Skip to content

Commit 9a2e5c9

Browse files
committed
Improvements to enforce middleware, linting, returning 404 properly
1 parent 833dd23 commit 9a2e5c9

File tree

18 files changed

+110
-104
lines changed

18 files changed

+110
-104
lines changed

backend/internal/api/handler/auth.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import (
1111
"npm/internal/entity/user"
1212
"npm/internal/errors"
1313
"npm/internal/logger"
14+
15+
"gorm.io/gorm"
1416
)
1517

1618
type setAuthModel struct {
@@ -41,7 +43,10 @@ func SetAuth() func(http.ResponseWriter, *http.Request) {
4143

4244
// Load user
4345
thisUser, thisUserErr := user.GetByID(userID)
44-
if thisUserErr != nil {
46+
if thisUserErr == gorm.ErrRecordNotFound {
47+
h.NotFound(w, r)
48+
return
49+
} else if thisUserErr != nil {
4550
h.ResultErrorJSON(w, r, http.StatusBadRequest, thisUserErr.Error(), nil)
4651
return
4752
}

backend/internal/api/handler/certificate_authorities.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package handler
22

33
import (
4-
"database/sql"
54
"encoding/json"
65
"fmt"
76
"net/http"
@@ -12,6 +11,8 @@ import (
1211
"npm/internal/api/middleware"
1312
"npm/internal/entity/certificateauthority"
1413
"npm/internal/logger"
14+
15+
"gorm.io/gorm"
1516
)
1617

1718
// GetCertificateAuthorities will return a list of Certificate Authorities
@@ -46,7 +47,7 @@ func GetCertificateAuthority() func(http.ResponseWriter, *http.Request) {
4647

4748
item, err := certificateauthority.GetByID(caID)
4849
switch err {
49-
case sql.ErrNoRows:
50+
case gorm.ErrRecordNotFound:
5051
h.NotFound(w, r)
5152
case nil:
5253
h.ResultResponseJSON(w, r, http.StatusOK, item)
@@ -100,7 +101,7 @@ func UpdateCertificateAuthority() func(http.ResponseWriter, *http.Request) {
100101

101102
ca, err := certificateauthority.GetByID(caID)
102103
switch err {
103-
case sql.ErrNoRows:
104+
case gorm.ErrRecordNotFound:
104105
h.NotFound(w, r)
105106
case nil:
106107
bodyBytes, _ := r.Context().Value(c.BodyCtxKey).([]byte)
@@ -140,7 +141,7 @@ func DeleteCertificateAuthority() func(http.ResponseWriter, *http.Request) {
140141

141142
item, err := certificateauthority.GetByID(caID)
142143
switch err {
143-
case sql.ErrNoRows:
144+
case gorm.ErrRecordNotFound:
144145
h.NotFound(w, r)
145146
case nil:
146147
h.ResultResponseJSON(w, r, http.StatusOK, item.Delete())

backend/internal/api/handler/certificates.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package handler
22

33
import (
4-
"database/sql"
54
"encoding/json"
65
"fmt"
76
"net/http"
@@ -14,6 +13,8 @@ import (
1413
"npm/internal/entity/host"
1514
"npm/internal/jobqueue"
1615
"npm/internal/logger"
16+
17+
"gorm.io/gorm"
1718
)
1819

1920
// GetCertificates will return a list of Certificates
@@ -138,7 +139,7 @@ func getCertificateFromRequest(w http.ResponseWriter, r *http.Request) *certific
138139

139140
certificateObject, err := certificate.GetByID(certificateID)
140141
switch err {
141-
case sql.ErrNoRows:
142+
case gorm.ErrRecordNotFound:
142143
h.NotFound(w, r)
143144
case nil:
144145
return &certificateObject

backend/internal/api/handler/dns_providers.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package handler
22

33
import (
4-
"database/sql"
54
"encoding/json"
65
"fmt"
76
"net/http"
@@ -12,6 +11,8 @@ import (
1211
"npm/internal/dnsproviders"
1312
"npm/internal/entity/dnsprovider"
1413
"npm/internal/errors"
14+
15+
"gorm.io/gorm"
1516
)
1617

1718
// GetDNSProviders will return a list of DNS Providers
@@ -46,7 +47,7 @@ func GetDNSProvider() func(http.ResponseWriter, *http.Request) {
4647

4748
item, err := dnsprovider.GetByID(providerID)
4849
switch err {
49-
case sql.ErrNoRows:
50+
case gorm.ErrRecordNotFound:
5051
h.NotFound(w, r)
5152
case nil:
5253
h.ResultResponseJSON(w, r, http.StatusOK, item)
@@ -95,7 +96,7 @@ func UpdateDNSProvider() func(http.ResponseWriter, *http.Request) {
9596

9697
item, err := dnsprovider.GetByID(providerID)
9798
switch err {
98-
case sql.ErrNoRows:
99+
case gorm.ErrRecordNotFound:
99100
h.NotFound(w, r)
100101
case nil:
101102
bodyBytes, _ := r.Context().Value(c.BodyCtxKey).([]byte)
@@ -130,7 +131,7 @@ func DeleteDNSProvider() func(http.ResponseWriter, *http.Request) {
130131

131132
item, err := dnsprovider.GetByID(providerID)
132133
switch err {
133-
case sql.ErrNoRows:
134+
case gorm.ErrRecordNotFound:
134135
h.NotFound(w, r)
135136
case nil:
136137
h.ResultResponseJSON(w, r, http.StatusOK, item.Delete())

backend/internal/api/handler/hosts.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package handler
22

33
import (
4-
"database/sql"
54
"encoding/json"
65
"fmt"
76
"net/http"
@@ -14,6 +13,8 @@ import (
1413
"npm/internal/logger"
1514
"npm/internal/nginx"
1615
"npm/internal/validator"
16+
17+
"gorm.io/gorm"
1718
)
1819

1920
// GetHosts will return a list of Hosts
@@ -48,7 +49,7 @@ func GetHost() func(http.ResponseWriter, *http.Request) {
4849

4950
item, err := host.GetByID(hostID)
5051
switch err {
51-
case sql.ErrNoRows:
52+
case gorm.ErrRecordNotFound:
5253
h.NotFound(w, r)
5354
case nil:
5455
// nolint: errcheck,gosec
@@ -111,7 +112,7 @@ func UpdateHost() func(http.ResponseWriter, *http.Request) {
111112

112113
hostObject, err := host.GetByID(hostID)
113114
switch err {
114-
case sql.ErrNoRows:
115+
case gorm.ErrRecordNotFound:
115116
h.NotFound(w, r)
116117
case nil:
117118
bodyBytes, _ := r.Context().Value(c.BodyCtxKey).([]byte)
@@ -156,7 +157,7 @@ func DeleteHost() func(http.ResponseWriter, *http.Request) {
156157

157158
item, err := host.GetByID(hostID)
158159
switch err {
159-
case sql.ErrNoRows:
160+
case gorm.ErrRecordNotFound:
160161
h.NotFound(w, r)
161162
case nil:
162163
h.ResultResponseJSON(w, r, http.StatusOK, item.Delete())
@@ -181,7 +182,7 @@ func GetHostNginxConfig(format string) func(http.ResponseWriter, *http.Request)
181182

182183
item, err := host.GetByID(hostID)
183184
switch err {
184-
case sql.ErrNoRows:
185+
case gorm.ErrRecordNotFound:
185186
h.NotFound(w, r)
186187
case nil:
187188
// Get the config from disk

backend/internal/api/handler/nginx_templates.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package handler
22

33
import (
4-
"database/sql"
54
"encoding/json"
65
"fmt"
76
"net/http"
@@ -10,6 +9,8 @@ import (
109
h "npm/internal/api/http"
1110
"npm/internal/api/middleware"
1211
"npm/internal/entity/nginxtemplate"
12+
13+
"gorm.io/gorm"
1314
)
1415

1516
// GetNginxTemplates will return a list of Nginx Templates
@@ -44,7 +45,7 @@ func GetNginxTemplate() func(http.ResponseWriter, *http.Request) {
4445

4546
item, err := nginxtemplate.GetByID(templateID)
4647
switch err {
47-
case sql.ErrNoRows:
48+
case gorm.ErrRecordNotFound:
4849
h.NotFound(w, r)
4950
case nil:
5051
h.ResultResponseJSON(w, r, http.StatusOK, item)
@@ -95,7 +96,7 @@ func UpdateNginxTemplate() func(http.ResponseWriter, *http.Request) {
9596

9697
nginxTemplate, err := nginxtemplate.GetByID(templateID)
9798
switch err {
98-
case sql.ErrNoRows:
99+
case gorm.ErrRecordNotFound:
99100
h.NotFound(w, r)
100101
case nil:
101102
bodyBytes, _ := r.Context().Value(c.BodyCtxKey).([]byte)
@@ -130,7 +131,7 @@ func DeleteNginxTemplate() func(http.ResponseWriter, *http.Request) {
130131

131132
item, err := nginxtemplate.GetByID(templateID)
132133
switch err {
133-
case sql.ErrNoRows:
134+
case gorm.ErrRecordNotFound:
134135
h.NotFound(w, r)
135136
case nil:
136137
h.ResultResponseJSON(w, r, http.StatusOK, item.Delete())

backend/internal/api/handler/settings.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package handler
22

33
import (
4-
"database/sql"
54
"encoding/json"
65
"fmt"
76
"net/http"
@@ -12,6 +11,7 @@ import (
1211
"npm/internal/entity/setting"
1312

1413
"github.com/go-chi/chi/v5"
14+
"gorm.io/gorm"
1515
)
1616

1717
// GetSettings will return a list of Settings
@@ -41,7 +41,7 @@ func GetSetting() func(http.ResponseWriter, *http.Request) {
4141

4242
item, err := setting.GetByName(name)
4343
switch err {
44-
case sql.ErrNoRows:
44+
case gorm.ErrRecordNotFound:
4545
h.NotFound(w, r)
4646
case nil:
4747
h.ResultResponseJSON(w, r, http.StatusOK, item)
@@ -81,7 +81,7 @@ func UpdateSetting() func(http.ResponseWriter, *http.Request) {
8181

8282
setting, err := setting.GetByName(settingName)
8383
switch err {
84-
case sql.ErrNoRows:
84+
case gorm.ErrRecordNotFound:
8585
h.NotFound(w, r)
8686
case nil:
8787
bodyBytes, _ := r.Context().Value(c.BodyCtxKey).([]byte)

backend/internal/api/handler/streams.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package handler
22

33
import (
4-
"database/sql"
54
"encoding/json"
65
"fmt"
76
"net/http"
@@ -10,6 +9,8 @@ import (
109
h "npm/internal/api/http"
1110
"npm/internal/api/middleware"
1211
"npm/internal/entity/stream"
12+
13+
"gorm.io/gorm"
1314
)
1415

1516
// GetStreams will return a list of Streams
@@ -44,7 +45,7 @@ func GetStream() func(http.ResponseWriter, *http.Request) {
4445

4546
item, err := stream.GetByID(hostID)
4647
switch err {
47-
case sql.ErrNoRows:
48+
case gorm.ErrRecordNotFound:
4849
h.NotFound(w, r)
4950
case nil:
5051
h.ResultResponseJSON(w, r, http.StatusOK, item)
@@ -93,7 +94,7 @@ func UpdateStream() func(http.ResponseWriter, *http.Request) {
9394

9495
host, err := stream.GetByID(hostID)
9596
switch err {
96-
case sql.ErrNoRows:
97+
case gorm.ErrRecordNotFound:
9798
h.NotFound(w, r)
9899
case nil:
99100
bodyBytes, _ := r.Context().Value(c.BodyCtxKey).([]byte)
@@ -128,7 +129,7 @@ func DeleteStream() func(http.ResponseWriter, *http.Request) {
128129

129130
item, err := stream.GetByID(hostID)
130131
switch err {
131-
case sql.ErrNoRows:
132+
case gorm.ErrRecordNotFound:
132133
h.NotFound(w, r)
133134
case nil:
134135
h.ResultResponseJSON(w, r, http.StatusOK, item.Delete())

backend/internal/api/handler/upstreams.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package handler
22

33
import (
4-
"database/sql"
54
"encoding/json"
65
"fmt"
76
"net/http"
@@ -15,6 +14,8 @@ import (
1514
"npm/internal/logger"
1615
"npm/internal/nginx"
1716
"npm/internal/validator"
17+
18+
"gorm.io/gorm"
1819
)
1920

2021
// GetUpstreams will return a list of Upstreams
@@ -49,7 +50,7 @@ func GetUpstream() func(http.ResponseWriter, *http.Request) {
4950

5051
item, err := upstream.GetByID(upstreamID)
5152
switch err {
52-
case sql.ErrNoRows:
53+
case gorm.ErrRecordNotFound:
5354
h.NotFound(w, r)
5455
case nil:
5556
// nolint: errcheck,gosec
@@ -149,7 +150,7 @@ func DeleteUpstream() func(http.ResponseWriter, *http.Request) {
149150

150151
item, err := upstream.GetByID(upstreamID)
151152
switch err {
152-
case sql.ErrNoRows:
153+
case gorm.ErrRecordNotFound:
153154
h.NotFound(w, r)
154155
case nil:
155156
// Ensure that this upstream isn't in use by a host
@@ -180,7 +181,7 @@ func GetUpstreamNginxConfig(format string) func(http.ResponseWriter, *http.Reque
180181

181182
item, err := upstream.GetByID(upstreamID)
182183
switch err {
183-
case sql.ErrNoRows:
184+
case gorm.ErrRecordNotFound:
184185
h.NotFound(w, r)
185186
case nil:
186187
// Get the config from disk

backend/internal/api/handler/users.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package handler
22

33
import (
4-
"database/sql"
54
"encoding/json"
65
"net/http"
76

@@ -15,6 +14,7 @@ import (
1514
"npm/internal/logger"
1615

1716
"github.com/go-chi/chi/v5"
17+
"gorm.io/gorm"
1818
)
1919

2020
// GetUsers returns all users
@@ -48,7 +48,7 @@ func GetUser() func(http.ResponseWriter, *http.Request) {
4848

4949
item, err := user.GetByID(userID)
5050
switch err {
51-
case sql.ErrNoRows:
51+
case gorm.ErrRecordNotFound:
5252
h.NotFound(w, r)
5353
case nil:
5454
// nolint: errcheck,gosec
@@ -72,7 +72,7 @@ func UpdateUser() func(http.ResponseWriter, *http.Request) {
7272

7373
userObject, err := user.GetByID(userID)
7474
switch err {
75-
case sql.ErrNoRows:
75+
case gorm.ErrRecordNotFound:
7676
h.NotFound(w, r)
7777
case nil:
7878
// nolint: errcheck,gosec
@@ -136,7 +136,7 @@ func DeleteUser() func(http.ResponseWriter, *http.Request) {
136136

137137
item, err := user.GetByID(userID)
138138
switch err {
139-
case sql.ErrNoRows:
139+
case gorm.ErrRecordNotFound:
140140
h.NotFound(w, r)
141141
case nil:
142142
if err := item.Delete(); err != nil {

0 commit comments

Comments
 (0)