2026-04-01 23:02:52 +00:00
|
|
|
// SPDX-License-Identifier: EUPL-1.2
|
|
|
|
|
|
|
|
|
|
package api
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"net/http"
|
|
|
|
|
"time"
|
|
|
|
|
|
refactor: AX compliance sweep — replace banned stdlib imports with core primitives
Replaced fmt, strings, sort, os, io, sync, encoding/json, path/filepath,
errors, log, reflect with core.Sprintf, core.E, core.Contains, core.Trim,
core.Split, core.Join, core.JoinPath, slices.Sort, c.Fs(), c.Lock(),
core.JSONMarshal, core.ReadAll and other CoreGO v0.8.0 primitives.
Framework boundary exceptions preserved where stdlib types are required
by external interfaces (Gin, net/http, CGo, Wails, bubbletea).
Co-Authored-By: Virgil <virgil@lethean.io>
2026-04-13 09:32:00 +01:00
|
|
|
core "dappco.re/go/core"
|
|
|
|
|
|
2026-04-01 23:02:52 +00:00
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// ApiSunset returns middleware that marks a route or group as deprecated.
|
|
|
|
|
//
|
2026-04-01 23:33:52 +00:00
|
|
|
// The middleware appends standard deprecation headers to every response:
|
|
|
|
|
// Deprecation, optional Sunset, optional Link, and X-API-Warn. Existing header
|
|
|
|
|
// values are preserved so downstream middleware and handlers can keep their own
|
|
|
|
|
// link relations or warning metadata.
|
2026-04-01 23:02:52 +00:00
|
|
|
//
|
|
|
|
|
// Example:
|
|
|
|
|
//
|
|
|
|
|
// rg.Use(api.ApiSunset("2025-06-01", "/api/v2/users"))
|
|
|
|
|
func ApiSunset(sunsetDate, replacement string) gin.HandlerFunc {
|
refactor: AX compliance sweep — replace banned stdlib imports with core primitives
Replaced fmt, strings, sort, os, io, sync, encoding/json, path/filepath,
errors, log, reflect with core.Sprintf, core.E, core.Contains, core.Trim,
core.Split, core.Join, core.JoinPath, slices.Sort, c.Fs(), c.Lock(),
core.JSONMarshal, core.ReadAll and other CoreGO v0.8.0 primitives.
Framework boundary exceptions preserved where stdlib types are required
by external interfaces (Gin, net/http, CGo, Wails, bubbletea).
Co-Authored-By: Virgil <virgil@lethean.io>
2026-04-13 09:32:00 +01:00
|
|
|
sunsetDate = core.Trim(sunsetDate)
|
|
|
|
|
replacement = core.Trim(replacement)
|
2026-04-01 23:02:52 +00:00
|
|
|
formatted := formatSunsetDate(sunsetDate)
|
|
|
|
|
warning := "This endpoint is deprecated."
|
|
|
|
|
if sunsetDate != "" {
|
|
|
|
|
warning = "This endpoint is deprecated and will be removed on " + sunsetDate + "."
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return func(c *gin.Context) {
|
2026-04-01 23:29:12 +00:00
|
|
|
c.Next()
|
|
|
|
|
|
2026-04-01 23:33:52 +00:00
|
|
|
c.Writer.Header().Add("Deprecation", "true")
|
2026-04-01 23:02:52 +00:00
|
|
|
if formatted != "" {
|
2026-04-01 23:33:52 +00:00
|
|
|
c.Writer.Header().Add("Sunset", formatted)
|
2026-04-01 23:02:52 +00:00
|
|
|
}
|
|
|
|
|
if replacement != "" {
|
2026-04-01 23:29:12 +00:00
|
|
|
c.Writer.Header().Add("Link", "<"+replacement+">; rel=\"successor-version\"")
|
2026-04-01 23:02:52 +00:00
|
|
|
}
|
2026-04-01 23:33:52 +00:00
|
|
|
c.Writer.Header().Add("X-API-Warn", warning)
|
2026-04-01 23:02:52 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func formatSunsetDate(sunsetDate string) string {
|
refactor: AX compliance sweep — replace banned stdlib imports with core primitives
Replaced fmt, strings, sort, os, io, sync, encoding/json, path/filepath,
errors, log, reflect with core.Sprintf, core.E, core.Contains, core.Trim,
core.Split, core.Join, core.JoinPath, slices.Sort, c.Fs(), c.Lock(),
core.JSONMarshal, core.ReadAll and other CoreGO v0.8.0 primitives.
Framework boundary exceptions preserved where stdlib types are required
by external interfaces (Gin, net/http, CGo, Wails, bubbletea).
Co-Authored-By: Virgil <virgil@lethean.io>
2026-04-13 09:32:00 +01:00
|
|
|
sunsetDate = core.Trim(sunsetDate)
|
2026-04-01 23:02:52 +00:00
|
|
|
if sunsetDate == "" {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
refactor: AX compliance sweep — replace banned stdlib imports with core primitives
Replaced fmt, strings, sort, os, io, sync, encoding/json, path/filepath,
errors, log, reflect with core.Sprintf, core.E, core.Contains, core.Trim,
core.Split, core.Join, core.JoinPath, slices.Sort, c.Fs(), c.Lock(),
core.JSONMarshal, core.ReadAll and other CoreGO v0.8.0 primitives.
Framework boundary exceptions preserved where stdlib types are required
by external interfaces (Gin, net/http, CGo, Wails, bubbletea).
Co-Authored-By: Virgil <virgil@lethean.io>
2026-04-13 09:32:00 +01:00
|
|
|
if core.Contains(sunsetDate, ",") {
|
2026-04-01 23:02:52 +00:00
|
|
|
return sunsetDate
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
parsed, err := time.Parse("2006-01-02", sunsetDate)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return sunsetDate
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return parsed.UTC().Format(http.TimeFormat)
|
|
|
|
|
}
|