Akron | 49ceeb4 | 2025-05-23 17:46:01 +0200 | [diff] [blame] | 1 | package main |
| 2 | |
| 3 | import ( |
Akron | 49ceeb4 | 2025-05-23 17:46:01 +0200 | [diff] [blame] | 4 | "fmt" |
| 5 | "os" |
| 6 | "os/signal" |
Akron | 14678dc | 2025-06-05 13:01:38 +0200 | [diff] [blame] | 7 | "path/filepath" |
Akron | 49ceeb4 | 2025-05-23 17:46:01 +0200 | [diff] [blame] | 8 | "strings" |
| 9 | "syscall" |
| 10 | |
Akron | a00d475 | 2025-05-26 17:34:36 +0200 | [diff] [blame] | 11 | "github.com/KorAP/KoralPipe-TermMapper/config" |
Akron | fa55bb2 | 2025-05-26 15:10:42 +0200 | [diff] [blame] | 12 | "github.com/KorAP/KoralPipe-TermMapper/mapper" |
Akron | 1fc750e | 2025-05-26 16:54:18 +0200 | [diff] [blame] | 13 | "github.com/alecthomas/kong" |
Akron | 49ceeb4 | 2025-05-23 17:46:01 +0200 | [diff] [blame] | 14 | "github.com/gofiber/fiber/v2" |
| 15 | "github.com/rs/zerolog" |
| 16 | "github.com/rs/zerolog/log" |
| 17 | ) |
| 18 | |
Akron | 74e1c07 | 2025-05-26 14:38:25 +0200 | [diff] [blame] | 19 | const ( |
| 20 | maxInputLength = 1024 * 1024 // 1MB |
| 21 | maxParamLength = 1024 // 1KB |
| 22 | ) |
| 23 | |
Akron | a00d475 | 2025-05-26 17:34:36 +0200 | [diff] [blame] | 24 | type appConfig struct { |
Akron | a8a66ce | 2025-06-05 10:50:17 +0200 | [diff] [blame] | 25 | Port *int `kong:"short='p',help='Port to listen on'"` |
Akron | e1cff7c | 2025-06-04 18:43:32 +0200 | [diff] [blame] | 26 | 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] | 27 | 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] | 28 | LogLevel *string `kong:"short='l',help='Log level (debug, info, warn, error)'"` |
Akron | 49ceeb4 | 2025-05-23 17:46:01 +0200 | [diff] [blame] | 29 | } |
| 30 | |
Akron | dab2711 | 2025-06-05 13:52:43 +0200 | [diff] [blame^] | 31 | type TemplateMapping struct { |
| 32 | ID string |
| 33 | Description string |
| 34 | } |
| 35 | |
Akron | 40aaa63 | 2025-06-03 17:57:52 +0200 | [diff] [blame] | 36 | // TemplateData holds data for the Kalamar plugin template |
| 37 | type TemplateData struct { |
| 38 | Title string |
| 39 | Version string |
Akron | fc77b5e | 2025-06-04 11:44:43 +0200 | [diff] [blame] | 40 | Hash string |
| 41 | Date string |
Akron | 40aaa63 | 2025-06-03 17:57:52 +0200 | [diff] [blame] | 42 | Description string |
Akron | 06d21f0 | 2025-06-04 14:36:07 +0200 | [diff] [blame] | 43 | Server string |
| 44 | SDK string |
Akron | c376dcc | 2025-06-04 17:00:18 +0200 | [diff] [blame] | 45 | MapID string |
Akron | dab2711 | 2025-06-05 13:52:43 +0200 | [diff] [blame^] | 46 | Mappings []TemplateMapping |
Akron | 40aaa63 | 2025-06-03 17:57:52 +0200 | [diff] [blame] | 47 | } |
| 48 | |
Akron | a00d475 | 2025-05-26 17:34:36 +0200 | [diff] [blame] | 49 | func parseConfig() *appConfig { |
| 50 | cfg := &appConfig{} |
Akron | fc77b5e | 2025-06-04 11:44:43 +0200 | [diff] [blame] | 51 | |
| 52 | desc := config.Description |
| 53 | desc += " [" + config.Version + "]" |
| 54 | |
Akron | 1fc750e | 2025-05-26 16:54:18 +0200 | [diff] [blame] | 55 | ctx := kong.Parse(cfg, |
Akron | fc77b5e | 2025-06-04 11:44:43 +0200 | [diff] [blame] | 56 | kong.Description(desc), |
Akron | 1fc750e | 2025-05-26 16:54:18 +0200 | [diff] [blame] | 57 | kong.UsageOnError(), |
| 58 | ) |
| 59 | if ctx.Error != nil { |
| 60 | fmt.Fprintln(os.Stderr, ctx.Error) |
Akron | 49ceeb4 | 2025-05-23 17:46:01 +0200 | [diff] [blame] | 61 | os.Exit(1) |
| 62 | } |
Akron | 49ceeb4 | 2025-05-23 17:46:01 +0200 | [diff] [blame] | 63 | return cfg |
| 64 | } |
| 65 | |
| 66 | func setupLogger(level string) { |
| 67 | // Parse log level |
| 68 | lvl, err := zerolog.ParseLevel(strings.ToLower(level)) |
| 69 | if err != nil { |
| 70 | log.Error().Err(err).Str("level", level).Msg("Invalid log level, defaulting to info") |
| 71 | lvl = zerolog.InfoLevel |
| 72 | } |
| 73 | |
| 74 | // Configure zerolog |
| 75 | zerolog.SetGlobalLevel(lvl) |
| 76 | log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr}) |
| 77 | } |
| 78 | |
| 79 | func main() { |
| 80 | // Parse command line flags |
Akron | 1fc750e | 2025-05-26 16:54:18 +0200 | [diff] [blame] | 81 | cfg := parseConfig() |
Akron | 49ceeb4 | 2025-05-23 17:46:01 +0200 | [diff] [blame] | 82 | |
Akron | e1cff7c | 2025-06-04 18:43:32 +0200 | [diff] [blame] | 83 | // Validate command line arguments |
| 84 | if cfg.Config == "" && len(cfg.Mappings) == 0 { |
| 85 | log.Fatal().Msg("At least one configuration source must be provided: use -c for main config file or -m for mapping files") |
| 86 | } |
| 87 | |
Akron | 14678dc | 2025-06-05 13:01:38 +0200 | [diff] [blame] | 88 | // Expand glob patterns in mapping files |
| 89 | expandedMappings, err := expandGlobs(cfg.Mappings) |
| 90 | if err != nil { |
| 91 | log.Fatal().Err(err).Msg("Failed to expand glob patterns in mapping files") |
| 92 | } |
| 93 | |
Akron | e1cff7c | 2025-06-04 18:43:32 +0200 | [diff] [blame] | 94 | // Load configuration from multiple sources |
Akron | 14678dc | 2025-06-05 13:01:38 +0200 | [diff] [blame] | 95 | yamlConfig, err := config.LoadFromSources(cfg.Config, expandedMappings) |
Akron | a00d475 | 2025-05-26 17:34:36 +0200 | [diff] [blame] | 96 | if err != nil { |
| 97 | log.Fatal().Err(err).Msg("Failed to load configuration") |
| 98 | } |
| 99 | |
Akron | a8a66ce | 2025-06-05 10:50:17 +0200 | [diff] [blame] | 100 | finalPort := yamlConfig.Port |
| 101 | finalLogLevel := yamlConfig.LogLevel |
| 102 | |
| 103 | // Use command line values if provided (they override config file) |
| 104 | if cfg.Port != nil { |
| 105 | finalPort = *cfg.Port |
| 106 | } |
| 107 | if cfg.LogLevel != nil { |
| 108 | finalLogLevel = *cfg.LogLevel |
| 109 | } |
| 110 | |
| 111 | // Set up logging with the final log level |
| 112 | setupLogger(finalLogLevel) |
| 113 | |
Akron | 49ceeb4 | 2025-05-23 17:46:01 +0200 | [diff] [blame] | 114 | // Create a new mapper instance |
Akron | a00d475 | 2025-05-26 17:34:36 +0200 | [diff] [blame] | 115 | m, err := mapper.NewMapper(yamlConfig.Lists) |
Akron | 49ceeb4 | 2025-05-23 17:46:01 +0200 | [diff] [blame] | 116 | if err != nil { |
| 117 | log.Fatal().Err(err).Msg("Failed to create mapper") |
| 118 | } |
| 119 | |
| 120 | // Create fiber app |
| 121 | app := fiber.New(fiber.Config{ |
| 122 | DisableStartupMessage: true, |
Akron | 74e1c07 | 2025-05-26 14:38:25 +0200 | [diff] [blame] | 123 | BodyLimit: maxInputLength, |
Akron | 49ceeb4 | 2025-05-23 17:46:01 +0200 | [diff] [blame] | 124 | }) |
| 125 | |
| 126 | // Set up routes |
Akron | 40aaa63 | 2025-06-03 17:57:52 +0200 | [diff] [blame] | 127 | setupRoutes(app, m, yamlConfig) |
Akron | 49ceeb4 | 2025-05-23 17:46:01 +0200 | [diff] [blame] | 128 | |
| 129 | // Start server |
| 130 | go func() { |
Akron | a8a66ce | 2025-06-05 10:50:17 +0200 | [diff] [blame] | 131 | log.Info().Int("port", finalPort).Msg("Starting server") |
| 132 | if err := app.Listen(fmt.Sprintf(":%d", finalPort)); err != nil { |
Akron | 49ceeb4 | 2025-05-23 17:46:01 +0200 | [diff] [blame] | 133 | log.Fatal().Err(err).Msg("Server error") |
| 134 | } |
| 135 | }() |
| 136 | |
| 137 | // Wait for interrupt signal |
| 138 | sigChan := make(chan os.Signal, 1) |
| 139 | signal.Notify(sigChan, os.Interrupt, syscall.SIGTERM) |
| 140 | <-sigChan |
| 141 | |
| 142 | // Graceful shutdown |
| 143 | log.Info().Msg("Shutting down server") |
| 144 | if err := app.Shutdown(); err != nil { |
| 145 | log.Error().Err(err).Msg("Error during shutdown") |
| 146 | } |
| 147 | } |
| 148 | |
Akron | 06d21f0 | 2025-06-04 14:36:07 +0200 | [diff] [blame] | 149 | func setupRoutes(app *fiber.App, m *mapper.Mapper, yamlConfig *config.MappingConfig) { |
Akron | 49ceeb4 | 2025-05-23 17:46:01 +0200 | [diff] [blame] | 150 | // Health check endpoint |
| 151 | app.Get("/health", func(c *fiber.Ctx) error { |
| 152 | return c.SendString("OK") |
| 153 | }) |
| 154 | |
| 155 | // Transformation endpoint |
| 156 | app.Post("/:map/query", handleTransform(m)) |
Akron | 40aaa63 | 2025-06-03 17:57:52 +0200 | [diff] [blame] | 157 | |
| 158 | // Kalamar plugin endpoint |
Akron | c471c0a | 2025-06-04 11:56:22 +0200 | [diff] [blame] | 159 | app.Get("/", handleKalamarPlugin(yamlConfig)) |
Akron | c376dcc | 2025-06-04 17:00:18 +0200 | [diff] [blame] | 160 | app.Get("/:map", handleKalamarPlugin(yamlConfig)) |
Akron | 49ceeb4 | 2025-05-23 17:46:01 +0200 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | func handleTransform(m *mapper.Mapper) fiber.Handler { |
| 164 | return func(c *fiber.Ctx) error { |
| 165 | // Get parameters |
| 166 | mapID := c.Params("map") |
| 167 | dir := c.Query("dir", "atob") |
| 168 | foundryA := c.Query("foundryA", "") |
| 169 | foundryB := c.Query("foundryB", "") |
| 170 | layerA := c.Query("layerA", "") |
| 171 | layerB := c.Query("layerB", "") |
| 172 | |
Akron | 74e1c07 | 2025-05-26 14:38:25 +0200 | [diff] [blame] | 173 | // Validate input parameters |
| 174 | if err := validateInput(mapID, dir, foundryA, foundryB, layerA, layerB, c.Body()); err != nil { |
| 175 | return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{ |
| 176 | "error": err.Error(), |
| 177 | }) |
| 178 | } |
| 179 | |
Akron | 49ceeb4 | 2025-05-23 17:46:01 +0200 | [diff] [blame] | 180 | // Validate direction |
| 181 | if dir != "atob" && dir != "btoa" { |
| 182 | return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{ |
| 183 | "error": "invalid direction, must be 'atob' or 'btoa'", |
| 184 | }) |
| 185 | } |
| 186 | |
| 187 | // Parse request body |
Akron | 2cbdab5 | 2025-05-23 17:57:10 +0200 | [diff] [blame] | 188 | var jsonData any |
Akron | 49ceeb4 | 2025-05-23 17:46:01 +0200 | [diff] [blame] | 189 | if err := c.BodyParser(&jsonData); err != nil { |
| 190 | return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{ |
| 191 | "error": "invalid JSON in request body", |
| 192 | }) |
| 193 | } |
| 194 | |
Akron | a1a183f | 2025-05-26 17:47:33 +0200 | [diff] [blame] | 195 | // Parse direction |
| 196 | direction, err := mapper.ParseDirection(dir) |
| 197 | if err != nil { |
| 198 | return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{ |
| 199 | "error": err.Error(), |
| 200 | }) |
| 201 | } |
| 202 | |
Akron | 49ceeb4 | 2025-05-23 17:46:01 +0200 | [diff] [blame] | 203 | // Apply mappings |
Akron | 7b4984e | 2025-05-26 19:12:20 +0200 | [diff] [blame] | 204 | result, err := m.ApplyQueryMappings(mapID, mapper.MappingOptions{ |
Akron | a1a183f | 2025-05-26 17:47:33 +0200 | [diff] [blame] | 205 | Direction: direction, |
Akron | 49ceeb4 | 2025-05-23 17:46:01 +0200 | [diff] [blame] | 206 | FoundryA: foundryA, |
| 207 | FoundryB: foundryB, |
| 208 | LayerA: layerA, |
| 209 | LayerB: layerB, |
| 210 | }, jsonData) |
| 211 | |
| 212 | if err != nil { |
| 213 | log.Error().Err(err). |
| 214 | Str("mapID", mapID). |
| 215 | Str("direction", dir). |
| 216 | Msg("Failed to apply mappings") |
| 217 | |
| 218 | return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{ |
| 219 | "error": err.Error(), |
| 220 | }) |
| 221 | } |
| 222 | |
| 223 | return c.JSON(result) |
| 224 | } |
| 225 | } |
Akron | 74e1c07 | 2025-05-26 14:38:25 +0200 | [diff] [blame] | 226 | |
| 227 | // validateInput checks if the input parameters are valid |
| 228 | func validateInput(mapID, dir, foundryA, foundryB, layerA, layerB string, body []byte) error { |
Akron | 69d43bf | 2025-05-26 17:09:00 +0200 | [diff] [blame] | 229 | // Define parameter checks |
| 230 | params := []struct { |
Akron | 74e1c07 | 2025-05-26 14:38:25 +0200 | [diff] [blame] | 231 | name string |
| 232 | value string |
| 233 | }{ |
| 234 | {"mapID", mapID}, |
| 235 | {"dir", dir}, |
| 236 | {"foundryA", foundryA}, |
| 237 | {"foundryB", foundryB}, |
| 238 | {"layerA", layerA}, |
| 239 | {"layerB", layerB}, |
Akron | 69d43bf | 2025-05-26 17:09:00 +0200 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | for _, param := range params { |
| 243 | // Check input lengths |
| 244 | if len(param.value) > maxParamLength { |
| 245 | return fmt.Errorf("%s too long (max %d bytes)", param.name, maxParamLength) |
| 246 | } |
| 247 | // Check for invalid characters in parameters |
Akron | 74e1c07 | 2025-05-26 14:38:25 +0200 | [diff] [blame] | 248 | if strings.ContainsAny(param.value, "<>{}[]\\") { |
| 249 | return fmt.Errorf("%s contains invalid characters", param.name) |
| 250 | } |
| 251 | } |
| 252 | |
Akron | 69d43bf | 2025-05-26 17:09:00 +0200 | [diff] [blame] | 253 | if len(body) > maxInputLength { |
| 254 | return fmt.Errorf("request body too large (max %d bytes)", maxInputLength) |
| 255 | } |
| 256 | |
Akron | 74e1c07 | 2025-05-26 14:38:25 +0200 | [diff] [blame] | 257 | return nil |
| 258 | } |
Akron | 40aaa63 | 2025-06-03 17:57:52 +0200 | [diff] [blame] | 259 | |
Akron | 06d21f0 | 2025-06-04 14:36:07 +0200 | [diff] [blame] | 260 | func handleKalamarPlugin(yamlConfig *config.MappingConfig) fiber.Handler { |
Akron | 40aaa63 | 2025-06-03 17:57:52 +0200 | [diff] [blame] | 261 | return func(c *fiber.Ctx) error { |
Akron | c376dcc | 2025-06-04 17:00:18 +0200 | [diff] [blame] | 262 | mapID := c.Params("map") |
| 263 | |
Akron | dab2711 | 2025-06-05 13:52:43 +0200 | [diff] [blame^] | 264 | // Get list of available mappings |
| 265 | var mappings []TemplateMapping |
Akron | 40aaa63 | 2025-06-03 17:57:52 +0200 | [diff] [blame] | 266 | for _, list := range yamlConfig.Lists { |
Akron | dab2711 | 2025-06-05 13:52:43 +0200 | [diff] [blame^] | 267 | mappings = append(mappings, TemplateMapping{ |
| 268 | ID: list.ID, |
| 269 | Description: list.Description, |
| 270 | }) |
Akron | 40aaa63 | 2025-06-03 17:57:52 +0200 | [diff] [blame] | 271 | } |
| 272 | |
Akron | 06d21f0 | 2025-06-04 14:36:07 +0200 | [diff] [blame] | 273 | // Use values from config (defaults are already applied during parsing) |
| 274 | server := yamlConfig.Server |
| 275 | sdk := yamlConfig.SDK |
| 276 | |
Akron | 40aaa63 | 2025-06-03 17:57:52 +0200 | [diff] [blame] | 277 | // Prepare template data |
| 278 | data := TemplateData{ |
Akron | fc77b5e | 2025-06-04 11:44:43 +0200 | [diff] [blame] | 279 | Title: config.Title, |
| 280 | Version: config.Version, |
| 281 | Hash: config.Buildhash, |
| 282 | Date: config.Buildtime, |
| 283 | Description: config.Description, |
Akron | 06d21f0 | 2025-06-04 14:36:07 +0200 | [diff] [blame] | 284 | Server: server, |
| 285 | SDK: sdk, |
Akron | c376dcc | 2025-06-04 17:00:18 +0200 | [diff] [blame] | 286 | MapID: mapID, |
Akron | dab2711 | 2025-06-05 13:52:43 +0200 | [diff] [blame^] | 287 | Mappings: mappings, |
Akron | 40aaa63 | 2025-06-03 17:57:52 +0200 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | // Generate HTML |
| 291 | html := generateKalamarPluginHTML(data) |
| 292 | |
| 293 | c.Set("Content-Type", "text/html") |
| 294 | return c.SendString(html) |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | // generateKalamarPluginHTML creates the HTML template for the Kalamar plugin page |
| 299 | // This function can be easily modified to change the appearance and content |
| 300 | func generateKalamarPluginHTML(data TemplateData) string { |
| 301 | html := `<!DOCTYPE html> |
| 302 | <html lang="en"> |
| 303 | <head> |
| 304 | <meta charset="UTF-8"> |
Akron | 40aaa63 | 2025-06-03 17:57:52 +0200 | [diff] [blame] | 305 | <title>` + data.Title + `</title> |
Akron | 06d21f0 | 2025-06-04 14:36:07 +0200 | [diff] [blame] | 306 | <script src="` + data.SDK + `" |
| 307 | data-server="` + data.Server + `"></script> |
Akron | 40aaa63 | 2025-06-03 17:57:52 +0200 | [diff] [blame] | 308 | </head> |
| 309 | <body> |
| 310 | <div class="container"> |
| 311 | <h1>` + data.Title + `</h1> |
Akron | c376dcc | 2025-06-04 17:00:18 +0200 | [diff] [blame] | 312 | <p>` + data.Description + `</p>` |
| 313 | |
| 314 | if data.MapID != "" { |
| 315 | html += `<p>Map ID: ` + data.MapID + `</p>` |
| 316 | } |
| 317 | |
| 318 | html += ` <h2>Plugin Information</h2> |
Akron | c471c0a | 2025-06-04 11:56:22 +0200 | [diff] [blame] | 319 | <p><strong>Version:</strong> <tt>` + data.Version + `</tt></p> |
| 320 | <p><strong>Build Date:</strong> <tt>` + data.Date + `</tt></p> |
| 321 | <p><strong>Build Hash:</strong> <tt>` + data.Hash + `</tt></p> |
Akron | 40aaa63 | 2025-06-03 17:57:52 +0200 | [diff] [blame] | 322 | |
Akron | c471c0a | 2025-06-04 11:56:22 +0200 | [diff] [blame] | 323 | <h2>Available API Endpoints</h2> |
| 324 | <dl> |
Akron | 40aaa63 | 2025-06-03 17:57:52 +0200 | [diff] [blame] | 325 | |
Akron | c376dcc | 2025-06-04 17:00:18 +0200 | [diff] [blame] | 326 | <dt><tt><strong>GET</strong> /:map</tt></dt> |
| 327 | <dd><small>Kalamar integration</small></dd> |
Akron | e1cff7c | 2025-06-04 18:43:32 +0200 | [diff] [blame] | 328 | |
| 329 | <dt><tt><strong>POST</strong> /:map/query</tt></dt> |
Akron | c376dcc | 2025-06-04 17:00:18 +0200 | [diff] [blame] | 330 | <dd><small>Transform JSON query objects using term mapping rules</small></dd> |
| 331 | |
Akron | c471c0a | 2025-06-04 11:56:22 +0200 | [diff] [blame] | 332 | </dl> |
Akron | c376dcc | 2025-06-04 17:00:18 +0200 | [diff] [blame] | 333 | |
| 334 | <h2>Available Term Mappings</h2> |
Akron | dab2711 | 2025-06-05 13:52:43 +0200 | [diff] [blame^] | 335 | <dl>` |
Akron | 40aaa63 | 2025-06-03 17:57:52 +0200 | [diff] [blame] | 336 | |
Akron | dab2711 | 2025-06-05 13:52:43 +0200 | [diff] [blame^] | 337 | for _, m := range data.Mappings { |
| 338 | html += `<dt><tt>` + m.ID + `</tt></dt>` |
| 339 | html += `<dd>` + m.Description + `</dd>` |
Akron | 40aaa63 | 2025-06-03 17:57:52 +0200 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | html += ` |
Akron | dab2711 | 2025-06-05 13:52:43 +0200 | [diff] [blame^] | 343 | </dl>` |
Akron | 06d21f0 | 2025-06-04 14:36:07 +0200 | [diff] [blame] | 344 | |
Akron | c376dcc | 2025-06-04 17:00:18 +0200 | [diff] [blame] | 345 | if data.MapID != "" { |
| 346 | html += ` <script> |
Akron | 06d21f0 | 2025-06-04 14:36:07 +0200 | [diff] [blame] | 347 | <!-- activates/deactivates Mapper. --> |
| 348 | |
| 349 | let data = { |
| 350 | 'action' : 'pipe', |
Akron | c376dcc | 2025-06-04 17:00:18 +0200 | [diff] [blame] | 351 | 'service' : 'https://korap.ids-mannheim.de/plugin/termmapper/` + data.MapID + `/query' |
Akron | 06d21f0 | 2025-06-04 14:36:07 +0200 | [diff] [blame] | 352 | }; |
| 353 | |
| 354 | function pluginit (p) { |
| 355 | p.onMessage = function(msg) { |
| 356 | if (msg.key == 'termmapper') { |
| 357 | if (msg.value) { |
| 358 | data['job'] = 'add'; |
| 359 | } |
| 360 | else { |
| 361 | data['job'] = 'del'; |
| 362 | }; |
| 363 | KorAPlugin.sendMsg(data); |
| 364 | }; |
| 365 | }; |
| 366 | }; |
Akron | c376dcc | 2025-06-04 17:00:18 +0200 | [diff] [blame] | 367 | </script>` |
| 368 | } |
| 369 | |
| 370 | html += ` </body> |
Akron | 40aaa63 | 2025-06-03 17:57:52 +0200 | [diff] [blame] | 371 | </html>` |
| 372 | |
| 373 | return html |
| 374 | } |
Akron | 14678dc | 2025-06-05 13:01:38 +0200 | [diff] [blame] | 375 | |
| 376 | // expandGlobs expands glob patterns in the slice of file paths |
| 377 | // Returns the expanded list of files or an error if glob expansion fails |
| 378 | func expandGlobs(patterns []string) ([]string, error) { |
| 379 | var expanded []string |
| 380 | |
| 381 | for _, pattern := range patterns { |
| 382 | // Use filepath.Glob which works cross-platform |
| 383 | matches, err := filepath.Glob(pattern) |
| 384 | if err != nil { |
| 385 | return nil, fmt.Errorf("failed to expand glob pattern '%s': %w", pattern, err) |
| 386 | } |
| 387 | |
| 388 | // If no matches found, treat as literal filename (consistent with shell behavior) |
| 389 | if len(matches) == 0 { |
| 390 | log.Warn().Str("pattern", pattern).Msg("Glob pattern matched no files, treating as literal filename") |
| 391 | expanded = append(expanded, pattern) |
| 392 | } else { |
| 393 | expanded = append(expanded, matches...) |
| 394 | } |
| 395 | } |
| 396 | |
| 397 | return expanded, nil |
| 398 | } |