Moved v3 code from NginxProxyManager/nginx-proxy-manager-3 to NginxProxyManager/nginx-proxy-manager

This commit is contained in:
Jamie Curnow
2022-05-12 08:47:31 +10:00
parent 4db34f5894
commit 2110ecc382
830 changed files with 38168 additions and 36635 deletions

View File

@ -0,0 +1,46 @@
package http
import (
"context"
"encoding/json"
"errors"
"github.com/qri-io/jsonschema"
)
var (
// ErrInvalidJSON is an error for invalid json
ErrInvalidJSON = errors.New("JSON is invalid")
// ErrInvalidPayload is an error for invalid incoming data
ErrInvalidPayload = errors.New("Payload is invalid")
)
// ValidateRequestSchema takes a Schema and the Content to validate against it
func ValidateRequestSchema(schema string, requestBody []byte) ([]jsonschema.KeyError, error) {
var jsonErrors []jsonschema.KeyError
var schemaBytes = []byte(schema)
// Make sure the body is valid JSON
if !isJSON(requestBody) {
return jsonErrors, ErrInvalidJSON
}
rs := &jsonschema.Schema{}
if err := json.Unmarshal(schemaBytes, rs); err != nil {
return jsonErrors, err
}
var validationErr error
ctx := context.TODO()
if jsonErrors, validationErr = rs.ValidateBytes(ctx, requestBody); len(jsonErrors) > 0 {
return jsonErrors, validationErr
}
// Valid
return nil, nil
}
func isJSON(bytes []byte) bool {
var js map[string]interface{}
return json.Unmarshal(bytes, &js) == nil
}

View File

@ -0,0 +1,91 @@
package http
import (
"encoding/json"
"fmt"
"net/http"
"reflect"
c "npm/internal/api/context"
"npm/internal/errors"
"npm/internal/logger"
"github.com/qri-io/jsonschema"
)
// Response interface for standard API results
type Response struct {
Result interface{} `json:"result"`
Error interface{} `json:"error,omitempty"`
}
// ErrorResponse interface for errors returned via the API
type ErrorResponse struct {
Code interface{} `json:"code"`
Message interface{} `json:"message"`
Invalid interface{} `json:"invalid,omitempty"`
}
// ResultResponseJSON will write the result as json to the http output
func ResultResponseJSON(w http.ResponseWriter, r *http.Request, status int, result interface{}) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(status)
var response Response
resultClass := fmt.Sprintf("%v", reflect.TypeOf(result))
if resultClass == "http.ErrorResponse" {
response = Response{
Error: result,
}
} else {
response = Response{
Result: result,
}
}
var payload []byte
var err error
if getPrettyPrintFromContext(r) {
payload, err = json.MarshalIndent(response, "", " ")
} else {
payload, err = json.Marshal(response)
}
if err != nil {
logger.Error("ResponseMarshalError", err)
}
fmt.Fprint(w, string(payload))
}
// ResultSchemaErrorJSON will format the result as a standard error object and send it for output
func ResultSchemaErrorJSON(w http.ResponseWriter, r *http.Request, errs []jsonschema.KeyError) {
errorResponse := ErrorResponse{
Code: http.StatusBadRequest,
Message: errors.ErrValidationFailed,
Invalid: errs,
}
ResultResponseJSON(w, r, http.StatusBadRequest, errorResponse)
}
// ResultErrorJSON will format the result as a standard error object and send it for output
func ResultErrorJSON(w http.ResponseWriter, r *http.Request, status int, message string, extended interface{}) {
errorResponse := ErrorResponse{
Code: status,
Message: message,
Invalid: extended,
}
ResultResponseJSON(w, r, status, errorResponse)
}
// getPrettyPrintFromContext returns the PrettyPrint setting
func getPrettyPrintFromContext(r *http.Request) bool {
pretty, ok := r.Context().Value(c.PrettyPrintCtxKey).(bool)
if !ok {
return false
}
return pretty
}