Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions api/requests.http
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,10 @@ Content-Type: application/json
"messageTypes": ["SMS", "MMS"]
}

###
# @no-redirect
GET http://localhost:3000/ HTTP/1.1

###
GET http://localhost:3000/metrics HTTP/1.1

Expand Down
24 changes: 24 additions & 0 deletions internal/sms-gateway/handlers/root.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package handlers

import (
"fmt"
"path"
"strings"

Expand All @@ -16,6 +17,8 @@ type rootHandler struct {
}

func (h *rootHandler) Register(app *fiber.App) {
app.Get("/", h.handleRoot)

if h.config.PublicPath != "/api" {
app.Use(func(c *fiber.Ctx) error {
err := c.Next()
Expand All @@ -34,13 +37,34 @@ func (h *rootHandler) Register(app *fiber.App) {
h.registerOpenAPI(app)
}

func (h *rootHandler) handleRoot(c *fiber.Ctx) error {
h.setLinkHeaders(c)

if err := c.Redirect(h.config.PublicPath, fiber.StatusMovedPermanently); err != nil {
return fmt.Errorf("redirect to public path %q: %w", h.config.PublicPath, err)
}
return nil
}

func (h *rootHandler) setLinkHeaders(c *fiber.Ctx) {
publicPath := strings.TrimRight(h.config.PublicPath, "/")
c.Set(fiber.HeaderLink,
`</.well-known/api-catalog>; rel="api-catalog", `+
`<`+publicPath+`/docs/doc.json>; rel="service-desc", `+
`<https://docs.sms-gate.app/>; rel="service-doc", `+
`</.well-known/api-catalog>; rel="describedby"`,
)
}

func (h *rootHandler) registerOpenAPI(router fiber.Router) {
if !h.config.OpenAPIEnabled {
return
}

router.Use(func(c *fiber.Ctx) error {
if c.Path() == "/api" || c.Path() == "/api/" {
h.setLinkHeaders(c)

return c.Redirect("/api/docs", fiber.StatusMovedPermanently)
}

Expand Down
Loading