mirror of
https://github.com/bitechdev/ResolveSpec.git
synced 2026-09-21 23:51:59 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
20c67166d0 | ||
|
|
749dad4ed1 | ||
|
|
d6c5740f9c | ||
|
|
817b781c88 | ||
|
|
87eaa9e18c | ||
|
|
4f6878099b | ||
|
|
0d8b136b91 |
+113
-102
@@ -306,15 +306,16 @@ func (h *Handler) handleRead(ctx context.Context, w common.ResponseWriter, id st
|
|||||||
|
|
||||||
txErr := h.db.RunInTransaction(ctx, func(tx common.Database) error {
|
txErr := h.db.RunInTransaction(ctx, func(tx common.Database) error {
|
||||||
hookCtx := &HookContext{
|
hookCtx := &HookContext{
|
||||||
Context: ctx,
|
Context: ctx,
|
||||||
Handler: h,
|
Handler: h,
|
||||||
Schema: schema,
|
Schema: schema,
|
||||||
Entity: entity,
|
Entity: entity,
|
||||||
Model: model,
|
Model: model,
|
||||||
Options: options,
|
Operation: "read",
|
||||||
ID: id,
|
Options: options,
|
||||||
Writer: w,
|
ID: id,
|
||||||
Tx: tx,
|
Writer: w,
|
||||||
|
Tx: tx,
|
||||||
}
|
}
|
||||||
if err := h.hooks.ExecuteBeforeOp(BeforeRead, hookCtx); err != nil {
|
if err := h.hooks.ExecuteBeforeOp(BeforeRead, hookCtx); err != nil {
|
||||||
statusCode, errCode, errMsg = http.StatusInternalServerError, "hook_error", "BeforeRead hook failed"
|
statusCode, errCode, errMsg = http.StatusInternalServerError, "hook_error", "BeforeRead hook failed"
|
||||||
@@ -722,15 +723,16 @@ func (h *Handler) handleCreate(ctx context.Context, w common.ResponseWriter, dat
|
|||||||
var nestedResult *common.ProcessResult
|
var nestedResult *common.ProcessResult
|
||||||
err := h.db.RunInTransaction(ctx, func(tx common.Database) error {
|
err := h.db.RunInTransaction(ctx, func(tx common.Database) error {
|
||||||
hookCtx := &HookContext{
|
hookCtx := &HookContext{
|
||||||
Context: ctx,
|
Context: ctx,
|
||||||
Handler: h,
|
Handler: h,
|
||||||
Schema: schema,
|
Schema: schema,
|
||||||
Entity: entity,
|
Entity: entity,
|
||||||
Model: model,
|
Model: model,
|
||||||
Options: options,
|
Operation: "create",
|
||||||
Data: v,
|
Options: options,
|
||||||
Writer: w,
|
Data: v,
|
||||||
Tx: tx,
|
Writer: w,
|
||||||
|
Tx: tx,
|
||||||
}
|
}
|
||||||
if err := h.hooks.ExecuteBeforeOp(BeforeCreate, hookCtx); err != nil {
|
if err := h.hooks.ExecuteBeforeOp(BeforeCreate, hookCtx); err != nil {
|
||||||
return fmt.Errorf("BeforeCreate hook failed: %w", err)
|
return fmt.Errorf("BeforeCreate hook failed: %w", err)
|
||||||
@@ -769,15 +771,16 @@ func (h *Handler) handleCreate(ctx context.Context, w common.ResponseWriter, dat
|
|||||||
var responseData interface{} = v
|
var responseData interface{} = v
|
||||||
err := h.db.RunInTransaction(ctx, func(tx common.Database) error {
|
err := h.db.RunInTransaction(ctx, func(tx common.Database) error {
|
||||||
hookCtx := &HookContext{
|
hookCtx := &HookContext{
|
||||||
Context: ctx,
|
Context: ctx,
|
||||||
Handler: h,
|
Handler: h,
|
||||||
Schema: schema,
|
Schema: schema,
|
||||||
Entity: entity,
|
Entity: entity,
|
||||||
Model: model,
|
Model: model,
|
||||||
Options: options,
|
Operation: "create",
|
||||||
Data: v,
|
Options: options,
|
||||||
Writer: w,
|
Data: v,
|
||||||
Tx: tx,
|
Writer: w,
|
||||||
|
Tx: tx,
|
||||||
}
|
}
|
||||||
if err := h.hooks.ExecuteBeforeOp(BeforeCreate, hookCtx); err != nil {
|
if err := h.hooks.ExecuteBeforeOp(BeforeCreate, hookCtx); err != nil {
|
||||||
return fmt.Errorf("BeforeCreate hook failed: %w", err)
|
return fmt.Errorf("BeforeCreate hook failed: %w", err)
|
||||||
@@ -851,15 +854,16 @@ func (h *Handler) handleCreate(ctx context.Context, w common.ResponseWriter, dat
|
|||||||
|
|
||||||
for _, item := range v {
|
for _, item := range v {
|
||||||
hookCtx := &HookContext{
|
hookCtx := &HookContext{
|
||||||
Context: ctx,
|
Context: ctx,
|
||||||
Handler: h,
|
Handler: h,
|
||||||
Schema: schema,
|
Schema: schema,
|
||||||
Entity: entity,
|
Entity: entity,
|
||||||
Model: model,
|
Model: model,
|
||||||
Options: options,
|
Operation: "create",
|
||||||
Data: item,
|
Options: options,
|
||||||
Writer: w,
|
Data: item,
|
||||||
Tx: tx,
|
Writer: w,
|
||||||
|
Tx: tx,
|
||||||
}
|
}
|
||||||
if err := h.hooks.ExecuteBeforeOp(BeforeCreate, hookCtx); err != nil {
|
if err := h.hooks.ExecuteBeforeOp(BeforeCreate, hookCtx); err != nil {
|
||||||
return fmt.Errorf("BeforeCreate hook failed: %w", err)
|
return fmt.Errorf("BeforeCreate hook failed: %w", err)
|
||||||
@@ -898,15 +902,16 @@ func (h *Handler) handleCreate(ctx context.Context, w common.ResponseWriter, dat
|
|||||||
err := h.db.RunInTransaction(ctx, func(tx common.Database) error {
|
err := h.db.RunInTransaction(ctx, func(tx common.Database) error {
|
||||||
for _, item := range v {
|
for _, item := range v {
|
||||||
hookCtx := &HookContext{
|
hookCtx := &HookContext{
|
||||||
Context: ctx,
|
Context: ctx,
|
||||||
Handler: h,
|
Handler: h,
|
||||||
Schema: schema,
|
Schema: schema,
|
||||||
Entity: entity,
|
Entity: entity,
|
||||||
Model: model,
|
Model: model,
|
||||||
Options: options,
|
Operation: "create",
|
||||||
Data: item,
|
Options: options,
|
||||||
Writer: w,
|
Data: item,
|
||||||
Tx: tx,
|
Writer: w,
|
||||||
|
Tx: tx,
|
||||||
}
|
}
|
||||||
if err := h.hooks.ExecuteBeforeOp(BeforeCreate, hookCtx); err != nil {
|
if err := h.hooks.ExecuteBeforeOp(BeforeCreate, hookCtx); err != nil {
|
||||||
return fmt.Errorf("BeforeCreate hook failed: %w", err)
|
return fmt.Errorf("BeforeCreate hook failed: %w", err)
|
||||||
@@ -982,15 +987,16 @@ func (h *Handler) handleCreate(ctx context.Context, w common.ResponseWriter, dat
|
|||||||
for _, item := range v {
|
for _, item := range v {
|
||||||
if itemMap, ok := item.(map[string]interface{}); ok {
|
if itemMap, ok := item.(map[string]interface{}); ok {
|
||||||
hookCtx := &HookContext{
|
hookCtx := &HookContext{
|
||||||
Context: ctx,
|
Context: ctx,
|
||||||
Handler: h,
|
Handler: h,
|
||||||
Schema: schema,
|
Schema: schema,
|
||||||
Entity: entity,
|
Entity: entity,
|
||||||
Model: model,
|
Model: model,
|
||||||
Options: options,
|
Operation: "create",
|
||||||
Data: itemMap,
|
Options: options,
|
||||||
Writer: w,
|
Data: itemMap,
|
||||||
Tx: tx,
|
Writer: w,
|
||||||
|
Tx: tx,
|
||||||
}
|
}
|
||||||
if err := h.hooks.ExecuteBeforeOp(BeforeCreate, hookCtx); err != nil {
|
if err := h.hooks.ExecuteBeforeOp(BeforeCreate, hookCtx); err != nil {
|
||||||
return fmt.Errorf("BeforeCreate hook failed: %w", err)
|
return fmt.Errorf("BeforeCreate hook failed: %w", err)
|
||||||
@@ -1035,15 +1041,16 @@ func (h *Handler) handleCreate(ctx context.Context, w common.ResponseWriter, dat
|
|||||||
}
|
}
|
||||||
|
|
||||||
hookCtx := &HookContext{
|
hookCtx := &HookContext{
|
||||||
Context: ctx,
|
Context: ctx,
|
||||||
Handler: h,
|
Handler: h,
|
||||||
Schema: schema,
|
Schema: schema,
|
||||||
Entity: entity,
|
Entity: entity,
|
||||||
Model: model,
|
Model: model,
|
||||||
Options: options,
|
Operation: "create",
|
||||||
Data: itemMap,
|
Options: options,
|
||||||
Writer: w,
|
Data: itemMap,
|
||||||
Tx: tx,
|
Writer: w,
|
||||||
|
Tx: tx,
|
||||||
}
|
}
|
||||||
if err := h.hooks.ExecuteBeforeOp(BeforeCreate, hookCtx); err != nil {
|
if err := h.hooks.ExecuteBeforeOp(BeforeCreate, hookCtx); err != nil {
|
||||||
return fmt.Errorf("BeforeCreate hook failed: %w", err)
|
return fmt.Errorf("BeforeCreate hook failed: %w", err)
|
||||||
@@ -1166,16 +1173,17 @@ func (h *Handler) handleUpdate(ctx context.Context, w common.ResponseWriter, url
|
|||||||
// they must run before the existence-check select so that select is
|
// they must run before the existence-check select so that select is
|
||||||
// also subject to RLS on this connection/transaction.
|
// also subject to RLS on this connection/transaction.
|
||||||
hookCtx := &HookContext{
|
hookCtx := &HookContext{
|
||||||
Context: ctx,
|
Context: ctx,
|
||||||
Handler: h,
|
Handler: h,
|
||||||
Schema: schema,
|
Schema: schema,
|
||||||
Entity: entity,
|
Entity: entity,
|
||||||
Model: model,
|
Model: model,
|
||||||
Options: options,
|
Operation: "update",
|
||||||
ID: urlID,
|
Options: options,
|
||||||
Data: updates,
|
ID: urlID,
|
||||||
Writer: w,
|
Data: updates,
|
||||||
Tx: tx,
|
Writer: w,
|
||||||
|
Tx: tx,
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := h.hooks.ExecuteBeforeOp(BeforeUpdate, hookCtx); err != nil {
|
if err := h.hooks.ExecuteBeforeOp(BeforeUpdate, hookCtx); err != nil {
|
||||||
@@ -1387,16 +1395,17 @@ func (h *Handler) handleUpdate(ctx context.Context, w common.ResponseWriter, url
|
|||||||
|
|
||||||
// Execute BeforeUpdate hooks inside transaction
|
// Execute BeforeUpdate hooks inside transaction
|
||||||
hookCtx := &HookContext{
|
hookCtx := &HookContext{
|
||||||
Context: ctx,
|
Context: ctx,
|
||||||
Handler: h,
|
Handler: h,
|
||||||
Schema: schema,
|
Schema: schema,
|
||||||
Entity: entity,
|
Entity: entity,
|
||||||
Model: model,
|
Model: model,
|
||||||
Options: options,
|
Operation: "update",
|
||||||
ID: itemIDStr,
|
Options: options,
|
||||||
Data: item,
|
ID: itemIDStr,
|
||||||
Writer: w,
|
Data: item,
|
||||||
Tx: tx,
|
Writer: w,
|
||||||
|
Tx: tx,
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := h.hooks.ExecuteBeforeOp(BeforeUpdate, hookCtx); err != nil {
|
if err := h.hooks.ExecuteBeforeOp(BeforeUpdate, hookCtx); err != nil {
|
||||||
@@ -1543,16 +1552,17 @@ func (h *Handler) handleUpdate(ctx context.Context, w common.ResponseWriter, url
|
|||||||
|
|
||||||
// Execute BeforeUpdate hooks inside transaction
|
// Execute BeforeUpdate hooks inside transaction
|
||||||
hookCtx := &HookContext{
|
hookCtx := &HookContext{
|
||||||
Context: ctx,
|
Context: ctx,
|
||||||
Handler: h,
|
Handler: h,
|
||||||
Schema: schema,
|
Schema: schema,
|
||||||
Entity: entity,
|
Entity: entity,
|
||||||
Model: model,
|
Model: model,
|
||||||
Options: options,
|
Operation: "update",
|
||||||
ID: itemIDStr,
|
Options: options,
|
||||||
Data: itemMap,
|
ID: itemIDStr,
|
||||||
Writer: w,
|
Data: itemMap,
|
||||||
Tx: tx,
|
Writer: w,
|
||||||
|
Tx: tx,
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := h.hooks.ExecuteBeforeOp(BeforeUpdate, hookCtx); err != nil {
|
if err := h.hooks.ExecuteBeforeOp(BeforeUpdate, hookCtx); err != nil {
|
||||||
@@ -1648,15 +1658,16 @@ func (h *Handler) handleDelete(ctx context.Context, w common.ResponseWriter, id
|
|||||||
|
|
||||||
// Execute BeforeDelete hooks (covers model-rule checks before any deletion)
|
// Execute BeforeDelete hooks (covers model-rule checks before any deletion)
|
||||||
hookCtx := &HookContext{
|
hookCtx := &HookContext{
|
||||||
Context: ctx,
|
Context: ctx,
|
||||||
Handler: h,
|
Handler: h,
|
||||||
Schema: schema,
|
Schema: schema,
|
||||||
Entity: entity,
|
Entity: entity,
|
||||||
Model: model,
|
Model: model,
|
||||||
ID: id,
|
Operation: "delete",
|
||||||
Data: data,
|
ID: id,
|
||||||
Writer: w,
|
Data: data,
|
||||||
Tx: h.db,
|
Writer: w,
|
||||||
|
Tx: h.db,
|
||||||
}
|
}
|
||||||
if err := h.hooks.ExecuteBeforeOp(BeforeDelete, hookCtx); err != nil {
|
if err := h.hooks.ExecuteBeforeOp(BeforeDelete, hookCtx); err != nil {
|
||||||
logger.Error("BeforeDelete hook failed: %v", err)
|
logger.Error("BeforeDelete hook failed: %v", err)
|
||||||
|
|||||||
@@ -25,12 +25,18 @@ func RegisterSecurityHooks(handler *Handler, securityList *security.SecurityList
|
|||||||
// Hook 1: BeforeRead - Load security rules
|
// Hook 1: BeforeRead - Load security rules
|
||||||
handler.Hooks().Register(BeforeRead, func(hookCtx *HookContext) error {
|
handler.Hooks().Register(BeforeRead, func(hookCtx *HookContext) error {
|
||||||
secCtx := newSecurityContext(hookCtx)
|
secCtx := newSecurityContext(hookCtx)
|
||||||
|
if security.IsModelSecurityDisabled(secCtx) {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
return security.LoadSecurityRules(secCtx, securityList)
|
return security.LoadSecurityRules(secCtx, securityList)
|
||||||
})
|
})
|
||||||
|
|
||||||
// Hook 2: BeforeScan - Apply row-level security filters
|
// Hook 2: BeforeScan - Apply row-level security filters
|
||||||
handler.Hooks().Register(BeforeScan, func(hookCtx *HookContext) error {
|
handler.Hooks().Register(BeforeScan, func(hookCtx *HookContext) error {
|
||||||
secCtx := newSecurityContext(hookCtx)
|
secCtx := newSecurityContext(hookCtx)
|
||||||
|
if security.ShouldSkipRowSecurity(secCtx, hookCtx.Operation) {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
return security.ApplyRowSecurity(secCtx, securityList)
|
return security.ApplyRowSecurity(secCtx, securityList)
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -97,6 +103,10 @@ func (s *securityContext) GetEntity() string {
|
|||||||
return s.ctx.Entity
|
return s.ctx.Entity
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *securityContext) GetOperation() string {
|
||||||
|
return s.ctx.Operation
|
||||||
|
}
|
||||||
|
|
||||||
func (s *securityContext) GetModel() interface{} {
|
func (s *securityContext) GetModel() interface{} {
|
||||||
return s.ctx.Model
|
return s.ctx.Model
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -233,8 +233,18 @@ func (h *Handler) Handle(w common.ResponseWriter, r common.Request, params map[s
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
validId, _ := strconv.ParseInt(id, 10, 64)
|
validId, _ := strconv.ParseInt(id, 10, 64)
|
||||||
if validId > 0 {
|
updateID := id
|
||||||
h.handleUpdate(ctx, w, id, nil, data, options)
|
isUpdate := validId > 0
|
||||||
|
if !isUpdate {
|
||||||
|
// No valid /:id in the URL - check whether the body itself carries
|
||||||
|
// a valid primary key value and treat this as an update if so.
|
||||||
|
if pkID, ok := h.extractPrimaryKeyFromBody(model, data); ok && pkID != "0" {
|
||||||
|
updateID = pkID
|
||||||
|
isUpdate = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if isUpdate {
|
||||||
|
h.handleUpdate(ctx, w, updateID, nil, data, options)
|
||||||
} else {
|
} else {
|
||||||
h.handleCreate(ctx, w, data, options)
|
h.handleCreate(ctx, w, data, options)
|
||||||
}
|
}
|
||||||
@@ -271,6 +281,49 @@ func (h *Handler) Handle(w common.ResponseWriter, r common.Request, params map[s
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// extractPrimaryKeyFromBody looks for a valid primary key value inside a
|
||||||
|
// decoded (single-record) POST body, keyed by the model's primary key column
|
||||||
|
// or its JSON equivalent. It returns the string form of that value and true
|
||||||
|
// if one was found and is non-empty/non-zero; otherwise ("", false).
|
||||||
|
func (h *Handler) extractPrimaryKeyFromBody(model interface{}, data interface{}) (string, bool) {
|
||||||
|
dataMap, ok := data.(map[string]interface{})
|
||||||
|
if !ok {
|
||||||
|
// Batch payloads (slices) aren't eligible for this implicit-update detection.
|
||||||
|
return "", false
|
||||||
|
}
|
||||||
|
|
||||||
|
pkCol := reflection.GetPrimaryKeyName(model)
|
||||||
|
if pkCol == "" {
|
||||||
|
return "", false
|
||||||
|
}
|
||||||
|
|
||||||
|
val, exists := dataMap[pkCol]
|
||||||
|
if !exists {
|
||||||
|
modelType := reflection.GetPointerElement(reflect.TypeOf(model))
|
||||||
|
for jsonKey, col := range reflection.BuildJSONToDBColumnMap(modelType) {
|
||||||
|
if col == pkCol {
|
||||||
|
val, exists = dataMap[jsonKey]
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !exists || val == nil || reflection.IsEmptyValue(val) {
|
||||||
|
return "", false
|
||||||
|
}
|
||||||
|
|
||||||
|
switch v := val.(type) {
|
||||||
|
case float64:
|
||||||
|
if v <= 0 {
|
||||||
|
return "", false
|
||||||
|
}
|
||||||
|
return strconv.FormatInt(int64(v), 10), true
|
||||||
|
case string:
|
||||||
|
return v, true
|
||||||
|
default:
|
||||||
|
return fmt.Sprintf("%v", v), true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// HandleGet processes GET requests for metadata
|
// HandleGet processes GET requests for metadata
|
||||||
func (h *Handler) HandleGet(w common.ResponseWriter, r common.Request, params map[string]string) {
|
func (h *Handler) HandleGet(w common.ResponseWriter, r common.Request, params map[string]string) {
|
||||||
// Capture panics and return error response
|
// Capture panics and return error response
|
||||||
@@ -379,6 +432,7 @@ func (h *Handler) handleRead(ctx context.Context, w common.ResponseWriter, id st
|
|||||||
Entity: entity,
|
Entity: entity,
|
||||||
TableName: tableName,
|
TableName: tableName,
|
||||||
Model: model,
|
Model: model,
|
||||||
|
Operation: "read",
|
||||||
Options: options,
|
Options: options,
|
||||||
ID: id,
|
ID: id,
|
||||||
Writer: w,
|
Writer: w,
|
||||||
@@ -1236,6 +1290,7 @@ func (h *Handler) handleCreate(ctx context.Context, w common.ResponseWriter, dat
|
|||||||
Entity: entity,
|
Entity: entity,
|
||||||
TableName: tableName,
|
TableName: tableName,
|
||||||
Model: model,
|
Model: model,
|
||||||
|
Operation: "create",
|
||||||
Options: options,
|
Options: options,
|
||||||
Data: data,
|
Data: data,
|
||||||
Writer: w,
|
Writer: w,
|
||||||
@@ -1335,6 +1390,7 @@ func (h *Handler) handleCreate(ctx context.Context, w common.ResponseWriter, dat
|
|||||||
Entity: entity,
|
Entity: entity,
|
||||||
TableName: tableName,
|
TableName: tableName,
|
||||||
Model: model,
|
Model: model,
|
||||||
|
Operation: "create",
|
||||||
Options: options,
|
Options: options,
|
||||||
Data: modelValue,
|
Data: modelValue,
|
||||||
Writer: w,
|
Writer: w,
|
||||||
@@ -1489,6 +1545,7 @@ func (h *Handler) handleUpdate(ctx context.Context, w common.ResponseWriter, id
|
|||||||
TableName: tableName,
|
TableName: tableName,
|
||||||
Tx: tx,
|
Tx: tx,
|
||||||
Model: model,
|
Model: model,
|
||||||
|
Operation: "update",
|
||||||
Options: options,
|
Options: options,
|
||||||
ID: id,
|
ID: id,
|
||||||
Data: dataMap,
|
Data: dataMap,
|
||||||
@@ -1686,6 +1743,7 @@ func (h *Handler) handleDelete(ctx context.Context, w common.ResponseWriter, id
|
|||||||
Entity: entity,
|
Entity: entity,
|
||||||
TableName: tableName,
|
TableName: tableName,
|
||||||
Model: model,
|
Model: model,
|
||||||
|
Operation: "delete",
|
||||||
ID: itemID,
|
ID: itemID,
|
||||||
Writer: w,
|
Writer: w,
|
||||||
Tx: tx,
|
Tx: tx,
|
||||||
@@ -1760,6 +1818,7 @@ func (h *Handler) handleDelete(ctx context.Context, w common.ResponseWriter, id
|
|||||||
Entity: entity,
|
Entity: entity,
|
||||||
TableName: tableName,
|
TableName: tableName,
|
||||||
Model: model,
|
Model: model,
|
||||||
|
Operation: "delete",
|
||||||
ID: itemIDStr,
|
ID: itemIDStr,
|
||||||
Writer: w,
|
Writer: w,
|
||||||
Tx: tx,
|
Tx: tx,
|
||||||
@@ -1818,6 +1877,7 @@ func (h *Handler) handleDelete(ctx context.Context, w common.ResponseWriter, id
|
|||||||
Entity: entity,
|
Entity: entity,
|
||||||
TableName: tableName,
|
TableName: tableName,
|
||||||
Model: model,
|
Model: model,
|
||||||
|
Operation: "delete",
|
||||||
ID: itemIDStr,
|
ID: itemIDStr,
|
||||||
Writer: w,
|
Writer: w,
|
||||||
Tx: tx,
|
Tx: tx,
|
||||||
@@ -1902,6 +1962,7 @@ func (h *Handler) handleDelete(ctx context.Context, w common.ResponseWriter, id
|
|||||||
Entity: entity,
|
Entity: entity,
|
||||||
TableName: tableName,
|
TableName: tableName,
|
||||||
Model: model,
|
Model: model,
|
||||||
|
Operation: "delete",
|
||||||
ID: id,
|
ID: id,
|
||||||
Writer: w,
|
Writer: w,
|
||||||
Tx: h.db,
|
Tx: h.db,
|
||||||
|
|||||||
+59
-17
@@ -232,9 +232,37 @@ func LoadSecurityRules(secCtx SecurityContext, securityList *SecurityList) error
|
|||||||
// ApplyRowSecurity is a public wrapper for applyRowSecurity that accepts a SecurityContext
|
// ApplyRowSecurity is a public wrapper for applyRowSecurity that accepts a SecurityContext
|
||||||
// This allows other packages to apply row-level security using the generic interface
|
// This allows other packages to apply row-level security using the generic interface
|
||||||
func ApplyRowSecurity(secCtx SecurityContext, securityList *SecurityList) error {
|
func ApplyRowSecurity(secCtx SecurityContext, securityList *SecurityList) error {
|
||||||
|
// Spec adapters that expose the dispatched operation can enforce the same
|
||||||
|
// model-rule bypass even when ApplyRowSecurity is called directly.
|
||||||
|
if operationCtx, ok := secCtx.(interface{ GetOperation() string }); ok &&
|
||||||
|
ShouldSkipRowSecurity(secCtx, operationCtx.GetOperation()) {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
return applyRowSecurity(secCtx, securityList)
|
return applyRowSecurity(secCtx, securityList)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ShouldSkipRowSecurity reports whether row-security enforcement should be
|
||||||
|
// skipped for the operation. It uses the same model-rule resolution as
|
||||||
|
// CheckModelAuthAllowed so the model registry remains the single source of
|
||||||
|
// truth for security behavior.
|
||||||
|
func ShouldSkipRowSecurity(secCtx SecurityContext, operation string) bool {
|
||||||
|
rules, ok := resolveModelRules(secCtx)
|
||||||
|
if !ok {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
return rules.SecurityDisabled || (operation == "read" && rules.CanPublicRead)
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsModelSecurityDisabled reports whether all model-level security processing
|
||||||
|
// is disabled for the model. This is distinct from ShouldSkipRowSecurity:
|
||||||
|
// CanPublicRead skips row filtering for reads but must still allow other read
|
||||||
|
// security, such as column masking, to be loaded.
|
||||||
|
func IsModelSecurityDisabled(secCtx SecurityContext) bool {
|
||||||
|
rules, ok := resolveModelRules(secCtx)
|
||||||
|
return ok && rules.SecurityDisabled
|
||||||
|
}
|
||||||
|
|
||||||
// ApplyColumnSecurity is a public wrapper for applyColumnSecurity that accepts a SecurityContext
|
// ApplyColumnSecurity is a public wrapper for applyColumnSecurity that accepts a SecurityContext
|
||||||
// This allows other packages to apply column-level security using the generic interface
|
// This allows other packages to apply column-level security using the generic interface
|
||||||
func ApplyColumnSecurity(secCtx SecurityContext, securityList *SecurityList) error {
|
func ApplyColumnSecurity(secCtx SecurityContext, securityList *SecurityList) error {
|
||||||
@@ -303,25 +331,14 @@ func checkModelDeleteAllowed(secCtx SecurityContext) error {
|
|||||||
// 7. Guest (UserID == 0) → return "authentication required".
|
// 7. Guest (UserID == 0) → return "authentication required".
|
||||||
// 8. Authenticated user → allow (operation-specific checks remain in BeforeUpdate/BeforeDelete).
|
// 8. Authenticated user → allow (operation-specific checks remain in BeforeUpdate/BeforeDelete).
|
||||||
func CheckModelAuthAllowed(secCtx SecurityContext, operation string) error {
|
func CheckModelAuthAllowed(secCtx SecurityContext, operation string) error {
|
||||||
rules, ok := GetModelRulesFromContext(secCtx.GetContext())
|
rules, ok := resolveModelRules(secCtx)
|
||||||
if !ok {
|
if !ok {
|
||||||
schema := secCtx.GetSchema()
|
// Model not registered - fall through to auth check
|
||||||
entity := secCtx.GetEntity()
|
userID, _ := secCtx.GetUserID()
|
||||||
var err error
|
if userID == 0 {
|
||||||
if schema != "" {
|
return fmt.Errorf("authentication required")
|
||||||
rules, err = modelregistry.GetModelRulesByName(fmt.Sprintf("%s.%s", schema, entity))
|
|
||||||
}
|
|
||||||
if err != nil || schema == "" {
|
|
||||||
rules, err = modelregistry.GetModelRulesByName(entity)
|
|
||||||
}
|
|
||||||
if err != nil {
|
|
||||||
// Model not registered - fall through to auth check
|
|
||||||
userID, _ := secCtx.GetUserID()
|
|
||||||
if userID == 0 {
|
|
||||||
return fmt.Errorf("authentication required")
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if rules.SecurityDisabled {
|
if rules.SecurityDisabled {
|
||||||
@@ -347,6 +364,31 @@ func CheckModelAuthAllowed(secCtx SecurityContext, operation string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// resolveModelRules returns model rules from the request context first, then
|
||||||
|
// falls back to the schema-qualified and unqualified registry names.
|
||||||
|
func resolveModelRules(secCtx SecurityContext) (modelregistry.ModelRules, bool) {
|
||||||
|
if rules, ok := GetModelRulesFromContext(secCtx.GetContext()); ok {
|
||||||
|
return rules, true
|
||||||
|
}
|
||||||
|
|
||||||
|
schema := secCtx.GetSchema()
|
||||||
|
entity := secCtx.GetEntity()
|
||||||
|
var err error
|
||||||
|
if schema != "" {
|
||||||
|
var rules modelregistry.ModelRules
|
||||||
|
rules, err = modelregistry.GetModelRulesByName(fmt.Sprintf("%s.%s", schema, entity))
|
||||||
|
if err == nil {
|
||||||
|
return rules, true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
rules, err := modelregistry.GetModelRulesByName(entity)
|
||||||
|
if err != nil {
|
||||||
|
return modelregistry.ModelRules{}, false
|
||||||
|
}
|
||||||
|
return rules, true
|
||||||
|
}
|
||||||
|
|
||||||
// CheckModelUpdateAllowed is the public wrapper for checkModelUpdateAllowed.
|
// CheckModelUpdateAllowed is the public wrapper for checkModelUpdateAllowed.
|
||||||
func CheckModelUpdateAllowed(secCtx SecurityContext) error {
|
func CheckModelUpdateAllowed(secCtx SecurityContext) error {
|
||||||
return checkModelUpdateAllowed(secCtx)
|
return checkModelUpdateAllowed(secCtx)
|
||||||
|
|||||||
@@ -5,8 +5,10 @@
|
|||||||
package quickproxy
|
package quickproxy
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httputil"
|
"net/http/httputil"
|
||||||
@@ -26,6 +28,17 @@ type Rule struct {
|
|||||||
// The incoming request path and query are forwarded unchanged; only the
|
// The incoming request path and query are forwarded unchanged; only the
|
||||||
// scheme and host are rewritten to Target's.
|
// scheme and host are rewritten to Target's.
|
||||||
Target string
|
Target string
|
||||||
|
|
||||||
|
// Exclude is a list of URL path prefixes that this rule should not
|
||||||
|
// proxy, even though they fall under URLPrefix. Each entry is a full
|
||||||
|
// path from root and must itself start with URLPrefix (e.g. rule
|
||||||
|
// URLPrefix "/api" excluding a subpath must use "/api/health", not
|
||||||
|
// "/health"). A request matching an Exclude prefix is treated as if
|
||||||
|
// this rule didn't match at all: matching continues against any other
|
||||||
|
// configured rule, falling back if none match. This is typically used
|
||||||
|
// to carve out paths (e.g. "/health") from a catch-all "/" rule so
|
||||||
|
// they're served by the fallback handler instead of being proxied.
|
||||||
|
Exclude []string
|
||||||
}
|
}
|
||||||
|
|
||||||
// DefaultTimeout is the dial and response-header timeout applied to
|
// DefaultTimeout is the dial and response-header timeout applied to
|
||||||
@@ -50,8 +63,19 @@ func WithTimeout(d time.Duration) Option {
|
|||||||
|
|
||||||
// compiledRule pairs a Rule with its ready-to-use reverse proxy.
|
// compiledRule pairs a Rule with its ready-to-use reverse proxy.
|
||||||
type compiledRule struct {
|
type compiledRule struct {
|
||||||
prefix string
|
prefix string
|
||||||
proxy *httputil.ReverseProxy
|
excludes []string
|
||||||
|
proxy *httputil.ReverseProxy
|
||||||
|
}
|
||||||
|
|
||||||
|
// excluded reports whether path falls under one of the rule's Exclude prefixes.
|
||||||
|
func (r *compiledRule) excluded(path string) bool {
|
||||||
|
for _, ex := range r.excludes {
|
||||||
|
if strings.HasPrefix(path, ex) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
// Service holds a compiled set of proxy rules and performs longest-prefix
|
// Service holds a compiled set of proxy rules and performs longest-prefix
|
||||||
@@ -98,9 +122,19 @@ func NewService(rules []Rule, opts ...Option) (*Service, error) {
|
|||||||
return nil, fmt.Errorf("quickproxy: invalid target %q for prefix %q", r.Target, r.URLPrefix)
|
return nil, fmt.Errorf("quickproxy: invalid target %q for prefix %q", r.Target, r.URLPrefix)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for _, ex := range r.Exclude {
|
||||||
|
if !strings.HasPrefix(ex, "/") {
|
||||||
|
return nil, fmt.Errorf("quickproxy: exclude prefix %q for rule %q must start with /", ex, r.URLPrefix)
|
||||||
|
}
|
||||||
|
if !strings.HasPrefix(ex, r.URLPrefix) {
|
||||||
|
return nil, fmt.Errorf("quickproxy: exclude prefix %q for rule %q must itself start with the rule's URLPrefix", ex, r.URLPrefix)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
compiled = append(compiled, compiledRule{
|
compiled = append(compiled, compiledRule{
|
||||||
prefix: r.URLPrefix,
|
prefix: r.URLPrefix,
|
||||||
proxy: newReverseProxy(target, cfg.timeout),
|
excludes: r.Exclude,
|
||||||
|
proxy: newReverseProxy(target, cfg.timeout),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -159,6 +193,15 @@ func (s *Service) Handler(fallback http.Handler) http.Handler {
|
|||||||
|
|
||||||
for i := range s.rules {
|
for i := range s.rules {
|
||||||
s.rules[i].proxy.ErrorHandler = func(w http.ResponseWriter, r *http.Request, _ error) {
|
s.rules[i].proxy.ErrorHandler = func(w http.ResponseWriter, r *http.Request, _ error) {
|
||||||
|
// ReverseProxy consumes and closes r.Body while attempting the
|
||||||
|
// upstream request, even when that attempt fails (per the
|
||||||
|
// http.RoundTripper contract). Restore a fresh copy from
|
||||||
|
// r.GetBody, set below, before handing the request to fallback.
|
||||||
|
if r.GetBody != nil {
|
||||||
|
if body, err := r.GetBody(); err == nil {
|
||||||
|
r.Body = body
|
||||||
|
}
|
||||||
|
}
|
||||||
fallback.ServeHTTP(w, r)
|
fallback.ServeHTTP(w, r)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -169,16 +212,38 @@ func (s *Service) Handler(fallback http.Handler) http.Handler {
|
|||||||
fallback.ServeHTTP(w, r)
|
fallback.ServeHTTP(w, r)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Buffer the body so it can be replayed to fallback if the upstream
|
||||||
|
// attempt fails; see ErrorHandler above.
|
||||||
|
if r.Body != nil && r.Body != http.NoBody {
|
||||||
|
bodyBytes, err := io.ReadAll(r.Body)
|
||||||
|
r.Body.Close()
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, "failed to read request body", http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
r.Body = io.NopCloser(bytes.NewReader(bodyBytes))
|
||||||
|
r.GetBody = func() (io.ReadCloser, error) {
|
||||||
|
return io.NopCloser(bytes.NewReader(bodyBytes)), nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
rule.proxy.ServeHTTP(w, r)
|
rule.proxy.ServeHTTP(w, r)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// match returns the longest-prefix rule matching path, or nil if none match.
|
// match returns the longest-prefix rule matching path, or nil if none match.
|
||||||
|
// A rule whose Exclude covers path is skipped, and matching continues
|
||||||
|
// against the next-longest-prefix rule.
|
||||||
func (s *Service) match(path string) *compiledRule {
|
func (s *Service) match(path string) *compiledRule {
|
||||||
for i := range s.rules {
|
for i := range s.rules {
|
||||||
if strings.HasPrefix(path, s.rules[i].prefix) {
|
if !strings.HasPrefix(path, s.rules[i].prefix) {
|
||||||
return &s.rules[i]
|
continue
|
||||||
}
|
}
|
||||||
|
if s.rules[i].excluded(path) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
return &s.rules[i]
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
@@ -23,7 +24,19 @@ func TestNewService_Validation(t *testing.T) {
|
|||||||
{URLPrefix: "/api", Target: "http://localhost:1"},
|
{URLPrefix: "/api", Target: "http://localhost:1"},
|
||||||
{URLPrefix: "/api", Target: "http://localhost:2"},
|
{URLPrefix: "/api", Target: "http://localhost:2"},
|
||||||
}, true},
|
}, true},
|
||||||
|
{"bad exclude prefix", []Rule{
|
||||||
|
{URLPrefix: "/", Target: "http://localhost:1", Exclude: []string{"health"}},
|
||||||
|
}, true},
|
||||||
|
{"exclude outside rule's URLPrefix", []Rule{
|
||||||
|
{URLPrefix: "/api", Target: "http://localhost:1", Exclude: []string{"/health"}},
|
||||||
|
}, true},
|
||||||
{"valid", []Rule{{URLPrefix: "/api", Target: "http://localhost:1"}}, false},
|
{"valid", []Rule{{URLPrefix: "/api", Target: "http://localhost:1"}}, false},
|
||||||
|
{"valid with exclude", []Rule{
|
||||||
|
{URLPrefix: "/", Target: "http://localhost:1", Exclude: []string{"/health"}},
|
||||||
|
}, false},
|
||||||
|
{"valid with nested exclude", []Rule{
|
||||||
|
{URLPrefix: "/api", Target: "http://localhost:1", Exclude: []string{"/api/health"}},
|
||||||
|
}, false},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
@@ -118,6 +131,75 @@ func TestHandler_UnreachableUpstreamFallsBack(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestHandler_UnreachableUpstreamFallsBackWithBody(t *testing.T) {
|
||||||
|
// A closed listener address: nothing is listening, so dialing fails and
|
||||||
|
// ReverseProxy invokes ErrorHandler. The fallback handler must still see
|
||||||
|
// the original request body, even though ReverseProxy consumed and
|
||||||
|
// closed it while attempting (and failing) the upstream request.
|
||||||
|
unreachable := "http://127.0.0.1:1"
|
||||||
|
|
||||||
|
svc, err := NewService([]Rule{{URLPrefix: "/", Target: unreachable}}, WithTimeout(500*time.Millisecond))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("NewService: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
echoBody := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
body, err := io.ReadAll(r.Body)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("fallback reading body: %v", err)
|
||||||
|
}
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
|
_, _ = w.Write(body)
|
||||||
|
})
|
||||||
|
|
||||||
|
handler := svc.Handler(echoBody)
|
||||||
|
|
||||||
|
req := httptest.NewRequest(http.MethodPost, "/submit", strings.NewReader("payload=1"))
|
||||||
|
rr := httptest.NewRecorder()
|
||||||
|
handler.ServeHTTP(rr, req)
|
||||||
|
|
||||||
|
if rr.Code != http.StatusOK {
|
||||||
|
t.Fatalf("status = %d, want 200", rr.Code)
|
||||||
|
}
|
||||||
|
if got := rr.Body.String(); got != "payload=1" {
|
||||||
|
t.Fatalf("body = %q, want payload=1", got)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestHandler_404FallsBackWithBody(t *testing.T) {
|
||||||
|
upstream := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
w.WriteHeader(http.StatusNotFound)
|
||||||
|
}))
|
||||||
|
defer upstream.Close()
|
||||||
|
|
||||||
|
svc, err := NewService([]Rule{{URLPrefix: "/", Target: upstream.URL}})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("NewService: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
echoBody := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
body, err := io.ReadAll(r.Body)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("fallback reading body: %v", err)
|
||||||
|
}
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
|
_, _ = w.Write(body)
|
||||||
|
})
|
||||||
|
|
||||||
|
handler := svc.Handler(echoBody)
|
||||||
|
|
||||||
|
req := httptest.NewRequest(http.MethodPut, "/missing", strings.NewReader("payload=2"))
|
||||||
|
rr := httptest.NewRecorder()
|
||||||
|
handler.ServeHTTP(rr, req)
|
||||||
|
|
||||||
|
if rr.Code != http.StatusOK {
|
||||||
|
t.Fatalf("status = %d, want 200", rr.Code)
|
||||||
|
}
|
||||||
|
if got := rr.Body.String(); got != "payload=2" {
|
||||||
|
t.Fatalf("body = %q, want payload=2", got)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestHandler_NonNotFoundErrorsPassThrough(t *testing.T) {
|
func TestHandler_NonNotFoundErrorsPassThrough(t *testing.T) {
|
||||||
codes := []int{http.StatusOK, http.StatusForbidden, http.StatusBadRequest, http.StatusInternalServerError}
|
codes := []int{http.StatusOK, http.StatusForbidden, http.StatusBadRequest, http.StatusInternalServerError}
|
||||||
|
|
||||||
@@ -186,6 +268,81 @@ func TestHandler_LongestPrefixMatch(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestHandler_ExcludeFallsBackToFallback(t *testing.T) {
|
||||||
|
upstream := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
_, _ = w.Write([]byte("upstream:" + r.URL.Path))
|
||||||
|
}))
|
||||||
|
defer upstream.Close()
|
||||||
|
|
||||||
|
svc, err := NewService([]Rule{
|
||||||
|
{URLPrefix: "/", Target: upstream.URL, Exclude: []string{"/health"}},
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("NewService: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
handler := svc.Handler(fallbackHandler("fallback-content"))
|
||||||
|
|
||||||
|
req := httptest.NewRequest(http.MethodGet, "/health", nil)
|
||||||
|
rr := httptest.NewRecorder()
|
||||||
|
handler.ServeHTTP(rr, req)
|
||||||
|
|
||||||
|
if got := rr.Body.String(); got != "fallback-content" {
|
||||||
|
t.Fatalf("body = %q, want fallback-content", got)
|
||||||
|
}
|
||||||
|
|
||||||
|
req = httptest.NewRequest(http.MethodGet, "/health/live", nil)
|
||||||
|
rr = httptest.NewRecorder()
|
||||||
|
handler.ServeHTTP(rr, req)
|
||||||
|
|
||||||
|
if got := rr.Body.String(); got != "fallback-content" {
|
||||||
|
t.Fatalf("body = %q, want fallback-content", got)
|
||||||
|
}
|
||||||
|
|
||||||
|
req = httptest.NewRequest(http.MethodGet, "/other", nil)
|
||||||
|
rr = httptest.NewRecorder()
|
||||||
|
handler.ServeHTTP(rr, req)
|
||||||
|
|
||||||
|
if got := rr.Body.String(); got != "upstream:/other" {
|
||||||
|
t.Fatalf("body = %q, want upstream:/other", got)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestHandler_ExcludeFallsThroughToNextRule(t *testing.T) {
|
||||||
|
specific := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
_, _ = w.Write([]byte("specific"))
|
||||||
|
}))
|
||||||
|
defer specific.Close()
|
||||||
|
|
||||||
|
general := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
_, _ = w.Write([]byte("general"))
|
||||||
|
}))
|
||||||
|
defer general.Close()
|
||||||
|
|
||||||
|
svc, err := NewService([]Rule{
|
||||||
|
{URLPrefix: "/api", Target: general.URL},
|
||||||
|
{URLPrefix: "/api/v1", Target: specific.URL, Exclude: []string{"/api/v1/health"}},
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("NewService: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
handler := svc.Handler(fallbackHandler("fallback"))
|
||||||
|
|
||||||
|
for path, want := range map[string]string{
|
||||||
|
"/api/v1/thing": "specific",
|
||||||
|
"/api/v1/health": "general",
|
||||||
|
} {
|
||||||
|
req := httptest.NewRequest(http.MethodGet, path, nil)
|
||||||
|
rr := httptest.NewRecorder()
|
||||||
|
handler.ServeHTTP(rr, req)
|
||||||
|
|
||||||
|
if got := rr.Body.String(); got != want {
|
||||||
|
t.Errorf("path %s: body = %q, want %q", path, got, want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestHandler_NoMatchFallsBack(t *testing.T) {
|
func TestHandler_NoMatchFallsBack(t *testing.T) {
|
||||||
svc, err := NewService([]Rule{{URLPrefix: "/api", Target: "http://127.0.0.1:1"}})
|
svc, err := NewService([]Rule{{URLPrefix: "/api", Target: "http://127.0.0.1:1"}})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
Reference in New Issue
Block a user