server

package
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 15, 2025 License: Apache-2.0 Imports: 4 Imported by: 0

README

server

Go Reference

Servidor HTTP modular baseado em Fiber, com middlewares para forwarding de headers, controle de cache, cabeçalhos customizados e healthcheck.

Instalação

go get gitlab.globoi.com/globoplay/go-prime/server

Visão Geral

  • Remove cabeçalhos padrão de identificação do servidor
  • Adiciona cabeçalho X-Origin-App para rastreio de origem
  • Middleware para forwarding de headers customizáveis
  • Middleware para controle de cache HTTP
  • Endpoint /healthcheck pronto para uso

Exemplo Rápido

import (
    "log"
    "gitlab.globoi.com/globoplay/go-prime/server"
)

func main() {
    srv := server.NewServer("my-app", []string{"x-request-id", "x-client-user-agent"})
    log.Fatal(srv.App.Listen(":8080"))
}

Middlewares

ForwardHeadersMiddleware

Coleta e encaminha headers HTTP de interesse para handlers e serviços downstream. Útil para rastreamento, autenticação e contexto de requisição.

  • Por padrão, encaminha headers comuns de tracing e identificação.
  • Permite customizar a lista de headers.
  • Adiciona sempre o header x-origin-app.

Configuração:

app.Use(server.ForwardHeadersMiddleware("my-app", []string{"x-request-id", "x-client-user-agent"}))

Acessando headers encaminhados:

headers := c.Locals("forwardedHeaders").(map[string]string)
SetCacheControlMiddleware

Define o header Cache-Control para rotas ou grupos, facilitando o controle de cache HTTP.

  • Suporta os tipos: public, private, no-store, no-cache.
  • Permite definir TTL (max-age) em segundos.

Configuração:

app.Get("/public", server.SetCacheControlMiddleware(server.CachePublic, 60), handler)
app.Get("/private", server.SetCacheControlMiddleware(server.CachePrivate, 0), handler)

Endpoint de Healthcheck

O servidor já expõe o endpoint /healthcheck para monitoramento:

curl http://localhost:8080/healthcheck
# Resposta: OK

Estrutura e Extensibilidade

  • O tipo Server expõe o campo App para customização avançada com Fiber.
  • Adicione middlewares, rotas e handlers conforme sua necessidade.
  • Integre facilmente com middlewares de autenticação, logging, cache, etc.

API

Veja a documentação completa em pkg.go.dev.

Contribuindo

Contribuições são bem-vindas! Abra issues ou pull requests.

Licença

MIT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ForwardHeadersMiddleware

func ForwardHeadersMiddleware(appName string, forwardHeaders []string) fiber.Handler

func SetCacheControlMiddleware

func SetCacheControlMiddleware(cacheType CacheType, ttl int) fiber.Handler

SetCacheControlMiddleware sets the Cache-Control header for a route or group in Fiber.

Parameters:

cacheType: One of the allowed CacheType values (public, private, no-store, no-cache).
ttl: Time to live in seconds for the cache (max-age). If <= 0, max-age is omitted.

Usage:

app.Get("/route", SetCacheControlMiddleware(CachePublic, 60), handler)

If an invalid cacheType is provided, the middleware returns an error and does not set the header.

Types

type CacheType

type CacheType string

CacheType defines allowed values for the Cache-Control header type. Use the provided constants for safety and validation.

const (
	CachePublic  CacheType = "public"
	CachePrivate CacheType = "private"
	CacheNoStore CacheType = "no-store"
	CacheNoCache CacheType = "no-cache"
)

type ForwardedHeadersKeyType

type ForwardedHeadersKeyType struct{}

type Server

type Server struct {
	App *fiber.App
}

func NewServer

func NewServer(name string, forwardHeaders []string) *Server

NewServer creates and configures a Fiber server instance.

Parameters:

name: The name of the origin application. Used for the X-Origin-App header.
forwardHeaders: List of headers to be forwarded. If empty, uses defaults.

Behavior:

  • Removes default server identification headers.
  • Sets the X-Origin-App header in the request.
  • Applies ForwardHeadersMiddleware to collect and forward headers.
  • Adds a /healthcheck endpoint for health monitoring.

Usage:

server := NewServer("my-app", []string{"x-request-id", "x-client-user-agent"})
log.Fatal(server.App.Listen(":8080"))

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL