blob: fcc3ad7778350c22e917e5d00bb6a837b7c925c3 [file] [log] [blame]
Akron49ceeb42025-05-23 17:46:01 +02001package main
2
3import (
Akron49ceeb42025-05-23 17:46:01 +02004 "fmt"
5 "os"
6 "os/signal"
Akron14678dc2025-06-05 13:01:38 +02007 "path/filepath"
Akron49ceeb42025-05-23 17:46:01 +02008 "strings"
9 "syscall"
10
Akrona00d4752025-05-26 17:34:36 +020011 "github.com/KorAP/KoralPipe-TermMapper/config"
Akronfa55bb22025-05-26 15:10:42 +020012 "github.com/KorAP/KoralPipe-TermMapper/mapper"
Akron1fc750e2025-05-26 16:54:18 +020013 "github.com/alecthomas/kong"
Akron49ceeb42025-05-23 17:46:01 +020014 "github.com/gofiber/fiber/v2"
15 "github.com/rs/zerolog"
16 "github.com/rs/zerolog/log"
17)
18
Akron74e1c072025-05-26 14:38:25 +020019const (
20 maxInputLength = 1024 * 1024 // 1MB
21 maxParamLength = 1024 // 1KB
22)
23
Akrona00d4752025-05-26 17:34:36 +020024type appConfig struct {
Akrona8a66ce2025-06-05 10:50:17 +020025 Port *int `kong:"short='p',help='Port to listen on'"`
Akrone1cff7c2025-06-04 18:43:32 +020026 Config string `kong:"short='c',help='YAML configuration file containing mapping directives and global settings'"`
Akron14678dc2025-06-05 13:01:38 +020027 Mappings []string `kong:"short='m',help='Individual YAML mapping files to load (supports glob patterns like dir/*.yaml)'"`
Akrona8a66ce2025-06-05 10:50:17 +020028 LogLevel *string `kong:"short='l',help='Log level (debug, info, warn, error)'"`
Akron49ceeb42025-05-23 17:46:01 +020029}
30
Akrondab27112025-06-05 13:52:43 +020031type TemplateMapping struct {
32 ID string
33 Description string
34}
35
Akron40aaa632025-06-03 17:57:52 +020036// TemplateData holds data for the Kalamar plugin template
37type TemplateData struct {
38 Title string
39 Version string
Akronfc77b5e2025-06-04 11:44:43 +020040 Hash string
41 Date string
Akron40aaa632025-06-03 17:57:52 +020042 Description string
Akron06d21f02025-06-04 14:36:07 +020043 Server string
44 SDK string
Akronc376dcc2025-06-04 17:00:18 +020045 MapID string
Akrondab27112025-06-05 13:52:43 +020046 Mappings []TemplateMapping
Akron40aaa632025-06-03 17:57:52 +020047}
48
Akrona00d4752025-05-26 17:34:36 +020049func parseConfig() *appConfig {
50 cfg := &appConfig{}
Akronfc77b5e2025-06-04 11:44:43 +020051
52 desc := config.Description
53 desc += " [" + config.Version + "]"
54
Akron1fc750e2025-05-26 16:54:18 +020055 ctx := kong.Parse(cfg,
Akronfc77b5e2025-06-04 11:44:43 +020056 kong.Description(desc),
Akron1fc750e2025-05-26 16:54:18 +020057 kong.UsageOnError(),
58 )
59 if ctx.Error != nil {
60 fmt.Fprintln(os.Stderr, ctx.Error)
Akron49ceeb42025-05-23 17:46:01 +020061 os.Exit(1)
62 }
Akron49ceeb42025-05-23 17:46:01 +020063 return cfg
64}
65
66func 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
79func main() {
80 // Parse command line flags
Akron1fc750e2025-05-26 16:54:18 +020081 cfg := parseConfig()
Akron49ceeb42025-05-23 17:46:01 +020082
Akrone1cff7c2025-06-04 18:43:32 +020083 // 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
Akron14678dc2025-06-05 13:01:38 +020088 // 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
Akrone1cff7c2025-06-04 18:43:32 +020094 // Load configuration from multiple sources
Akron14678dc2025-06-05 13:01:38 +020095 yamlConfig, err := config.LoadFromSources(cfg.Config, expandedMappings)
Akrona00d4752025-05-26 17:34:36 +020096 if err != nil {
97 log.Fatal().Err(err).Msg("Failed to load configuration")
98 }
99
Akrona8a66ce2025-06-05 10:50:17 +0200100 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
Akron49ceeb42025-05-23 17:46:01 +0200114 // Create a new mapper instance
Akrona00d4752025-05-26 17:34:36 +0200115 m, err := mapper.NewMapper(yamlConfig.Lists)
Akron49ceeb42025-05-23 17:46:01 +0200116 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,
Akron74e1c072025-05-26 14:38:25 +0200123 BodyLimit: maxInputLength,
Akron49ceeb42025-05-23 17:46:01 +0200124 })
125
126 // Set up routes
Akron40aaa632025-06-03 17:57:52 +0200127 setupRoutes(app, m, yamlConfig)
Akron49ceeb42025-05-23 17:46:01 +0200128
129 // Start server
130 go func() {
Akrona8a66ce2025-06-05 10:50:17 +0200131 log.Info().Int("port", finalPort).Msg("Starting server")
132 if err := app.Listen(fmt.Sprintf(":%d", finalPort)); err != nil {
Akron49ceeb42025-05-23 17:46:01 +0200133 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
Akron06d21f02025-06-04 14:36:07 +0200149func setupRoutes(app *fiber.App, m *mapper.Mapper, yamlConfig *config.MappingConfig) {
Akron49ceeb42025-05-23 17:46:01 +0200150 // 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))
Akron40aaa632025-06-03 17:57:52 +0200157
158 // Kalamar plugin endpoint
Akronc471c0a2025-06-04 11:56:22 +0200159 app.Get("/", handleKalamarPlugin(yamlConfig))
Akronc376dcc2025-06-04 17:00:18 +0200160 app.Get("/:map", handleKalamarPlugin(yamlConfig))
Akron49ceeb42025-05-23 17:46:01 +0200161}
162
163func 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
Akron74e1c072025-05-26 14:38:25 +0200173 // 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
Akron49ceeb42025-05-23 17:46:01 +0200180 // 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
Akron2cbdab52025-05-23 17:57:10 +0200188 var jsonData any
Akron49ceeb42025-05-23 17:46:01 +0200189 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
Akrona1a183f2025-05-26 17:47:33 +0200195 // 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
Akron49ceeb42025-05-23 17:46:01 +0200203 // Apply mappings
Akron7b4984e2025-05-26 19:12:20 +0200204 result, err := m.ApplyQueryMappings(mapID, mapper.MappingOptions{
Akrona1a183f2025-05-26 17:47:33 +0200205 Direction: direction,
Akron49ceeb42025-05-23 17:46:01 +0200206 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}
Akron74e1c072025-05-26 14:38:25 +0200226
227// validateInput checks if the input parameters are valid
228func validateInput(mapID, dir, foundryA, foundryB, layerA, layerB string, body []byte) error {
Akron69d43bf2025-05-26 17:09:00 +0200229 // Define parameter checks
230 params := []struct {
Akron74e1c072025-05-26 14:38:25 +0200231 name string
232 value string
233 }{
234 {"mapID", mapID},
235 {"dir", dir},
236 {"foundryA", foundryA},
237 {"foundryB", foundryB},
238 {"layerA", layerA},
239 {"layerB", layerB},
Akron69d43bf2025-05-26 17:09:00 +0200240 }
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
Akron74e1c072025-05-26 14:38:25 +0200248 if strings.ContainsAny(param.value, "<>{}[]\\") {
249 return fmt.Errorf("%s contains invalid characters", param.name)
250 }
251 }
252
Akron69d43bf2025-05-26 17:09:00 +0200253 if len(body) > maxInputLength {
254 return fmt.Errorf("request body too large (max %d bytes)", maxInputLength)
255 }
256
Akron74e1c072025-05-26 14:38:25 +0200257 return nil
258}
Akron40aaa632025-06-03 17:57:52 +0200259
Akron06d21f02025-06-04 14:36:07 +0200260func handleKalamarPlugin(yamlConfig *config.MappingConfig) fiber.Handler {
Akron40aaa632025-06-03 17:57:52 +0200261 return func(c *fiber.Ctx) error {
Akronc376dcc2025-06-04 17:00:18 +0200262 mapID := c.Params("map")
263
Akrondab27112025-06-05 13:52:43 +0200264 // Get list of available mappings
265 var mappings []TemplateMapping
Akron40aaa632025-06-03 17:57:52 +0200266 for _, list := range yamlConfig.Lists {
Akrondab27112025-06-05 13:52:43 +0200267 mappings = append(mappings, TemplateMapping{
268 ID: list.ID,
269 Description: list.Description,
270 })
Akron40aaa632025-06-03 17:57:52 +0200271 }
272
Akron06d21f02025-06-04 14:36:07 +0200273 // Use values from config (defaults are already applied during parsing)
274 server := yamlConfig.Server
275 sdk := yamlConfig.SDK
276
Akron40aaa632025-06-03 17:57:52 +0200277 // Prepare template data
278 data := TemplateData{
Akronfc77b5e2025-06-04 11:44:43 +0200279 Title: config.Title,
280 Version: config.Version,
281 Hash: config.Buildhash,
282 Date: config.Buildtime,
283 Description: config.Description,
Akron06d21f02025-06-04 14:36:07 +0200284 Server: server,
285 SDK: sdk,
Akronc376dcc2025-06-04 17:00:18 +0200286 MapID: mapID,
Akrondab27112025-06-05 13:52:43 +0200287 Mappings: mappings,
Akron40aaa632025-06-03 17:57:52 +0200288 }
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
300func generateKalamarPluginHTML(data TemplateData) string {
301 html := `<!DOCTYPE html>
302<html lang="en">
303<head>
304 <meta charset="UTF-8">
Akron40aaa632025-06-03 17:57:52 +0200305 <title>` + data.Title + `</title>
Akron06d21f02025-06-04 14:36:07 +0200306 <script src="` + data.SDK + `"
307 data-server="` + data.Server + `"></script>
Akron40aaa632025-06-03 17:57:52 +0200308</head>
309<body>
310 <div class="container">
311 <h1>` + data.Title + `</h1>
Akronc376dcc2025-06-04 17:00:18 +0200312 <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>
Akronc471c0a2025-06-04 11:56:22 +0200319 <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>
Akron40aaa632025-06-03 17:57:52 +0200322
Akronc471c0a2025-06-04 11:56:22 +0200323 <h2>Available API Endpoints</h2>
324 <dl>
Akron40aaa632025-06-03 17:57:52 +0200325
Akronc376dcc2025-06-04 17:00:18 +0200326 <dt><tt><strong>GET</strong> /:map</tt></dt>
327 <dd><small>Kalamar integration</small></dd>
Akrone1cff7c2025-06-04 18:43:32 +0200328
329 <dt><tt><strong>POST</strong> /:map/query</tt></dt>
Akronc376dcc2025-06-04 17:00:18 +0200330 <dd><small>Transform JSON query objects using term mapping rules</small></dd>
331
Akronc471c0a2025-06-04 11:56:22 +0200332 </dl>
Akronc376dcc2025-06-04 17:00:18 +0200333
334 <h2>Available Term Mappings</h2>
Akrondab27112025-06-05 13:52:43 +0200335 <dl>`
Akron40aaa632025-06-03 17:57:52 +0200336
Akrondab27112025-06-05 13:52:43 +0200337 for _, m := range data.Mappings {
338 html += `<dt><tt>` + m.ID + `</tt></dt>`
339 html += `<dd>` + m.Description + `</dd>`
Akron40aaa632025-06-03 17:57:52 +0200340 }
341
342 html += `
Akrondab27112025-06-05 13:52:43 +0200343 </dl>`
Akron06d21f02025-06-04 14:36:07 +0200344
Akronc376dcc2025-06-04 17:00:18 +0200345 if data.MapID != "" {
346 html += ` <script>
Akron06d21f02025-06-04 14:36:07 +0200347 <!-- activates/deactivates Mapper. -->
348
349 let data = {
350 'action' : 'pipe',
Akronc376dcc2025-06-04 17:00:18 +0200351 'service' : 'https://korap.ids-mannheim.de/plugin/termmapper/` + data.MapID + `/query'
Akron06d21f02025-06-04 14:36:07 +0200352 };
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 };
Akronc376dcc2025-06-04 17:00:18 +0200367 </script>`
368 }
369
370 html += ` </body>
Akron40aaa632025-06-03 17:57:52 +0200371</html>`
372
373 return html
374}
Akron14678dc2025-06-05 13:01:38 +0200375
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
378func 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}