github.com/yandex/pandora@v0.5.32/components/providers/http/middleware/headerdate/middleware.go (about)

     1  package headerdate
     2  
     3  import (
     4  	"context"
     5  	"net/http"
     6  	"time"
     7  
     8  	"go.uber.org/zap"
     9  )
    10  
    11  const defaultHeaderName = "Date"
    12  
    13  type Config struct {
    14  	Location   string
    15  	HeaderName string
    16  }
    17  
    18  func NewMiddleware(cfg Config) (*Middleware, error) {
    19  	m := &Middleware{location: time.UTC, header: defaultHeaderName}
    20  
    21  	if cfg.Location != "" {
    22  		loc, err := time.LoadLocation(cfg.Location)
    23  		if err != nil {
    24  			return nil, err
    25  		}
    26  		m.location = loc
    27  	}
    28  	if cfg.HeaderName != "" {
    29  		m.header = cfg.HeaderName
    30  	}
    31  
    32  	return m, nil
    33  }
    34  
    35  type Middleware struct {
    36  	location *time.Location
    37  	header   string
    38  }
    39  
    40  func (m *Middleware) InitMiddleware(ctx context.Context, log *zap.Logger) error {
    41  	return nil
    42  }
    43  
    44  func (m *Middleware) UpdateRequest(req *http.Request) error {
    45  	req.Header.Add(m.header, time.Now().In(m.location).Format(http.TimeFormat))
    46  	return nil
    47  }