lets all love linting

This commit is contained in:
grumbulon 2023-05-28 01:36:23 -04:00
parent 2a6c9eee4d
commit ac3d8277a1
7 changed files with 27 additions and 28 deletions

View file

@ -353,7 +353,7 @@ func (a *accountTest) CleanUp(db *gorm.DB) {
l := newResponder()
l.Response = Response{
Message: "unable to clean test database",
Err: err,
Err: err.Error(),
}
l.newLogEntry().errorLogger(l.Response)
}

View file

@ -1,7 +1,6 @@
package api
import (
"errors"
"fmt"
"net/http"
"time"
@ -29,6 +28,7 @@ import (
// @Router /api/login [post]
func Login(w http.ResponseWriter, r *http.Request) {
var result internal.User
logger, ok := r.Context().Value(keyLoggerContextID).(*Responder)
if !ok {
return
@ -50,7 +50,7 @@ func Login(w http.ResponseWriter, r *http.Request) {
logger.Response = Response{
Message: "unable to parse request",
Status: http.StatusInternalServerError,
Err: err,
Err: err.Error(),
}
logger.newLogEntry().apiError(logger.Response, w, r)
logger.newLogEntry().infoLogger(logger.Response)
@ -89,7 +89,7 @@ func Login(w http.ResponseWriter, r *http.Request) {
logger.Response = Response{
Message: "no password provided",
Status: http.StatusInternalServerError,
Err: errors.New("db connection failed"),
Err: "db connection failed",
}
logger.newLogEntry().apiError(logger.Response, w, r)
logger.newLogEntry().errorLogger(logger.Response)

View file

@ -27,7 +27,7 @@ func makeLocal(zone *ZoneRequest) error {
if err != nil {
logger.Response = Response{
Message: "no config file defined",
Err: err,
Err: err.Error(),
}
logger.newLogEntry().panicLogger(logger.Response)
@ -38,7 +38,7 @@ func makeLocal(zone *ZoneRequest) error {
if err = os.MkdirAll(path, 0o750); err != nil {
logger.Response = Response{
Message: "unable to make directory for zone files",
Err: err,
Err: err.Error(),
}
logger.newLogEntry().errorLogger(logger.Response)

View file

@ -29,7 +29,7 @@ func setDBMiddleware(next http.Handler) http.Handler {
if err != nil {
logger.Response = Response{
Message: "No config file defined",
Err: err,
Err: err.Error(),
}
logger.newLogEntry().panicLogger(logger.Response)
}
@ -39,7 +39,7 @@ func setDBMiddleware(next http.Handler) http.Handler {
if err != nil && !ok {
logger.Response = Response{
Message: "Error initializing DB",
Err: err,
Err: err.Error(),
}
logger.newLogEntry().panicLogger(logger.Response)
}
@ -59,14 +59,13 @@ func pommeLogger(next http.Handler) http.Handler {
})
}
// newLogEntry takes a Response struct and starts a new response chain
// newLogEntry takes a Response struct and starts a new response chain.
func (e *Responder) newLogEntry() *Responder {
return &Responder{}
}
// apiError sends unsuccessful API requests back to the user with the appropriate information.
func (e *Responder) apiError(logEntry Response, w http.ResponseWriter, r *http.Request) {
//log.Fatalln(e.Response, logEntry)
w.Header().Set("X-Content-Type-Options", "nosniff")
w.Header().Set("Content-Type", "application/json; charset=utf-8")
@ -78,8 +77,8 @@ func (e *Responder) apiError(logEntry Response, w http.ResponseWriter, r *http.R
w.WriteHeader(logEntry.Status)
if logEntry.Err != nil {
logEntry.Err = nil
if logEntry.Err != "" {
logEntry.Err = ""
} // nullify errors before returning to user
render.JSON(w, r, logEntry)
@ -106,5 +105,5 @@ func (e *Responder) errorLogger(logEntry Response) {
JSON: true,
})
logger.Err(logEntry.Err).Msg(logEntry.Message)
logger.Error().Msg(logEntry.Err)
}

View file

@ -61,9 +61,9 @@ type HTTPLogger[T Response] interface {
// Response is a generic response struct containing the necessary keys for a log and response action.
type Response struct {
Message string
Status int
Err string
Realm string
Err error
Status int
}
type Responder struct {

View file

@ -16,6 +16,7 @@ import (
// NewUser takes a POST request and user form and creates a user in the database.
func NewUser(w http.ResponseWriter, r *http.Request) {
var result internal.User
logger, ok := r.Context().Value(keyLoggerContextID).(*Responder)
if !ok {
return
@ -28,6 +29,7 @@ func NewUser(w http.ResponseWriter, r *http.Request) {
}
logger.newLogEntry().apiError(logger.Response, w, r)
return
}
@ -36,7 +38,7 @@ func NewUser(w http.ResponseWriter, r *http.Request) {
logger.Response = Response{
Message: "unable to parse request",
Status: http.StatusInternalServerError,
Err: err,
Err: err.Error(),
}
logger.newLogEntry().infoLogger(logger.Response)
@ -97,7 +99,7 @@ func NewUser(w http.ResponseWriter, r *http.Request) {
logger.Response = Response{
Message: "internal server error",
Status: http.StatusInternalServerError,
Err: err,
Err: err.Error(),
}
logger.newLogEntry().errorLogger(logger.Response)

View file

@ -1,7 +1,6 @@
package api
import (
"errors"
"fmt"
"io"
"log"
@ -54,7 +53,7 @@ func ReceiveFile(w http.ResponseWriter, r *http.Request) {
logger.Response = Response{
Message: "File upload failed",
Status: http.StatusInternalServerError,
Err: err,
Err: err.Error(),
}
logger.newLogEntry().errorLogger(logger.Response)
@ -70,7 +69,7 @@ func ReceiveFile(w http.ResponseWriter, r *http.Request) {
logger.Response = Response{
Message: "internal server error",
Status: http.StatusInternalServerError,
Err: err,
Err: err.Error(),
}
logger.newLogEntry().errorLogger(logger.Response)
logger.newLogEntry().apiError(logger.Response, w, r)
@ -80,7 +79,6 @@ func ReceiveFile(w http.ResponseWriter, r *http.Request) {
ok = validateContentType(file)
if !ok {
logger.Response = Response{
Message: "file type must be text/plain",
Status: http.StatusUnsupportedMediaType,
@ -96,9 +94,11 @@ func ReceiveFile(w http.ResponseWriter, r *http.Request) {
logger.Response = Response{
Message: "Expected username to be a string",
Status: http.StatusInternalServerError,
Err: errors.New("unable to assert string type to claims interface{}"),
Err: "unable to assert string type to claims interface{}",
}
logger.newLogEntry().errorLogger(logger.Response)
return
}
zoneFile := newDNSRequest(header.Filename, user, b)
@ -107,7 +107,7 @@ func ReceiveFile(w http.ResponseWriter, r *http.Request) {
logger.Response = Response{
Message: "Unable to parse zonefile",
Status: http.StatusInternalServerError,
Err: err,
Err: err.Error(),
}
logger.newLogEntry().errorLogger(logger.Response)
logger.newLogEntry().apiError(logger.Response, w, r)
@ -117,11 +117,10 @@ func ReceiveFile(w http.ResponseWriter, r *http.Request) {
db, ok := r.Context().Value(keyPrincipalContextID).(*gorm.DB)
if !ok {
logger.Response = Response{
Message: "Unable to parse zonefile",
Status: http.StatusInternalServerError,
Err: errors.New("unable to connect to DB"),
Err: "db connection failed", // retarded linter
}
logger.newLogEntry().errorLogger(logger.Response)
logger.newLogEntry().apiError(logger.Response, w, r)
@ -136,7 +135,7 @@ func ReceiveFile(w http.ResponseWriter, r *http.Request) {
logger.Response = Response{
Message: "Internal server error",
Status: http.StatusInternalServerError,
Err: errors.New("user does not exist"),
Err: "user does not exist",
}
logger.newLogEntry().errorLogger(logger.Response)
logger.newLogEntry().apiError(logger.Response, w, r)
@ -153,11 +152,10 @@ func ReceiveFile(w http.ResponseWriter, r *http.Request) {
})
if err := zoneFile.save(); err != nil {
logger.Response = Response{
Message: "Unable to save zonefile",
Status: http.StatusInternalServerError,
Err: err,
Err: err.Error(),
}
logger.newLogEntry().errorLogger(logger.Response)
logger.newLogEntry().apiError(logger.Response, w, r)