faas/gateway/handlers/callid_middleware.go
Alex Ellis (VMware) af0ccc9a45 Add Copyright headers where missing in handlers
- Added Copyright in handlers where missing
- Renamed Project to Author(s) where needed

Signed-off-by: Alex Ellis (VMware) <alexellis2@gmail.com>
2018-09-03 13:26:58 +01:00

30 lines
795 B
Go

// Copyright (c) OpenFaaS Author(s). All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
package handlers
import (
"fmt"
"net/http"
"time"
"github.com/docker/distribution/uuid"
)
// MakeCallIDMiddleware middleware tags a request with a uid
func MakeCallIDMiddleware(next http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
start := time.Now()
if len(r.Header.Get("X-Call-Id")) == 0 {
callID := uuid.Generate().String()
r.Header.Add("X-Call-Id", callID)
w.Header().Add("X-Call-Id", callID)
}
r.Header.Add("X-Start-Time", fmt.Sprintf("%d", start.UTC().UnixNano()))
w.Header().Add("X-Start-Time", fmt.Sprintf("%d", start.UTC().UnixNano()))
next(w, r)
}
}