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