mirror of
https://github.com/rwinkhart/go-boilerplate.git
synced 2026-08-28 04:46:41 -04:00
Bump dependencies & drop web package
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
module github.com/rwinkhart/go-boilerplate
|
module github.com/rwinkhart/go-boilerplate
|
||||||
|
|
||||||
go 1.26.1
|
go 1.26.3
|
||||||
|
|
||||||
require golang.org/x/term v0.41.0
|
require golang.org/x/term v0.43.0
|
||||||
|
|
||||||
require golang.org/x/sys v0.42.0 // indirect
|
require golang.org/x/sys v0.45.0 // indirect
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
golang.org/x/sys v0.42.0 h1:omrd2nAlyT5ESRdCLYdm3+fMfNFE/+Rf4bDIQImRJeo=
|
golang.org/x/sys v0.45.0 h1:dO4czNzziLiiXplLQgBCEpCvXQ3dnkn0SdaZSYdQ+FY=
|
||||||
golang.org/x/sys v0.42.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw=
|
golang.org/x/sys v0.45.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw=
|
||||||
golang.org/x/term v0.41.0 h1:QCgPso/Q3RTJx2Th4bDLqML4W6iJiaXFq2/ftQF13YU=
|
golang.org/x/term v0.43.0 h1:S4RLU2sB31O/NCl+zFN9Aru9A/Cq2aqKpTZJ6B+DwT4=
|
||||||
golang.org/x/term v0.41.0/go.mod h1:3pfBgksrReYfZ5lvYM0kSO0LIkAl4Yl2bXOkKP7Ec2A=
|
golang.org/x/term v0.43.0/go.mod h1:lrhlHNdQJHO+1qVYiHfFKVuVioJIheAc3fBSMFYEIsk=
|
||||||
|
|||||||
-57
@@ -1,57 +0,0 @@
|
|||||||
package web
|
|
||||||
|
|
||||||
import (
|
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
|
||||||
"net/http"
|
|
||||||
|
|
||||||
"github.com/rwinkhart/go-boilerplate/back"
|
|
||||||
)
|
|
||||||
|
|
||||||
// GenericResponse defines a generic status and message JSON response.
|
|
||||||
type GenericResponse struct {
|
|
||||||
Status string `json:"status"`
|
|
||||||
Message string `json:"message"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// WriteHTTPError writes an error status and message to an http ResponseWriter.
|
|
||||||
func WriteHTTPError(w *http.ResponseWriter, message string, responseCode int) {
|
|
||||||
jsonError := GenericResponse{
|
|
||||||
Status: "error",
|
|
||||||
Message: message,
|
|
||||||
}
|
|
||||||
|
|
||||||
// ERROR IGNORED
|
|
||||||
// Reason: Used in error handling path, thus this needs to be unable
|
|
||||||
// to fail (there would be no way to handle it gracefully).
|
|
||||||
output, _ := json.Marshal(jsonError)
|
|
||||||
|
|
||||||
(*w).Header().Set("Content-Type", "application/json")
|
|
||||||
(*w).WriteHeader(http.StatusBadRequest)
|
|
||||||
(*w).Write(output)
|
|
||||||
}
|
|
||||||
|
|
||||||
// WriteHTTPResponse writes a custom response to an http ResponseWriter.
|
|
||||||
func WriteHTTPResponse(w *http.ResponseWriter, unmarshaledResponse any) {
|
|
||||||
output, err := json.Marshal(unmarshaledResponse)
|
|
||||||
if err != nil {
|
|
||||||
WriteHTTPError(w, "Failed to marshal JSON: "+err.Error(), http.StatusInternalServerError)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
(*w).Header().Set("Content-Type", "application/json")
|
|
||||||
(*w).WriteHeader(http.StatusOK)
|
|
||||||
(*w).Write(output)
|
|
||||||
}
|
|
||||||
|
|
||||||
// LogAndVerifyRequest logs incoming HTTP requests
|
|
||||||
// and ensures the methods match what is expected.
|
|
||||||
// Returns: badMethod (bool) - if true, the method is not allowed and the handler should return.
|
|
||||||
func LogAndVerifyRequest(w *http.ResponseWriter, r *http.Request, expectedMethod string) bool {
|
|
||||||
if r.Method != expectedMethod {
|
|
||||||
WriteHTTPError(w, "Method not allowed", http.StatusMethodNotAllowed)
|
|
||||||
PrintLog(fmt.Sprintf("BAD method (expected: %s, got: %s) for request received on %s from %s", expectedMethod, r.Method, r.URL.Path, r.RemoteAddr), back.AnsiError)
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
PrintLog(fmt.Sprintf("Request received on %s from %s", r.URL.Path, r.RemoteAddr), "")
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
-21
@@ -1,21 +0,0 @@
|
|||||||
package web
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"log"
|
|
||||||
"os"
|
|
||||||
|
|
||||||
"github.com/rwinkhart/go-boilerplate/back"
|
|
||||||
)
|
|
||||||
|
|
||||||
// PrintLog colorizes a log and determines the best method for printing it.
|
|
||||||
func PrintLog(message, ansiColor string) {
|
|
||||||
if ansiColor != "" {
|
|
||||||
message = ansiColor + message + back.AnsiReset
|
|
||||||
}
|
|
||||||
if os.Getenv("INVOCATION_ID") != "" {
|
|
||||||
fmt.Println(message)
|
|
||||||
} else {
|
|
||||||
log.Println(message)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user