| Akron | 49ceeb4 | 2025-05-23 17:46:01 +0200 | [diff] [blame] | 1 | package main |
| 2 | |
| 3 | import ( |
| Akron | d8a76b3 | 2026-02-20 09:31:56 +0100 | [diff] [blame] | 4 | "bytes" |
| 5 | "embed" |
| Akron | 49ceeb4 | 2025-05-23 17:46:01 +0200 | [diff] [blame] | 6 | "fmt" |
| Akron | d8a76b3 | 2026-02-20 09:31:56 +0100 | [diff] [blame] | 7 | "html/template" |
| 8 | "io/fs" |
| Akron | 8006720 | 2025-06-06 14:16:25 +0200 | [diff] [blame] | 9 | "net/url" |
| Akron | 49ceeb4 | 2025-05-23 17:46:01 +0200 | [diff] [blame] | 10 | "os" |
| 11 | "os/signal" |
| Akron | 8006720 | 2025-06-06 14:16:25 +0200 | [diff] [blame] | 12 | "path" |
| Akron | 14678dc | 2025-06-05 13:01:38 +0200 | [diff] [blame] | 13 | "path/filepath" |
| Akron | d8a76b3 | 2026-02-20 09:31:56 +0100 | [diff] [blame] | 14 | "strconv" |
| Akron | 49ceeb4 | 2025-05-23 17:46:01 +0200 | [diff] [blame] | 15 | "strings" |
| 16 | "syscall" |
| Akron | d8a76b3 | 2026-02-20 09:31:56 +0100 | [diff] [blame] | 17 | texttemplate "text/template" |
| Akron | 3caee16 | 2025-07-01 17:44:58 +0200 | [diff] [blame] | 18 | "time" |
| Akron | 49ceeb4 | 2025-05-23 17:46:01 +0200 | [diff] [blame] | 19 | |
| Akron | 2ef703c | 2025-07-03 15:57:42 +0200 | [diff] [blame] | 20 | "github.com/KorAP/Koral-Mapper/config" |
| 21 | "github.com/KorAP/Koral-Mapper/mapper" |
| Akron | 1fc750e | 2025-05-26 16:54:18 +0200 | [diff] [blame] | 22 | "github.com/alecthomas/kong" |
| Akron | 49ceeb4 | 2025-05-23 17:46:01 +0200 | [diff] [blame] | 23 | "github.com/gofiber/fiber/v2" |
| 24 | "github.com/rs/zerolog" |
| 25 | "github.com/rs/zerolog/log" |
| 26 | ) |
| 27 | |
| Akron | d8a76b3 | 2026-02-20 09:31:56 +0100 | [diff] [blame] | 28 | //go:embed static/* |
| 29 | var staticFS embed.FS |
| 30 | |
| Akron | 74e1c07 | 2025-05-26 14:38:25 +0200 | [diff] [blame] | 31 | const ( |
| 32 | maxInputLength = 1024 * 1024 // 1MB |
| 33 | maxParamLength = 1024 // 1KB |
| 34 | ) |
| 35 | |
| Akron | a00d475 | 2025-05-26 17:34:36 +0200 | [diff] [blame] | 36 | type appConfig struct { |
| Akron | a8a66ce | 2025-06-05 10:50:17 +0200 | [diff] [blame] | 37 | Port *int `kong:"short='p',help='Port to listen on'"` |
| Akron | e1cff7c | 2025-06-04 18:43:32 +0200 | [diff] [blame] | 38 | Config string `kong:"short='c',help='YAML configuration file containing mapping directives and global settings'"` |
| Akron | 14678dc | 2025-06-05 13:01:38 +0200 | [diff] [blame] | 39 | Mappings []string `kong:"short='m',help='Individual YAML mapping files to load (supports glob patterns like dir/*.yaml)'"` |
| Akron | a8a66ce | 2025-06-05 10:50:17 +0200 | [diff] [blame] | 40 | LogLevel *string `kong:"short='l',help='Log level (debug, info, warn, error)'"` |
| Akron | 49ceeb4 | 2025-05-23 17:46:01 +0200 | [diff] [blame] | 41 | } |
| 42 | |
| Akron | d8a76b3 | 2026-02-20 09:31:56 +0100 | [diff] [blame] | 43 | type BasePageData struct { |
| Akron | 40aaa63 | 2025-06-03 17:57:52 +0200 | [diff] [blame] | 44 | Title string |
| 45 | Version string |
| Akron | fc77b5e | 2025-06-04 11:44:43 +0200 | [diff] [blame] | 46 | Hash string |
| 47 | Date string |
| Akron | 40aaa63 | 2025-06-03 17:57:52 +0200 | [diff] [blame] | 48 | Description string |
| Akron | 06d21f0 | 2025-06-04 14:36:07 +0200 | [diff] [blame] | 49 | Server string |
| 50 | SDK string |
| Akron | 43fb102 | 2026-02-20 11:38:49 +0100 | [diff] [blame] | 51 | Stylesheet string |
| Akron | 2ac2ec0 | 2025-06-05 15:26:42 +0200 | [diff] [blame] | 52 | ServiceURL string |
| Akron | 43fb102 | 2026-02-20 11:38:49 +0100 | [diff] [blame] | 53 | CookieName string |
| Akron | d8a76b3 | 2026-02-20 09:31:56 +0100 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | type SingleMappingPageData struct { |
| 57 | BasePageData |
| Akron | c376dcc | 2025-06-04 17:00:18 +0200 | [diff] [blame] | 58 | MapID string |
| Akron | d8a76b3 | 2026-02-20 09:31:56 +0100 | [diff] [blame] | 59 | Mappings []config.MappingList |
| 60 | QueryURL string |
| 61 | ResponseURL string |
| Akron | 40aaa63 | 2025-06-03 17:57:52 +0200 | [diff] [blame] | 62 | } |
| 63 | |
| Akron | cb51f81 | 2025-06-30 15:24:20 +0200 | [diff] [blame] | 64 | type QueryParams struct { |
| 65 | Dir string |
| 66 | FoundryA string |
| 67 | FoundryB string |
| 68 | LayerA string |
| 69 | LayerB string |
| 70 | } |
| 71 | |
| Akron | 49b525c | 2025-07-03 15:17:06 +0200 | [diff] [blame] | 72 | // requestParams holds common request parameters |
| 73 | type requestParams struct { |
| 74 | MapID string |
| 75 | Dir string |
| 76 | FoundryA string |
| 77 | FoundryB string |
| 78 | LayerA string |
| 79 | LayerB string |
| Akron | 8414ae5 | 2026-05-19 13:31:14 +0200 | [diff] [blame] | 80 | Rewrites *bool // nil = use mapping list default; non-nil = override |
| Akron | 49b525c | 2025-07-03 15:17:06 +0200 | [diff] [blame] | 81 | } |
| 82 | |
| Akron | 247a93a | 2026-02-20 16:28:40 +0100 | [diff] [blame] | 83 | // MappingSectionData contains per-section UI metadata so request and response |
| 84 | // rows can be rendered from one shared template block. |
| 85 | type MappingSectionData struct { |
| 86 | Title string |
| 87 | Mode string |
| 88 | CheckboxClass string |
| 89 | CheckboxName string |
| 90 | FieldsClass string |
| 91 | ArrowClass string |
| 92 | ArrowDirection string |
| 93 | ArrowLabel string |
| 94 | AnnotationLabel string |
| 95 | } |
| 96 | |
| Akron | d8a76b3 | 2026-02-20 09:31:56 +0100 | [diff] [blame] | 97 | // ConfigPageData holds all data passed to the configuration page template. |
| 98 | type ConfigPageData struct { |
| 99 | BasePageData |
| 100 | AnnotationMappings []config.MappingList |
| 101 | CorpusMappings []config.MappingList |
| Akron | 247a93a | 2026-02-20 16:28:40 +0100 | [diff] [blame] | 102 | MappingSections []MappingSectionData |
| Akron | d8a76b3 | 2026-02-20 09:31:56 +0100 | [diff] [blame] | 103 | } |
| 104 | |
| Akron | a00d475 | 2025-05-26 17:34:36 +0200 | [diff] [blame] | 105 | func parseConfig() *appConfig { |
| 106 | cfg := &appConfig{} |
| Akron | fc77b5e | 2025-06-04 11:44:43 +0200 | [diff] [blame] | 107 | |
| 108 | desc := config.Description |
| 109 | desc += " [" + config.Version + "]" |
| 110 | |
| Akron | 1fc750e | 2025-05-26 16:54:18 +0200 | [diff] [blame] | 111 | ctx := kong.Parse(cfg, |
| Akron | fc77b5e | 2025-06-04 11:44:43 +0200 | [diff] [blame] | 112 | kong.Description(desc), |
| Akron | 1fc750e | 2025-05-26 16:54:18 +0200 | [diff] [blame] | 113 | kong.UsageOnError(), |
| 114 | ) |
| 115 | if ctx.Error != nil { |
| 116 | fmt.Fprintln(os.Stderr, ctx.Error) |
| Akron | 49ceeb4 | 2025-05-23 17:46:01 +0200 | [diff] [blame] | 117 | os.Exit(1) |
| 118 | } |
| Akron | 49ceeb4 | 2025-05-23 17:46:01 +0200 | [diff] [blame] | 119 | return cfg |
| 120 | } |
| 121 | |
| 122 | func setupLogger(level string) { |
| 123 | // Parse log level |
| 124 | lvl, err := zerolog.ParseLevel(strings.ToLower(level)) |
| 125 | if err != nil { |
| 126 | log.Error().Err(err).Str("level", level).Msg("Invalid log level, defaulting to info") |
| 127 | lvl = zerolog.InfoLevel |
| 128 | } |
| 129 | |
| 130 | // Configure zerolog |
| 131 | zerolog.SetGlobalLevel(lvl) |
| 132 | log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr}) |
| 133 | } |
| 134 | |
| Akron | 3caee16 | 2025-07-01 17:44:58 +0200 | [diff] [blame] | 135 | // setupFiberLogger configures fiber's logger middleware to integrate with zerolog |
| 136 | func setupFiberLogger() fiber.Handler { |
| 137 | // Check if HTTP request logging should be enabled based on current log level |
| 138 | currentLevel := zerolog.GlobalLevel() |
| 139 | |
| 140 | // Only enable HTTP request logging if log level is debug or info |
| 141 | if currentLevel > zerolog.InfoLevel { |
| 142 | return func(c *fiber.Ctx) error { |
| 143 | return c.Next() |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | return func(c *fiber.Ctx) error { |
| 148 | // Record start time |
| 149 | start := time.Now() |
| 150 | |
| 151 | // Process request |
| 152 | err := c.Next() |
| 153 | |
| 154 | // Calculate latency |
| 155 | latency := time.Since(start) |
| 156 | status := c.Response().StatusCode() |
| 157 | |
| 158 | // Determine log level based on status code |
| 159 | logEvent := log.Info() |
| 160 | if status >= 400 && status < 500 { |
| 161 | logEvent = log.Warn() |
| 162 | } else if status >= 500 { |
| 163 | logEvent = log.Error() |
| 164 | } |
| 165 | |
| 166 | // Log the request |
| 167 | logEvent. |
| 168 | Int("status", status). |
| 169 | Dur("latency", latency). |
| 170 | Str("method", c.Method()). |
| 171 | Str("path", c.Path()). |
| 172 | Str("ip", c.IP()). |
| 173 | Str("user_agent", c.Get("User-Agent")). |
| 174 | Msg("HTTP request") |
| 175 | |
| 176 | return err |
| 177 | } |
| 178 | } |
| 179 | |
| Akron | 49b525c | 2025-07-03 15:17:06 +0200 | [diff] [blame] | 180 | // extractRequestParams extracts and validates common request parameters |
| 181 | func extractRequestParams(c *fiber.Ctx) (*requestParams, error) { |
| 182 | params := &requestParams{ |
| 183 | MapID: c.Params("map"), |
| 184 | Dir: c.Query("dir", "atob"), |
| 185 | FoundryA: c.Query("foundryA", ""), |
| 186 | FoundryB: c.Query("foundryB", ""), |
| 187 | LayerA: c.Query("layerA", ""), |
| 188 | LayerB: c.Query("layerB", ""), |
| 189 | } |
| 190 | |
| Akron | 8414ae5 | 2026-05-19 13:31:14 +0200 | [diff] [blame] | 191 | if rewrites := c.Query("rewrites", ""); rewrites != "" { |
| 192 | v := rewrites == "true" |
| 193 | params.Rewrites = &v |
| 194 | } |
| 195 | |
| Akron | 49b525c | 2025-07-03 15:17:06 +0200 | [diff] [blame] | 196 | // Validate input parameters |
| 197 | if err := validateInput(params.MapID, params.Dir, params.FoundryA, params.FoundryB, params.LayerA, params.LayerB, c.Body()); err != nil { |
| 198 | return nil, err |
| 199 | } |
| 200 | |
| 201 | // Validate direction |
| 202 | if params.Dir != "atob" && params.Dir != "btoa" { |
| 203 | return nil, fmt.Errorf("invalid direction, must be 'atob' or 'btoa'") |
| 204 | } |
| 205 | |
| 206 | return params, nil |
| 207 | } |
| 208 | |
| 209 | // parseRequestBody parses JSON request body and direction |
| 210 | func parseRequestBody(c *fiber.Ctx, dir string) (any, mapper.Direction, error) { |
| 211 | var jsonData any |
| 212 | if err := c.BodyParser(&jsonData); err != nil { |
| 213 | return nil, mapper.BtoA, fmt.Errorf("invalid JSON in request body") |
| 214 | } |
| 215 | |
| 216 | direction, err := mapper.ParseDirection(dir) |
| 217 | if err != nil { |
| 218 | return nil, mapper.BtoA, err |
| 219 | } |
| 220 | |
| 221 | return jsonData, direction, nil |
| 222 | } |
| 223 | |
| Akron | 49ceeb4 | 2025-05-23 17:46:01 +0200 | [diff] [blame] | 224 | func main() { |
| 225 | // Parse command line flags |
| Akron | 1fc750e | 2025-05-26 16:54:18 +0200 | [diff] [blame] | 226 | cfg := parseConfig() |
| Akron | 49ceeb4 | 2025-05-23 17:46:01 +0200 | [diff] [blame] | 227 | |
| Akron | e1cff7c | 2025-06-04 18:43:32 +0200 | [diff] [blame] | 228 | // Validate command line arguments |
| 229 | if cfg.Config == "" && len(cfg.Mappings) == 0 { |
| 230 | log.Fatal().Msg("At least one configuration source must be provided: use -c for main config file or -m for mapping files") |
| 231 | } |
| 232 | |
| Akron | 14678dc | 2025-06-05 13:01:38 +0200 | [diff] [blame] | 233 | // Expand glob patterns in mapping files |
| 234 | expandedMappings, err := expandGlobs(cfg.Mappings) |
| 235 | if err != nil { |
| 236 | log.Fatal().Err(err).Msg("Failed to expand glob patterns in mapping files") |
| 237 | } |
| 238 | |
| Akron | e1cff7c | 2025-06-04 18:43:32 +0200 | [diff] [blame] | 239 | // Load configuration from multiple sources |
| Akron | 14678dc | 2025-06-05 13:01:38 +0200 | [diff] [blame] | 240 | yamlConfig, err := config.LoadFromSources(cfg.Config, expandedMappings) |
| Akron | a00d475 | 2025-05-26 17:34:36 +0200 | [diff] [blame] | 241 | if err != nil { |
| 242 | log.Fatal().Err(err).Msg("Failed to load configuration") |
| 243 | } |
| 244 | |
| Akron | a8a66ce | 2025-06-05 10:50:17 +0200 | [diff] [blame] | 245 | finalPort := yamlConfig.Port |
| 246 | finalLogLevel := yamlConfig.LogLevel |
| 247 | |
| 248 | // Use command line values if provided (they override config file) |
| 249 | if cfg.Port != nil { |
| 250 | finalPort = *cfg.Port |
| 251 | } |
| 252 | if cfg.LogLevel != nil { |
| 253 | finalLogLevel = *cfg.LogLevel |
| 254 | } |
| 255 | |
| 256 | // Set up logging with the final log level |
| 257 | setupLogger(finalLogLevel) |
| 258 | |
| Akron | 49ceeb4 | 2025-05-23 17:46:01 +0200 | [diff] [blame] | 259 | // Create a new mapper instance |
| Akron | a00d475 | 2025-05-26 17:34:36 +0200 | [diff] [blame] | 260 | m, err := mapper.NewMapper(yamlConfig.Lists) |
| Akron | 49ceeb4 | 2025-05-23 17:46:01 +0200 | [diff] [blame] | 261 | if err != nil { |
| 262 | log.Fatal().Err(err).Msg("Failed to create mapper") |
| 263 | } |
| 264 | |
| 265 | // Create fiber app |
| 266 | app := fiber.New(fiber.Config{ |
| 267 | DisableStartupMessage: true, |
| Akron | 74e1c07 | 2025-05-26 14:38:25 +0200 | [diff] [blame] | 268 | BodyLimit: maxInputLength, |
| Akron | afbe86d | 2025-07-01 08:45:13 +0200 | [diff] [blame] | 269 | ReadBufferSize: 64 * 1024, // 64KB - increase header size limit |
| 270 | WriteBufferSize: 64 * 1024, // 64KB - increase response buffer size |
| Akron | 49ceeb4 | 2025-05-23 17:46:01 +0200 | [diff] [blame] | 271 | }) |
| 272 | |
| Akron | 3caee16 | 2025-07-01 17:44:58 +0200 | [diff] [blame] | 273 | // Add zerolog-integrated logger middleware |
| 274 | app.Use(setupFiberLogger()) |
| 275 | |
| Akron | 49ceeb4 | 2025-05-23 17:46:01 +0200 | [diff] [blame] | 276 | // Set up routes |
| Akron | 40aaa63 | 2025-06-03 17:57:52 +0200 | [diff] [blame] | 277 | setupRoutes(app, m, yamlConfig) |
| Akron | 49ceeb4 | 2025-05-23 17:46:01 +0200 | [diff] [blame] | 278 | |
| 279 | // Start server |
| 280 | go func() { |
| Akron | a8a66ce | 2025-06-05 10:50:17 +0200 | [diff] [blame] | 281 | log.Info().Int("port", finalPort).Msg("Starting server") |
| Akron | d8a76b3 | 2026-02-20 09:31:56 +0100 | [diff] [blame] | 282 | fmt.Printf("Starting server port=%d\n", finalPort) |
| Akron | ae3ffde | 2025-06-05 14:04:06 +0200 | [diff] [blame] | 283 | |
| 284 | for _, list := range yamlConfig.Lists { |
| 285 | log.Info().Str("id", list.ID).Str("desc", list.Description).Msg("Loaded mapping") |
| Akron | d8a76b3 | 2026-02-20 09:31:56 +0100 | [diff] [blame] | 286 | fmt.Printf("Loaded mapping desc=%s id=%s\n", |
| 287 | formatConsoleField(list.Description), |
| 288 | list.ID, |
| 289 | ) |
| Akron | ae3ffde | 2025-06-05 14:04:06 +0200 | [diff] [blame] | 290 | } |
| 291 | |
| Akron | a8a66ce | 2025-06-05 10:50:17 +0200 | [diff] [blame] | 292 | if err := app.Listen(fmt.Sprintf(":%d", finalPort)); err != nil { |
| Akron | 49ceeb4 | 2025-05-23 17:46:01 +0200 | [diff] [blame] | 293 | log.Fatal().Err(err).Msg("Server error") |
| 294 | } |
| 295 | }() |
| 296 | |
| 297 | // Wait for interrupt signal |
| 298 | sigChan := make(chan os.Signal, 1) |
| 299 | signal.Notify(sigChan, os.Interrupt, syscall.SIGTERM) |
| 300 | <-sigChan |
| 301 | |
| 302 | // Graceful shutdown |
| 303 | log.Info().Msg("Shutting down server") |
| 304 | if err := app.Shutdown(); err != nil { |
| 305 | log.Error().Err(err).Msg("Error during shutdown") |
| 306 | } |
| 307 | } |
| 308 | |
| Akron | 06d21f0 | 2025-06-04 14:36:07 +0200 | [diff] [blame] | 309 | func setupRoutes(app *fiber.App, m *mapper.Mapper, yamlConfig *config.MappingConfig) { |
| Akron | d8a76b3 | 2026-02-20 09:31:56 +0100 | [diff] [blame] | 310 | configTmpl := template.Must(template.ParseFS(staticFS, "static/config.html")) |
| 311 | pluginTmpl := texttemplate.Must(texttemplate.ParseFS(staticFS, "static/plugin.html")) |
| 312 | |
| Akron | 49ceeb4 | 2025-05-23 17:46:01 +0200 | [diff] [blame] | 313 | // Health check endpoint |
| 314 | app.Get("/health", func(c *fiber.Ctx) error { |
| 315 | return c.SendString("OK") |
| 316 | }) |
| 317 | |
| Akron | d8a76b3 | 2026-02-20 09:31:56 +0100 | [diff] [blame] | 318 | // Static file serving from embedded FS |
| 319 | app.Get("/static/*", handleStaticFile()) |
| 320 | |
| Akron | bf73a12 | 2026-02-27 15:02:16 +0100 | [diff] [blame] | 321 | // Composite cascade transformation endpoints (cfg in path) |
| 322 | app.Post("/query/:cfg", handleCompositeQueryTransform(m, yamlConfig.Lists)) |
| 323 | app.Post("/response/:cfg", handleCompositeResponseTransform(m, yamlConfig.Lists)) |
| Akron | 512aab6 | 2026-02-20 08:36:12 +0100 | [diff] [blame] | 324 | |
| Akron | 49ceeb4 | 2025-05-23 17:46:01 +0200 | [diff] [blame] | 325 | // Transformation endpoint |
| Akron | 8414ae5 | 2026-05-19 13:31:14 +0200 | [diff] [blame] | 326 | app.Post("/:map/query", handleTransform(m, yamlConfig.Lists)) |
| Akron | 40aaa63 | 2025-06-03 17:57:52 +0200 | [diff] [blame] | 327 | |
| Akron | 4de47a9 | 2025-06-27 11:58:11 +0200 | [diff] [blame] | 328 | // Response transformation endpoint |
| Akron | 8414ae5 | 2026-05-19 13:31:14 +0200 | [diff] [blame] | 329 | app.Post("/:map/response", handleResponseTransform(m, yamlConfig.Lists)) |
| Akron | 4de47a9 | 2025-06-27 11:58:11 +0200 | [diff] [blame] | 330 | |
| Akron | 40aaa63 | 2025-06-03 17:57:52 +0200 | [diff] [blame] | 331 | // Kalamar plugin endpoint |
| Akron | d8a76b3 | 2026-02-20 09:31:56 +0100 | [diff] [blame] | 332 | app.Get("/", handleKalamarPlugin(yamlConfig, configTmpl, pluginTmpl)) |
| 333 | app.Get("/:map", handleKalamarPlugin(yamlConfig, configTmpl, pluginTmpl)) |
| 334 | } |
| 335 | |
| 336 | func handleStaticFile() fiber.Handler { |
| 337 | return func(c *fiber.Ctx) error { |
| 338 | name := c.Params("*") |
| 339 | data, err := fs.ReadFile(staticFS, "static/"+name) |
| 340 | if err != nil { |
| 341 | return c.Status(fiber.StatusNotFound).SendString("not found") |
| 342 | } |
| 343 | switch { |
| 344 | case strings.HasSuffix(name, ".js"): |
| 345 | c.Set("Content-Type", "text/javascript; charset=utf-8") |
| 346 | case strings.HasSuffix(name, ".css"): |
| 347 | c.Set("Content-Type", "text/css; charset=utf-8") |
| 348 | case strings.HasSuffix(name, ".html"): |
| 349 | c.Set("Content-Type", "text/html; charset=utf-8") |
| 350 | } |
| 351 | return c.Send(data) |
| 352 | } |
| 353 | } |
| 354 | |
| 355 | func buildBasePageData(yamlConfig *config.MappingConfig) BasePageData { |
| 356 | return BasePageData{ |
| 357 | Title: config.Title, |
| 358 | Version: config.Version, |
| 359 | Hash: config.Buildhash, |
| 360 | Date: config.Buildtime, |
| 361 | Description: config.Description, |
| 362 | Server: yamlConfig.Server, |
| 363 | SDK: yamlConfig.SDK, |
| Akron | 43fb102 | 2026-02-20 11:38:49 +0100 | [diff] [blame] | 364 | Stylesheet: yamlConfig.Stylesheet, |
| Akron | d8a76b3 | 2026-02-20 09:31:56 +0100 | [diff] [blame] | 365 | ServiceURL: yamlConfig.ServiceURL, |
| Akron | 43fb102 | 2026-02-20 11:38:49 +0100 | [diff] [blame] | 366 | CookieName: yamlConfig.CookieName, |
| Akron | d8a76b3 | 2026-02-20 09:31:56 +0100 | [diff] [blame] | 367 | } |
| 368 | } |
| 369 | |
| 370 | func buildConfigPageData(yamlConfig *config.MappingConfig) ConfigPageData { |
| 371 | data := ConfigPageData{ |
| 372 | BasePageData: buildBasePageData(yamlConfig), |
| 373 | } |
| 374 | |
| 375 | for _, list := range yamlConfig.Lists { |
| 376 | normalized := list |
| 377 | if normalized.Type == "" { |
| 378 | normalized.Type = "annotation" |
| 379 | } |
| 380 | if list.IsCorpus() { |
| 381 | data.CorpusMappings = append(data.CorpusMappings, normalized) |
| 382 | } else { |
| 383 | data.AnnotationMappings = append(data.AnnotationMappings, normalized) |
| 384 | } |
| 385 | } |
| Akron | 247a93a | 2026-02-20 16:28:40 +0100 | [diff] [blame] | 386 | |
| 387 | data.MappingSections = []MappingSectionData{ |
| 388 | { |
| Akron | 8bdf520 | 2026-02-24 10:01:15 +0100 | [diff] [blame] | 389 | Title: "Request", |
| 390 | Mode: "request", |
| 391 | CheckboxClass: "request-cb", |
| 392 | CheckboxName: "request", |
| 393 | FieldsClass: "request-fields", |
| 394 | ArrowClass: "request-dir-arrow", |
| 395 | ArrowDirection: "atob", |
| 396 | ArrowLabel: "\u2192", |
| Akron | 247a93a | 2026-02-20 16:28:40 +0100 | [diff] [blame] | 397 | }, |
| 398 | { |
| Akron | 8bdf520 | 2026-02-24 10:01:15 +0100 | [diff] [blame] | 399 | Title: "Response", |
| 400 | Mode: "response", |
| 401 | CheckboxClass: "response-cb", |
| 402 | CheckboxName: "response", |
| 403 | FieldsClass: "response-fields", |
| 404 | ArrowClass: "response-dir-arrow", |
| 405 | ArrowDirection: "btoa", |
| 406 | ArrowLabel: "\u2190", |
| Akron | 247a93a | 2026-02-20 16:28:40 +0100 | [diff] [blame] | 407 | }, |
| 408 | } |
| 409 | |
| Akron | d8a76b3 | 2026-02-20 09:31:56 +0100 | [diff] [blame] | 410 | return data |
| Akron | 49ceeb4 | 2025-05-23 17:46:01 +0200 | [diff] [blame] | 411 | } |
| 412 | |
| Akron | 512aab6 | 2026-02-20 08:36:12 +0100 | [diff] [blame] | 413 | func handleCompositeQueryTransform(m *mapper.Mapper, lists []config.MappingList) fiber.Handler { |
| Akron | 8414ae5 | 2026-05-19 13:31:14 +0200 | [diff] [blame] | 414 | listsByID := make(map[string]*config.MappingList, len(lists)) |
| 415 | for i := range lists { |
| 416 | listsByID[lists[i].ID] = &lists[i] |
| 417 | } |
| 418 | |
| Akron | 512aab6 | 2026-02-20 08:36:12 +0100 | [diff] [blame] | 419 | return func(c *fiber.Ctx) error { |
| Akron | bf73a12 | 2026-02-27 15:02:16 +0100 | [diff] [blame] | 420 | cfgRaw := c.Params("cfg") |
| Akron | 512aab6 | 2026-02-20 08:36:12 +0100 | [diff] [blame] | 421 | if len(cfgRaw) > maxParamLength { |
| 422 | return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{ |
| 423 | "error": fmt.Sprintf("cfg too long (max %d bytes)", maxParamLength), |
| 424 | }) |
| 425 | } |
| 426 | |
| 427 | var jsonData any |
| 428 | if err := c.BodyParser(&jsonData); err != nil { |
| 429 | return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{ |
| 430 | "error": "invalid JSON in request body", |
| 431 | }) |
| 432 | } |
| 433 | |
| 434 | entries, err := ParseCfgParam(cfgRaw, lists) |
| 435 | if err != nil { |
| 436 | return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{ |
| 437 | "error": err.Error(), |
| 438 | }) |
| 439 | } |
| 440 | |
| 441 | if len(entries) == 0 { |
| 442 | return c.JSON(jsonData) |
| 443 | } |
| 444 | |
| Akron | 8414ae5 | 2026-05-19 13:31:14 +0200 | [diff] [blame] | 445 | rewrites := c.Query("rewrites", "") |
| 446 | var rewritesOverride *bool |
| 447 | if rewrites != "" { |
| 448 | v := rewrites == "true" |
| 449 | rewritesOverride = &v |
| 450 | } |
| 451 | |
| Akron | 512aab6 | 2026-02-20 08:36:12 +0100 | [diff] [blame] | 452 | orderedIDs := make([]string, 0, len(entries)) |
| 453 | opts := make([]mapper.MappingOptions, 0, len(entries)) |
| 454 | for _, entry := range entries { |
| 455 | dir := mapper.AtoB |
| 456 | if entry.Direction == "btoa" { |
| 457 | dir = mapper.BtoA |
| 458 | } |
| 459 | |
| Akron | 8414ae5 | 2026-05-19 13:31:14 +0200 | [diff] [blame] | 460 | addRewrites := false |
| 461 | if list, ok := listsByID[entry.ID]; ok { |
| 462 | addRewrites = list.Rewrites |
| 463 | } |
| 464 | if rewritesOverride != nil { |
| 465 | addRewrites = *rewritesOverride |
| 466 | } |
| 467 | |
| Akron | 512aab6 | 2026-02-20 08:36:12 +0100 | [diff] [blame] | 468 | orderedIDs = append(orderedIDs, entry.ID) |
| 469 | opts = append(opts, mapper.MappingOptions{ |
| Akron | 8414ae5 | 2026-05-19 13:31:14 +0200 | [diff] [blame] | 470 | Direction: dir, |
| 471 | FoundryA: entry.FoundryA, |
| 472 | LayerA: entry.LayerA, |
| 473 | FoundryB: entry.FoundryB, |
| 474 | LayerB: entry.LayerB, |
| 475 | FieldA: entry.FieldA, |
| 476 | FieldB: entry.FieldB, |
| 477 | AddRewrites: addRewrites, |
| Akron | 512aab6 | 2026-02-20 08:36:12 +0100 | [diff] [blame] | 478 | }) |
| 479 | } |
| 480 | |
| 481 | result, err := m.CascadeQueryMappings(orderedIDs, opts, jsonData) |
| 482 | if err != nil { |
| 483 | log.Error().Err(err).Str("cfg", cfgRaw).Msg("Failed to apply composite query mappings") |
| 484 | return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{ |
| 485 | "error": err.Error(), |
| 486 | }) |
| 487 | } |
| 488 | |
| 489 | return c.JSON(result) |
| 490 | } |
| 491 | } |
| 492 | |
| 493 | func handleCompositeResponseTransform(m *mapper.Mapper, lists []config.MappingList) fiber.Handler { |
| Akron | 8414ae5 | 2026-05-19 13:31:14 +0200 | [diff] [blame] | 494 | listsByID := make(map[string]*config.MappingList, len(lists)) |
| 495 | for i := range lists { |
| 496 | listsByID[lists[i].ID] = &lists[i] |
| 497 | } |
| 498 | |
| Akron | 512aab6 | 2026-02-20 08:36:12 +0100 | [diff] [blame] | 499 | return func(c *fiber.Ctx) error { |
| Akron | bf73a12 | 2026-02-27 15:02:16 +0100 | [diff] [blame] | 500 | cfgRaw := c.Params("cfg") |
| Akron | 512aab6 | 2026-02-20 08:36:12 +0100 | [diff] [blame] | 501 | if len(cfgRaw) > maxParamLength { |
| 502 | return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{ |
| 503 | "error": fmt.Sprintf("cfg too long (max %d bytes)", maxParamLength), |
| 504 | }) |
| 505 | } |
| 506 | |
| 507 | var jsonData any |
| 508 | if err := c.BodyParser(&jsonData); err != nil { |
| 509 | return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{ |
| 510 | "error": "invalid JSON in request body", |
| 511 | }) |
| 512 | } |
| 513 | |
| 514 | entries, err := ParseCfgParam(cfgRaw, lists) |
| 515 | if err != nil { |
| 516 | return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{ |
| 517 | "error": err.Error(), |
| 518 | }) |
| 519 | } |
| 520 | |
| 521 | if len(entries) == 0 { |
| 522 | return c.JSON(jsonData) |
| 523 | } |
| 524 | |
| Akron | 8414ae5 | 2026-05-19 13:31:14 +0200 | [diff] [blame] | 525 | rewrites := c.Query("rewrites", "") |
| 526 | var rewritesOverride *bool |
| 527 | if rewrites != "" { |
| 528 | v := rewrites == "true" |
| 529 | rewritesOverride = &v |
| 530 | } |
| 531 | |
| Akron | 512aab6 | 2026-02-20 08:36:12 +0100 | [diff] [blame] | 532 | orderedIDs := make([]string, 0, len(entries)) |
| 533 | opts := make([]mapper.MappingOptions, 0, len(entries)) |
| 534 | for _, entry := range entries { |
| 535 | dir := mapper.AtoB |
| 536 | if entry.Direction == "btoa" { |
| 537 | dir = mapper.BtoA |
| 538 | } |
| 539 | |
| Akron | 8414ae5 | 2026-05-19 13:31:14 +0200 | [diff] [blame] | 540 | addRewrites := false |
| 541 | if list, ok := listsByID[entry.ID]; ok { |
| 542 | addRewrites = list.Rewrites |
| 543 | } |
| 544 | if rewritesOverride != nil { |
| 545 | addRewrites = *rewritesOverride |
| 546 | } |
| 547 | |
| Akron | 512aab6 | 2026-02-20 08:36:12 +0100 | [diff] [blame] | 548 | orderedIDs = append(orderedIDs, entry.ID) |
| 549 | opts = append(opts, mapper.MappingOptions{ |
| Akron | 8414ae5 | 2026-05-19 13:31:14 +0200 | [diff] [blame] | 550 | Direction: dir, |
| 551 | FoundryA: entry.FoundryA, |
| 552 | LayerA: entry.LayerA, |
| 553 | FoundryB: entry.FoundryB, |
| 554 | LayerB: entry.LayerB, |
| 555 | FieldA: entry.FieldA, |
| 556 | FieldB: entry.FieldB, |
| 557 | AddRewrites: addRewrites, |
| Akron | 512aab6 | 2026-02-20 08:36:12 +0100 | [diff] [blame] | 558 | }) |
| 559 | } |
| 560 | |
| 561 | result, err := m.CascadeResponseMappings(orderedIDs, opts, jsonData) |
| 562 | if err != nil { |
| 563 | log.Error().Err(err).Str("cfg", cfgRaw).Msg("Failed to apply composite response mappings") |
| 564 | return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{ |
| 565 | "error": err.Error(), |
| 566 | }) |
| 567 | } |
| 568 | |
| 569 | return c.JSON(result) |
| 570 | } |
| 571 | } |
| 572 | |
| Akron | 8414ae5 | 2026-05-19 13:31:14 +0200 | [diff] [blame] | 573 | func handleTransform(m *mapper.Mapper, lists []config.MappingList) fiber.Handler { |
| 574 | listsByID := make(map[string]*config.MappingList, len(lists)) |
| 575 | for i := range lists { |
| 576 | listsByID[lists[i].ID] = &lists[i] |
| 577 | } |
| 578 | |
| Akron | 49ceeb4 | 2025-05-23 17:46:01 +0200 | [diff] [blame] | 579 | return func(c *fiber.Ctx) error { |
| Akron | 49b525c | 2025-07-03 15:17:06 +0200 | [diff] [blame] | 580 | // Extract and validate parameters |
| 581 | params, err := extractRequestParams(c) |
| 582 | if err != nil { |
| Akron | 74e1c07 | 2025-05-26 14:38:25 +0200 | [diff] [blame] | 583 | return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{ |
| 584 | "error": err.Error(), |
| 585 | }) |
| 586 | } |
| 587 | |
| Akron | 49ceeb4 | 2025-05-23 17:46:01 +0200 | [diff] [blame] | 588 | // Parse request body |
| Akron | 49b525c | 2025-07-03 15:17:06 +0200 | [diff] [blame] | 589 | jsonData, direction, err := parseRequestBody(c, params.Dir) |
| Akron | a1a183f | 2025-05-26 17:47:33 +0200 | [diff] [blame] | 590 | if err != nil { |
| 591 | return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{ |
| 592 | "error": err.Error(), |
| 593 | }) |
| 594 | } |
| 595 | |
| Akron | 8414ae5 | 2026-05-19 13:31:14 +0200 | [diff] [blame] | 596 | // Determine rewrites: query param overrides YAML default |
| 597 | addRewrites := false |
| 598 | if list, ok := listsByID[params.MapID]; ok { |
| 599 | addRewrites = list.Rewrites |
| 600 | } |
| 601 | if params.Rewrites != nil { |
| 602 | addRewrites = *params.Rewrites |
| 603 | } |
| 604 | |
| Akron | 49ceeb4 | 2025-05-23 17:46:01 +0200 | [diff] [blame] | 605 | // Apply mappings |
| Akron | 49b525c | 2025-07-03 15:17:06 +0200 | [diff] [blame] | 606 | result, err := m.ApplyQueryMappings(params.MapID, mapper.MappingOptions{ |
| Akron | 8414ae5 | 2026-05-19 13:31:14 +0200 | [diff] [blame] | 607 | Direction: direction, |
| 608 | FoundryA: params.FoundryA, |
| 609 | FoundryB: params.FoundryB, |
| 610 | LayerA: params.LayerA, |
| 611 | LayerB: params.LayerB, |
| 612 | AddRewrites: addRewrites, |
| Akron | 49ceeb4 | 2025-05-23 17:46:01 +0200 | [diff] [blame] | 613 | }, jsonData) |
| 614 | |
| 615 | if err != nil { |
| 616 | log.Error().Err(err). |
| Akron | 49b525c | 2025-07-03 15:17:06 +0200 | [diff] [blame] | 617 | Str("mapID", params.MapID). |
| 618 | Str("direction", params.Dir). |
| Akron | 49ceeb4 | 2025-05-23 17:46:01 +0200 | [diff] [blame] | 619 | Msg("Failed to apply mappings") |
| 620 | |
| 621 | return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{ |
| 622 | "error": err.Error(), |
| 623 | }) |
| 624 | } |
| 625 | |
| 626 | return c.JSON(result) |
| 627 | } |
| 628 | } |
| Akron | 74e1c07 | 2025-05-26 14:38:25 +0200 | [diff] [blame] | 629 | |
| Akron | 8414ae5 | 2026-05-19 13:31:14 +0200 | [diff] [blame] | 630 | func handleResponseTransform(m *mapper.Mapper, lists []config.MappingList) fiber.Handler { |
| 631 | listsByID := make(map[string]*config.MappingList, len(lists)) |
| 632 | for i := range lists { |
| 633 | listsByID[lists[i].ID] = &lists[i] |
| 634 | } |
| 635 | |
| Akron | 4de47a9 | 2025-06-27 11:58:11 +0200 | [diff] [blame] | 636 | return func(c *fiber.Ctx) error { |
| Akron | 49b525c | 2025-07-03 15:17:06 +0200 | [diff] [blame] | 637 | // Extract and validate parameters |
| 638 | params, err := extractRequestParams(c) |
| 639 | if err != nil { |
| Akron | 4de47a9 | 2025-06-27 11:58:11 +0200 | [diff] [blame] | 640 | return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{ |
| 641 | "error": err.Error(), |
| 642 | }) |
| 643 | } |
| 644 | |
| Akron | 4de47a9 | 2025-06-27 11:58:11 +0200 | [diff] [blame] | 645 | // Parse request body |
| Akron | 49b525c | 2025-07-03 15:17:06 +0200 | [diff] [blame] | 646 | jsonData, direction, err := parseRequestBody(c, params.Dir) |
| Akron | 4de47a9 | 2025-06-27 11:58:11 +0200 | [diff] [blame] | 647 | if err != nil { |
| 648 | return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{ |
| 649 | "error": err.Error(), |
| 650 | }) |
| 651 | } |
| 652 | |
| Akron | 8414ae5 | 2026-05-19 13:31:14 +0200 | [diff] [blame] | 653 | // Determine rewrites: query param overrides YAML default |
| 654 | addRewrites := false |
| 655 | if list, ok := listsByID[params.MapID]; ok { |
| 656 | addRewrites = list.Rewrites |
| 657 | } |
| 658 | if params.Rewrites != nil { |
| 659 | addRewrites = *params.Rewrites |
| 660 | } |
| 661 | |
| Akron | 4de47a9 | 2025-06-27 11:58:11 +0200 | [diff] [blame] | 662 | // Apply response mappings |
| Akron | 49b525c | 2025-07-03 15:17:06 +0200 | [diff] [blame] | 663 | result, err := m.ApplyResponseMappings(params.MapID, mapper.MappingOptions{ |
| Akron | 8414ae5 | 2026-05-19 13:31:14 +0200 | [diff] [blame] | 664 | Direction: direction, |
| 665 | FoundryA: params.FoundryA, |
| 666 | FoundryB: params.FoundryB, |
| 667 | LayerA: params.LayerA, |
| 668 | LayerB: params.LayerB, |
| 669 | AddRewrites: addRewrites, |
| Akron | 4de47a9 | 2025-06-27 11:58:11 +0200 | [diff] [blame] | 670 | }, jsonData) |
| 671 | |
| 672 | if err != nil { |
| 673 | log.Error().Err(err). |
| Akron | 49b525c | 2025-07-03 15:17:06 +0200 | [diff] [blame] | 674 | Str("mapID", params.MapID). |
| 675 | Str("direction", params.Dir). |
| Akron | 4de47a9 | 2025-06-27 11:58:11 +0200 | [diff] [blame] | 676 | Msg("Failed to apply response mappings") |
| 677 | |
| 678 | return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{ |
| 679 | "error": err.Error(), |
| 680 | }) |
| 681 | } |
| 682 | |
| 683 | return c.JSON(result) |
| 684 | } |
| 685 | } |
| 686 | |
| Akron | 74e1c07 | 2025-05-26 14:38:25 +0200 | [diff] [blame] | 687 | // validateInput checks if the input parameters are valid |
| 688 | func validateInput(mapID, dir, foundryA, foundryB, layerA, layerB string, body []byte) error { |
| Akron | 69d43bf | 2025-05-26 17:09:00 +0200 | [diff] [blame] | 689 | // Define parameter checks |
| 690 | params := []struct { |
| Akron | 74e1c07 | 2025-05-26 14:38:25 +0200 | [diff] [blame] | 691 | name string |
| 692 | value string |
| 693 | }{ |
| 694 | {"mapID", mapID}, |
| 695 | {"dir", dir}, |
| 696 | {"foundryA", foundryA}, |
| 697 | {"foundryB", foundryB}, |
| 698 | {"layerA", layerA}, |
| 699 | {"layerB", layerB}, |
| Akron | 69d43bf | 2025-05-26 17:09:00 +0200 | [diff] [blame] | 700 | } |
| 701 | |
| 702 | for _, param := range params { |
| Akron | 49b525c | 2025-07-03 15:17:06 +0200 | [diff] [blame] | 703 | // Check input lengths and invalid characters in one combined condition |
| Akron | 69d43bf | 2025-05-26 17:09:00 +0200 | [diff] [blame] | 704 | if len(param.value) > maxParamLength { |
| 705 | return fmt.Errorf("%s too long (max %d bytes)", param.name, maxParamLength) |
| 706 | } |
| Akron | 74e1c07 | 2025-05-26 14:38:25 +0200 | [diff] [blame] | 707 | if strings.ContainsAny(param.value, "<>{}[]\\") { |
| 708 | return fmt.Errorf("%s contains invalid characters", param.name) |
| 709 | } |
| 710 | } |
| 711 | |
| Akron | 69d43bf | 2025-05-26 17:09:00 +0200 | [diff] [blame] | 712 | if len(body) > maxInputLength { |
| 713 | return fmt.Errorf("request body too large (max %d bytes)", maxInputLength) |
| 714 | } |
| 715 | |
| Akron | 74e1c07 | 2025-05-26 14:38:25 +0200 | [diff] [blame] | 716 | return nil |
| 717 | } |
| Akron | 40aaa63 | 2025-06-03 17:57:52 +0200 | [diff] [blame] | 718 | |
| Akron | d8a76b3 | 2026-02-20 09:31:56 +0100 | [diff] [blame] | 719 | func handleKalamarPlugin(yamlConfig *config.MappingConfig, configTmpl *template.Template, pluginTmpl *texttemplate.Template) fiber.Handler { |
| Akron | 40aaa63 | 2025-06-03 17:57:52 +0200 | [diff] [blame] | 720 | return func(c *fiber.Ctx) error { |
| Akron | c376dcc | 2025-06-04 17:00:18 +0200 | [diff] [blame] | 721 | mapID := c.Params("map") |
| 722 | |
| Akron | d8a76b3 | 2026-02-20 09:31:56 +0100 | [diff] [blame] | 723 | // Config page (GET /) |
| 724 | if mapID == "" { |
| 725 | data := buildConfigPageData(yamlConfig) |
| 726 | var buf bytes.Buffer |
| 727 | if err := configTmpl.Execute(&buf, data); err != nil { |
| 728 | log.Error().Err(err).Msg("Failed to execute config template") |
| 729 | return c.Status(fiber.StatusInternalServerError).SendString("internal error") |
| 730 | } |
| 731 | c.Set("Content-Type", "text/html") |
| 732 | return c.Send(buf.Bytes()) |
| 733 | } |
| 734 | |
| 735 | // Single-mapping page (GET /:map) — existing behavior |
| Akron | cb51f81 | 2025-06-30 15:24:20 +0200 | [diff] [blame] | 736 | // Get query parameters |
| 737 | dir := c.Query("dir", "atob") |
| 738 | foundryA := c.Query("foundryA", "") |
| 739 | foundryB := c.Query("foundryB", "") |
| 740 | layerA := c.Query("layerA", "") |
| 741 | layerB := c.Query("layerB", "") |
| 742 | |
| Akron | 49b525c | 2025-07-03 15:17:06 +0200 | [diff] [blame] | 743 | // Validate input parameters and direction in one step |
| Akron | cb51f81 | 2025-06-30 15:24:20 +0200 | [diff] [blame] | 744 | if err := validateInput(mapID, dir, foundryA, foundryB, layerA, layerB, []byte{}); err != nil { |
| 745 | return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{ |
| 746 | "error": err.Error(), |
| 747 | }) |
| 748 | } |
| 749 | |
| Akron | cb51f81 | 2025-06-30 15:24:20 +0200 | [diff] [blame] | 750 | if dir != "atob" && dir != "btoa" { |
| 751 | return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{ |
| 752 | "error": "invalid direction, must be 'atob' or 'btoa'", |
| 753 | }) |
| 754 | } |
| 755 | |
| Akron | cb51f81 | 2025-06-30 15:24:20 +0200 | [diff] [blame] | 756 | queryParams := QueryParams{ |
| 757 | Dir: dir, |
| 758 | FoundryA: foundryA, |
| 759 | FoundryB: foundryB, |
| 760 | LayerA: layerA, |
| 761 | LayerB: layerB, |
| 762 | } |
| 763 | |
| Akron | d8a76b3 | 2026-02-20 09:31:56 +0100 | [diff] [blame] | 764 | queryURL, err := buildMapServiceURL(yamlConfig.ServiceURL, mapID, "query", queryParams) |
| 765 | if err != nil { |
| 766 | log.Warn().Err(err).Msg("Failed to build query service URL") |
| 767 | return c.Status(fiber.StatusInternalServerError).SendString("internal error") |
| 768 | } |
| 769 | reversed := queryParams |
| 770 | if queryParams.Dir == "btoa" { |
| 771 | reversed.Dir = "atob" |
| 772 | } else { |
| 773 | reversed.Dir = "btoa" |
| 774 | } |
| 775 | responseURL, err := buildMapServiceURL(yamlConfig.ServiceURL, mapID, "response", reversed) |
| 776 | if err != nil { |
| 777 | log.Warn().Err(err).Msg("Failed to build response service URL") |
| 778 | return c.Status(fiber.StatusInternalServerError).SendString("internal error") |
| 779 | } |
| Akron | 40aaa63 | 2025-06-03 17:57:52 +0200 | [diff] [blame] | 780 | |
| Akron | d8a76b3 | 2026-02-20 09:31:56 +0100 | [diff] [blame] | 781 | data := SingleMappingPageData{ |
| 782 | BasePageData: buildBasePageData(yamlConfig), |
| 783 | MapID: mapID, |
| 784 | Mappings: yamlConfig.Lists, |
| 785 | QueryURL: queryURL, |
| 786 | ResponseURL: responseURL, |
| 787 | } |
| 788 | |
| 789 | var buf bytes.Buffer |
| 790 | if err := pluginTmpl.Execute(&buf, data); err != nil { |
| 791 | log.Error().Err(err).Msg("Failed to execute plugin template") |
| 792 | return c.Status(fiber.StatusInternalServerError).SendString("internal error") |
| 793 | } |
| Akron | 40aaa63 | 2025-06-03 17:57:52 +0200 | [diff] [blame] | 794 | c.Set("Content-Type", "text/html") |
| Akron | d8a76b3 | 2026-02-20 09:31:56 +0100 | [diff] [blame] | 795 | return c.Send(buf.Bytes()) |
| Akron | 40aaa63 | 2025-06-03 17:57:52 +0200 | [diff] [blame] | 796 | } |
| 797 | } |
| 798 | |
| Akron | d8a76b3 | 2026-02-20 09:31:56 +0100 | [diff] [blame] | 799 | func buildMapServiceURL(serviceURL, mapID, endpoint string, params QueryParams) (string, error) { |
| 800 | service, err := url.Parse(serviceURL) |
| 801 | if err != nil { |
| 802 | return "", err |
| Akron | c376dcc | 2025-06-04 17:00:18 +0200 | [diff] [blame] | 803 | } |
| Akron | d8a76b3 | 2026-02-20 09:31:56 +0100 | [diff] [blame] | 804 | service.Path = path.Join(service.Path, mapID, endpoint) |
| 805 | service.RawQuery = buildQueryParams(params.Dir, params.FoundryA, params.FoundryB, params.LayerA, params.LayerB) |
| 806 | return service.String(), nil |
| 807 | } |
| Akron | c376dcc | 2025-06-04 17:00:18 +0200 | [diff] [blame] | 808 | |
| Akron | d8a76b3 | 2026-02-20 09:31:56 +0100 | [diff] [blame] | 809 | func formatConsoleField(value string) string { |
| 810 | if strings.ContainsAny(value, " \t") { |
| 811 | return strconv.Quote(value) |
| Akron | 40aaa63 | 2025-06-03 17:57:52 +0200 | [diff] [blame] | 812 | } |
| Akron | d8a76b3 | 2026-02-20 09:31:56 +0100 | [diff] [blame] | 813 | return value |
| Akron | 40aaa63 | 2025-06-03 17:57:52 +0200 | [diff] [blame] | 814 | } |
| Akron | 14678dc | 2025-06-05 13:01:38 +0200 | [diff] [blame] | 815 | |
| Akron | cb51f81 | 2025-06-30 15:24:20 +0200 | [diff] [blame] | 816 | // buildQueryParams builds a query string from the provided parameters |
| 817 | func buildQueryParams(dir, foundryA, foundryB, layerA, layerB string) string { |
| 818 | params := url.Values{} |
| 819 | if dir != "" { |
| 820 | params.Add("dir", dir) |
| 821 | } |
| 822 | if foundryA != "" { |
| 823 | params.Add("foundryA", foundryA) |
| 824 | } |
| 825 | if foundryB != "" { |
| 826 | params.Add("foundryB", foundryB) |
| 827 | } |
| 828 | if layerA != "" { |
| 829 | params.Add("layerA", layerA) |
| 830 | } |
| 831 | if layerB != "" { |
| 832 | params.Add("layerB", layerB) |
| 833 | } |
| 834 | return params.Encode() |
| 835 | } |
| 836 | |
| Akron | 14678dc | 2025-06-05 13:01:38 +0200 | [diff] [blame] | 837 | // expandGlobs expands glob patterns in the slice of file paths |
| 838 | // Returns the expanded list of files or an error if glob expansion fails |
| 839 | func expandGlobs(patterns []string) ([]string, error) { |
| 840 | var expanded []string |
| 841 | |
| 842 | for _, pattern := range patterns { |
| 843 | // Use filepath.Glob which works cross-platform |
| 844 | matches, err := filepath.Glob(pattern) |
| 845 | if err != nil { |
| 846 | return nil, fmt.Errorf("failed to expand glob pattern '%s': %w", pattern, err) |
| 847 | } |
| 848 | |
| 849 | // If no matches found, treat as literal filename (consistent with shell behavior) |
| 850 | if len(matches) == 0 { |
| 851 | log.Warn().Str("pattern", pattern).Msg("Glob pattern matched no files, treating as literal filename") |
| 852 | expanded = append(expanded, pattern) |
| 853 | } else { |
| 854 | expanded = append(expanded, matches...) |
| 855 | } |
| 856 | } |
| 857 | |
| 858 | return expanded, nil |
| 859 | } |