package run import ( "encoding/json" "errors" "fmt" "net/http" "net/url" "strconv" "time" "git.fjla.uk/fred.boniface/map-dots/imaging" "git.fjla.uk/fred.boniface/map-dots/log" ) func Server() { fmt.Println("Server Mode Not Implemented") envCheck() http.HandleFunc("/traccar/", handleTraccarRequest) http.HandleFunc("/help/", handleHelpRequest) serverAddr := "localhost:8198" // Set your desired server address fmt.Printf("Starting server on http://%s\n", serverAddr) log.Msg.Info("Starting server on http://" + serverAddr) err := http.ListenAndServe(serverAddr, nil) if err != nil { fmt.Printf("Error starting server: %s\n", err) log.Msg.Error("Server failed to start:" + err.Error()) } } func handleTraccarRequest(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json") queryValues := r.URL.Query() id, from, to, height, width, style, format, err := validateAndProcessParams(queryValues) if err != nil { http.Error(w, err.Error(), http.StatusBadRequest) return } fmt.Println(id, from, to) var nullArry []string var _ = imaging.Generate(height, width, style, format, nullArry) message := map[string]string{ "status": "success", "message": "Hello from map-dots", } // Marshal the JSON data jsonData, err := json.Marshal(message) if err != nil { http.Error(w, "JSON encoding error", http.StatusInternalServerError) return } // Write the JSON response w.Write(jsonData) } func validateAndProcessParams(queryValues url.Values) (string, string, string, int, int, string, string, error) { // Validate and process individual parameters id := queryValues.Get("id") if id == "" { return "", "", "", 0, 0, "", "", errors.New("missing required parameter 'id'") } from := queryValues.Get("from") to := queryValues.Get("to") heightStr := queryValues.Get("height") widthStr := queryValues.Get("width") style := queryValues.Get("style") format := queryValues.Get("format") // Apply defaults if parameters are not specified if from == "" { thirtyDaysAgo := time.Now().AddDate(0, 0, -30) from = thirtyDaysAgo.UTC().Format(time.RFC3339) } if to == "" { to = time.Now().UTC().Format(time.RFC3339) } if heightStr == "" { heightStr = "1080" } if widthStr == "" { widthStr = "1920" } if style == "" { style = "circle" } if format == "" { format = "png" } // VALIDATE HEIGHT/WIDTH // Convert height and width to integers height, errHeight := strconv.Atoi(heightStr) width, errWidth := strconv.Atoi(widthStr) if errHeight != nil || errWidth != nil { return "", "", "", 0, 0, "", "", errors.New("invalid height or width") } if height >= 7680 { return "", "", "", 0, 0, "", "", errors.New("invalid height, max: 7680") } if width >= 4320 { return "", "", "", 0, 0, "", "", errors.New("invalid width, max: 4320") } // VALIDATE FROM/TO // Parse the ISO date strings to time.Time objects fromTime, errFrom := time.Parse(time.RFC3339, from) toTime, errTo := time.Parse(time.RFC3339, to) if errFrom != nil || errTo != nil { return "", "", "", 0, 0, "", "", errors.New("invalid date format") } // Define the maximum allowable time duration (e.g., 90 days) maxAllowableDuration := time.Hour * 24 * 90 // Calculate the duration between fromTime and toTime duration := toTime.Sub(fromTime) if duration > maxAllowableDuration { return "", "", "", 0, 0, "", "", errors.New("date range is too wide, max: 90d") } // ... Validate other parameters as needed return id, from, to, height, width, style, format, nil } func handleHelpRequest(w http.ResponseWriter, r *http.Request) { helpText := ` API Usage Information: Endpoint: /traccar/:id Parameters: - id: Traccar device ID - from: Start date in ISO format (90-days or less after 'to') - to: End date in ISO format - height: Output image height (1-7680) - width: Output image width (1-4320) - style: Output image style (circles) - format: Output image format (png, jpeg, gif, bmp, tiff, webp) Example: /traccar/?id=1&from=2023-01-01T00:00:00Z&to=2023-02-01T00:00:00Z&height=600&width=800&style=circle&format=png ` w.Header().Set("Content-Type", "text/plain") fmt.Fprint(w, helpText) }