map-dots/run/server.go

147 lines
3.8 KiB
Go
Raw Normal View History

2023-08-11 14:32:44 +01:00
package run
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"net/url"
"strconv"
"time"
)
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("Server listening on http://%s\n", serverAddr)
err := http.ListenAndServe(serverAddr, nil)
if err != nil {
fmt.Printf("Error starting server: %s\n", err)
}
}
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, height, width, style, format)
message := map[string]string{
"status": "success",
2023-08-11 14:38:49 +01:00
"message": "Hello from map-dots",
2023-08-11 14:32:44 +01:00
}
// 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 = "300"
}
if widthStr == "" {
widthStr = "400"
}
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)
}