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