♻️ Move transformer to an isolated module
	
		
			
	
		
	
	
		
	
		
			All checks were successful
		
		
	
	
		
			
				
	
				release-nightly / build-docker (push) Successful in 1m3s
				
					
					
				
			
		
		
	
	
				
					
				
			
		
			All checks were successful
		
		
	
	release-nightly / build-docker (push) Successful in 1m3s
				This commit is contained in:
		| @@ -1,10 +0,0 @@ | |||||||
| package sign |  | ||||||
|  |  | ||||||
| import "encoding/json" |  | ||||||
|  |  | ||||||
| func DeserializeOptions[T any](data any) T { |  | ||||||
| 	var out T |  | ||||||
| 	raw, _ := json.Marshal(data) |  | ||||||
| 	_ = json.Unmarshal(raw, &out) |  | ||||||
| 	return out |  | ||||||
| } |  | ||||||
| @@ -1,6 +1,7 @@ | |||||||
| package sign | package sign | ||||||
|  |  | ||||||
| import ( | import ( | ||||||
|  | 	"code.smartsheep.studio/goatworks/roadsign/pkg/sign/transformers" | ||||||
| 	"errors" | 	"errors" | ||||||
| 	"math/rand" | 	"math/rand" | ||||||
|  |  | ||||||
| @@ -39,6 +40,8 @@ func (v *RoadApp) Forward(ctx *fiber.Ctx, site *SiteConfig) error { | |||||||
| 	} | 	} | ||||||
| } | } | ||||||
|  |  | ||||||
|  | type RequestTransformerConfig = transformers.RequestTransformerConfig | ||||||
|  |  | ||||||
| type SiteConfig struct { | type SiteConfig struct { | ||||||
| 	ID           string                      `json:"id"` | 	ID           string                      `json:"id"` | ||||||
| 	Rules        []*RouterRuleConfig         `json:"rules" yaml:"rules"` | 	Rules        []*RouterRuleConfig         `json:"rules" yaml:"rules"` | ||||||
|   | |||||||
| @@ -1,12 +1,12 @@ | |||||||
| package sign | package transformers | ||||||
| 
 | 
 | ||||||
| import ( | import ( | ||||||
| 	"regexp" | 	"encoding/json" | ||||||
| 	"strings" |  | ||||||
| 
 |  | ||||||
| 	"github.com/gofiber/fiber/v2" | 	"github.com/gofiber/fiber/v2" | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
|  | // Definitions | ||||||
|  | 
 | ||||||
| type RequestTransformer struct { | type RequestTransformer struct { | ||||||
| 	ModifyRequest  func(options any, ctx *fiber.Ctx) | 	ModifyRequest  func(options any, ctx *fiber.Ctx) | ||||||
| 	ModifyResponse func(options any, ctx *fiber.Ctx) | 	ModifyResponse func(options any, ctx *fiber.Ctx) | ||||||
| @@ -39,21 +39,18 @@ func (v *RequestTransformerConfig) TransformResponse(ctx *fiber.Ctx) { | |||||||
| 	} | 	} | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| var Transformers = map[string]RequestTransformer{ | // Helpers | ||||||
| 	"replacePath": { | 
 | ||||||
| 		ModifyRequest: func(options any, ctx *fiber.Ctx) { | func DeserializeOptions[T any](data any) T { | ||||||
| 			opts := DeserializeOptions[struct { | 	var out T | ||||||
| 				Pattern string `json:"pattern"` | 	raw, _ := json.Marshal(data) | ||||||
| 				Value   string `json:"value"` | 	_ = json.Unmarshal(raw, &out) | ||||||
| 				Repl    string `json:"repl"` // Use when complex mode(regexp) enabled | 	return out | ||||||
| 				Complex bool   `json:"complex"` | } | ||||||
| 			}](options) | 
 | ||||||
| 			path := string(ctx.Request().URI().Path()) | // Map of Transformers | ||||||
| 			if !opts.Complex { | // Every transformer need to be mapped here so that they can get work. | ||||||
| 				ctx.Path(strings.ReplaceAll(path, opts.Pattern, opts.Value)) | 
 | ||||||
| 			} else if ex := regexp.MustCompile(opts.Pattern); ex != nil { | var Transformers = map[string]RequestTransformer{ | ||||||
| 				ctx.Path(ex.ReplaceAllString(path, opts.Repl)) | 	"replacePath": ReplacePath, | ||||||
| 			} |  | ||||||
| 		}, |  | ||||||
| 	}, |  | ||||||
| } | } | ||||||
							
								
								
									
										24
									
								
								pkg/sign/transformers/replace_path.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										24
									
								
								pkg/sign/transformers/replace_path.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,24 @@ | |||||||
|  | package transformers | ||||||
|  |  | ||||||
|  | import ( | ||||||
|  | 	"github.com/gofiber/fiber/v2" | ||||||
|  | 	"regexp" | ||||||
|  | 	"strings" | ||||||
|  | ) | ||||||
|  |  | ||||||
|  | var ReplacePath = RequestTransformer{ | ||||||
|  | 	ModifyRequest: func(options any, ctx *fiber.Ctx) { | ||||||
|  | 		opts := DeserializeOptions[struct { | ||||||
|  | 			Pattern string `json:"pattern"` | ||||||
|  | 			Value   string `json:"value"` | ||||||
|  | 			Repl    string `json:"repl"` // Use when complex mode(regexp) enabled | ||||||
|  | 			Complex bool   `json:"complex"` | ||||||
|  | 		}](options) | ||||||
|  | 		path := string(ctx.Request().URI().Path()) | ||||||
|  | 		if !opts.Complex { | ||||||
|  | 			ctx.Path(strings.ReplaceAll(path, opts.Pattern, opts.Value)) | ||||||
|  | 		} else if ex := regexp.MustCompile(opts.Pattern); ex != nil { | ||||||
|  | 			ctx.Path(ex.ReplaceAllString(path, opts.Repl)) | ||||||
|  | 		} | ||||||
|  | 	}, | ||||||
|  | } | ||||||
		Reference in New Issue
	
	Block a user