UPDATE: Add raster tiles

This commit is contained in:
2026-04-14 09:34:56 +07:00
parent 72efb480ac
commit 34c97e803a
8 changed files with 291 additions and 6 deletions

View File

@@ -0,0 +1,113 @@
package controllers
import (
"context"
"fmt"
"history-api/internal/dtos/response"
"history-api/internal/services"
"strconv"
"time"
"github.com/gofiber/fiber/v3"
)
type RasterTileController struct {
service services.RasterTileService
}
func NewRasterTileController(svc services.RasterTileService) *RasterTileController {
return &RasterTileController{service: svc}
}
// GetMetadata godoc
// @Summary Get raster tile metadata
// @Description Retrieve map metadata
// @Tags Tile
// @Accept json
// @Produce json
// @Success 200 {object} response.CommonResponse
// @Failure 500 {object} response.CommonResponse
// @Router /raster-tiles/metadata [get]
func (h *RasterTileController) GetMetadata(c fiber.Ctx) error {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
res, err := h.service.GetMetadata(ctx)
if err != nil {
return c.Status(fiber.StatusInternalServerError).JSON(response.CommonResponse{
Status: false,
Message: err.Error(),
})
}
return c.Status(fiber.StatusOK).JSON(response.CommonResponse{
Status: true,
Data: res,
})
}
// GetTile godoc
// @Summary Get a map raster tile
// @Description Fetch vector or raster map tile data by Z, X, Y coordinates
// @Tags Tile
// @Produce application/octet-stream
// @Param z path int true "Zoom level (0-22)"
// @Param x path int true "X coordinate"
// @Param y path int true "Y coordinate"
// @Success 200 {file} byte
// @Failure 400 {object} response.CommonResponse
// @Failure 500 {object} response.CommonResponse
// @Router /raster-tiles/{z}/{x}/{y} [get]
func (h *RasterTileController) GetTile(c fiber.Ctx) error {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
z, x, y, err := h.parseTileParams(c)
if err != nil {
return c.Status(fiber.StatusBadRequest).JSON(response.CommonResponse{
Status: false,
Message: err.Error(),
})
}
data, headers, err := h.service.GetTile(ctx, z, x, y)
if err != nil {
return c.Status(fiber.StatusInternalServerError).JSON(response.CommonResponse{
Status: false,
Message: err.Error(),
})
}
for k, v := range headers {
c.Set(k, v)
}
return c.Status(fiber.StatusOK).Send(data)
}
func (h *RasterTileController) parseTileParams(c fiber.Ctx) (int, int, int, error) {
z, err := strconv.Atoi(c.Params("z"))
if err != nil {
return 0, 0, 0, fmt.Errorf("invalid z")
}
x, err := strconv.Atoi(c.Params("x"))
if err != nil {
return 0, 0, 0, fmt.Errorf("invalid x")
}
y, err := strconv.Atoi(c.Params("y"))
if err != nil {
return 0, 0, 0, fmt.Errorf("invalid y")
}
if z < 0 || x < 0 || y < 0 {
return 0, 0, 0, fmt.Errorf("coordinates must be positive")
}
if z > 22 {
return 0, 0, 0, fmt.Errorf("zoom level too large")
}
return z, x, y, nil
}