Improvements to enforce middleware, linting, returning 404 properly

This commit is contained in:
Jamie Curnow
2024-09-11 15:03:00 +10:00
parent 833dd23dce
commit 9a2e5c92d5
18 changed files with 110 additions and 104 deletions

View File

@ -11,6 +11,8 @@ import (
"npm/internal/entity/user"
"npm/internal/errors"
"npm/internal/logger"
"gorm.io/gorm"
)
type setAuthModel struct {
@ -41,7 +43,10 @@ func SetAuth() func(http.ResponseWriter, *http.Request) {
// Load user
thisUser, thisUserErr := user.GetByID(userID)
if thisUserErr != nil {
if thisUserErr == gorm.ErrRecordNotFound {
h.NotFound(w, r)
return
} else if thisUserErr != nil {
h.ResultErrorJSON(w, r, http.StatusBadRequest, thisUserErr.Error(), nil)
return
}

View File

@ -1,7 +1,6 @@
package handler
import (
"database/sql"
"encoding/json"
"fmt"
"net/http"
@ -12,6 +11,8 @@ import (
"npm/internal/api/middleware"
"npm/internal/entity/certificateauthority"
"npm/internal/logger"
"gorm.io/gorm"
)
// GetCertificateAuthorities will return a list of Certificate Authorities
@ -46,7 +47,7 @@ func GetCertificateAuthority() func(http.ResponseWriter, *http.Request) {
item, err := certificateauthority.GetByID(caID)
switch err {
case sql.ErrNoRows:
case gorm.ErrRecordNotFound:
h.NotFound(w, r)
case nil:
h.ResultResponseJSON(w, r, http.StatusOK, item)
@ -100,7 +101,7 @@ func UpdateCertificateAuthority() func(http.ResponseWriter, *http.Request) {
ca, err := certificateauthority.GetByID(caID)
switch err {
case sql.ErrNoRows:
case gorm.ErrRecordNotFound:
h.NotFound(w, r)
case nil:
bodyBytes, _ := r.Context().Value(c.BodyCtxKey).([]byte)
@ -140,7 +141,7 @@ func DeleteCertificateAuthority() func(http.ResponseWriter, *http.Request) {
item, err := certificateauthority.GetByID(caID)
switch err {
case sql.ErrNoRows:
case gorm.ErrRecordNotFound:
h.NotFound(w, r)
case nil:
h.ResultResponseJSON(w, r, http.StatusOK, item.Delete())

View File

@ -1,7 +1,6 @@
package handler
import (
"database/sql"
"encoding/json"
"fmt"
"net/http"
@ -14,6 +13,8 @@ import (
"npm/internal/entity/host"
"npm/internal/jobqueue"
"npm/internal/logger"
"gorm.io/gorm"
)
// GetCertificates will return a list of Certificates
@ -138,7 +139,7 @@ func getCertificateFromRequest(w http.ResponseWriter, r *http.Request) *certific
certificateObject, err := certificate.GetByID(certificateID)
switch err {
case sql.ErrNoRows:
case gorm.ErrRecordNotFound:
h.NotFound(w, r)
case nil:
return &certificateObject

View File

@ -1,7 +1,6 @@
package handler
import (
"database/sql"
"encoding/json"
"fmt"
"net/http"
@ -12,6 +11,8 @@ import (
"npm/internal/dnsproviders"
"npm/internal/entity/dnsprovider"
"npm/internal/errors"
"gorm.io/gorm"
)
// GetDNSProviders will return a list of DNS Providers
@ -46,7 +47,7 @@ func GetDNSProvider() func(http.ResponseWriter, *http.Request) {
item, err := dnsprovider.GetByID(providerID)
switch err {
case sql.ErrNoRows:
case gorm.ErrRecordNotFound:
h.NotFound(w, r)
case nil:
h.ResultResponseJSON(w, r, http.StatusOK, item)
@ -95,7 +96,7 @@ func UpdateDNSProvider() func(http.ResponseWriter, *http.Request) {
item, err := dnsprovider.GetByID(providerID)
switch err {
case sql.ErrNoRows:
case gorm.ErrRecordNotFound:
h.NotFound(w, r)
case nil:
bodyBytes, _ := r.Context().Value(c.BodyCtxKey).([]byte)
@ -130,7 +131,7 @@ func DeleteDNSProvider() func(http.ResponseWriter, *http.Request) {
item, err := dnsprovider.GetByID(providerID)
switch err {
case sql.ErrNoRows:
case gorm.ErrRecordNotFound:
h.NotFound(w, r)
case nil:
h.ResultResponseJSON(w, r, http.StatusOK, item.Delete())

View File

@ -1,7 +1,6 @@
package handler
import (
"database/sql"
"encoding/json"
"fmt"
"net/http"
@ -14,6 +13,8 @@ import (
"npm/internal/logger"
"npm/internal/nginx"
"npm/internal/validator"
"gorm.io/gorm"
)
// GetHosts will return a list of Hosts
@ -48,7 +49,7 @@ func GetHost() func(http.ResponseWriter, *http.Request) {
item, err := host.GetByID(hostID)
switch err {
case sql.ErrNoRows:
case gorm.ErrRecordNotFound:
h.NotFound(w, r)
case nil:
// nolint: errcheck,gosec
@ -111,7 +112,7 @@ func UpdateHost() func(http.ResponseWriter, *http.Request) {
hostObject, err := host.GetByID(hostID)
switch err {
case sql.ErrNoRows:
case gorm.ErrRecordNotFound:
h.NotFound(w, r)
case nil:
bodyBytes, _ := r.Context().Value(c.BodyCtxKey).([]byte)
@ -156,7 +157,7 @@ func DeleteHost() func(http.ResponseWriter, *http.Request) {
item, err := host.GetByID(hostID)
switch err {
case sql.ErrNoRows:
case gorm.ErrRecordNotFound:
h.NotFound(w, r)
case nil:
h.ResultResponseJSON(w, r, http.StatusOK, item.Delete())
@ -181,7 +182,7 @@ func GetHostNginxConfig(format string) func(http.ResponseWriter, *http.Request)
item, err := host.GetByID(hostID)
switch err {
case sql.ErrNoRows:
case gorm.ErrRecordNotFound:
h.NotFound(w, r)
case nil:
// Get the config from disk

View File

@ -1,7 +1,6 @@
package handler
import (
"database/sql"
"encoding/json"
"fmt"
"net/http"
@ -10,6 +9,8 @@ import (
h "npm/internal/api/http"
"npm/internal/api/middleware"
"npm/internal/entity/nginxtemplate"
"gorm.io/gorm"
)
// GetNginxTemplates will return a list of Nginx Templates
@ -44,7 +45,7 @@ func GetNginxTemplate() func(http.ResponseWriter, *http.Request) {
item, err := nginxtemplate.GetByID(templateID)
switch err {
case sql.ErrNoRows:
case gorm.ErrRecordNotFound:
h.NotFound(w, r)
case nil:
h.ResultResponseJSON(w, r, http.StatusOK, item)
@ -95,7 +96,7 @@ func UpdateNginxTemplate() func(http.ResponseWriter, *http.Request) {
nginxTemplate, err := nginxtemplate.GetByID(templateID)
switch err {
case sql.ErrNoRows:
case gorm.ErrRecordNotFound:
h.NotFound(w, r)
case nil:
bodyBytes, _ := r.Context().Value(c.BodyCtxKey).([]byte)
@ -130,7 +131,7 @@ func DeleteNginxTemplate() func(http.ResponseWriter, *http.Request) {
item, err := nginxtemplate.GetByID(templateID)
switch err {
case sql.ErrNoRows:
case gorm.ErrRecordNotFound:
h.NotFound(w, r)
case nil:
h.ResultResponseJSON(w, r, http.StatusOK, item.Delete())

View File

@ -1,7 +1,6 @@
package handler
import (
"database/sql"
"encoding/json"
"fmt"
"net/http"
@ -12,6 +11,7 @@ import (
"npm/internal/entity/setting"
"github.com/go-chi/chi/v5"
"gorm.io/gorm"
)
// GetSettings will return a list of Settings
@ -41,7 +41,7 @@ func GetSetting() func(http.ResponseWriter, *http.Request) {
item, err := setting.GetByName(name)
switch err {
case sql.ErrNoRows:
case gorm.ErrRecordNotFound:
h.NotFound(w, r)
case nil:
h.ResultResponseJSON(w, r, http.StatusOK, item)
@ -81,7 +81,7 @@ func UpdateSetting() func(http.ResponseWriter, *http.Request) {
setting, err := setting.GetByName(settingName)
switch err {
case sql.ErrNoRows:
case gorm.ErrRecordNotFound:
h.NotFound(w, r)
case nil:
bodyBytes, _ := r.Context().Value(c.BodyCtxKey).([]byte)

View File

@ -1,7 +1,6 @@
package handler
import (
"database/sql"
"encoding/json"
"fmt"
"net/http"
@ -10,6 +9,8 @@ import (
h "npm/internal/api/http"
"npm/internal/api/middleware"
"npm/internal/entity/stream"
"gorm.io/gorm"
)
// GetStreams will return a list of Streams
@ -44,7 +45,7 @@ func GetStream() func(http.ResponseWriter, *http.Request) {
item, err := stream.GetByID(hostID)
switch err {
case sql.ErrNoRows:
case gorm.ErrRecordNotFound:
h.NotFound(w, r)
case nil:
h.ResultResponseJSON(w, r, http.StatusOK, item)
@ -93,7 +94,7 @@ func UpdateStream() func(http.ResponseWriter, *http.Request) {
host, err := stream.GetByID(hostID)
switch err {
case sql.ErrNoRows:
case gorm.ErrRecordNotFound:
h.NotFound(w, r)
case nil:
bodyBytes, _ := r.Context().Value(c.BodyCtxKey).([]byte)
@ -128,7 +129,7 @@ func DeleteStream() func(http.ResponseWriter, *http.Request) {
item, err := stream.GetByID(hostID)
switch err {
case sql.ErrNoRows:
case gorm.ErrRecordNotFound:
h.NotFound(w, r)
case nil:
h.ResultResponseJSON(w, r, http.StatusOK, item.Delete())

View File

@ -1,7 +1,6 @@
package handler
import (
"database/sql"
"encoding/json"
"fmt"
"net/http"
@ -15,6 +14,8 @@ import (
"npm/internal/logger"
"npm/internal/nginx"
"npm/internal/validator"
"gorm.io/gorm"
)
// GetUpstreams will return a list of Upstreams
@ -49,7 +50,7 @@ func GetUpstream() func(http.ResponseWriter, *http.Request) {
item, err := upstream.GetByID(upstreamID)
switch err {
case sql.ErrNoRows:
case gorm.ErrRecordNotFound:
h.NotFound(w, r)
case nil:
// nolint: errcheck,gosec
@ -149,7 +150,7 @@ func DeleteUpstream() func(http.ResponseWriter, *http.Request) {
item, err := upstream.GetByID(upstreamID)
switch err {
case sql.ErrNoRows:
case gorm.ErrRecordNotFound:
h.NotFound(w, r)
case nil:
// Ensure that this upstream isn't in use by a host
@ -180,7 +181,7 @@ func GetUpstreamNginxConfig(format string) func(http.ResponseWriter, *http.Reque
item, err := upstream.GetByID(upstreamID)
switch err {
case sql.ErrNoRows:
case gorm.ErrRecordNotFound:
h.NotFound(w, r)
case nil:
// Get the config from disk

View File

@ -1,7 +1,6 @@
package handler
import (
"database/sql"
"encoding/json"
"net/http"
@ -15,6 +14,7 @@ import (
"npm/internal/logger"
"github.com/go-chi/chi/v5"
"gorm.io/gorm"
)
// GetUsers returns all users
@ -48,7 +48,7 @@ func GetUser() func(http.ResponseWriter, *http.Request) {
item, err := user.GetByID(userID)
switch err {
case sql.ErrNoRows:
case gorm.ErrRecordNotFound:
h.NotFound(w, r)
case nil:
// nolint: errcheck,gosec
@ -72,7 +72,7 @@ func UpdateUser() func(http.ResponseWriter, *http.Request) {
userObject, err := user.GetByID(userID)
switch err {
case sql.ErrNoRows:
case gorm.ErrRecordNotFound:
h.NotFound(w, r)
case nil:
// nolint: errcheck,gosec
@ -136,7 +136,7 @@ func DeleteUser() func(http.ResponseWriter, *http.Request) {
item, err := user.GetByID(userID)
switch err {
case sql.ErrNoRows:
case gorm.ErrRecordNotFound:
h.NotFound(w, r)
case nil:
if err := item.Delete(); err != nil {