// 2018 Rackspace, Inc. // // Licensed under the Apache License, Version 2.0 (the "License"): you may // not use this file except in compliance with the License. You may obtain // a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the // License for the specific language governing permissions and limitations // under the License. // Package raxmiddleware provides various middleware functions for use with gin package raxmiddleware import ( "fmt" "os" "runtime" "github.com/gin-gonic/gin" "github.com/gofrs/uuid" ) // CtxUUID defines the key to store in the gin.Context const CtxUUID = "X-Request-Id" // Boiler method serves as the method to read and set request and response headers as per current standards. // Some of the response headers defined here are either set by best practices, or recommended as-per the // Rackspace Engineering Handbook: // See: https://pages.github.rackspace.com/engineering/handbook/guides/api-design.html // Note: The www-authenticate header will be set in the specific authentication middleware. func Boiler() gin.HandlerFunc { return func(c *gin.Context) { // Set the Server response header with the information about software, GO version and // application version, used by the origin server to handle the request. c.Header("Server", fmt.Sprintf("%s %s/%s", runtime.Version(), os.Getenv("APP"), os.Getenv("API_VER"))) c.Header("X-Host", os.Getenv("VM")) if c.GetHeader("X-Request-Id") != "" { if id, err := uuid.FromString(c.GetHeader("X-Request-Id")); err == nil { c.Set(CtxUUID, id) c.Header("X-Request-Id", id.String()) return } } id, _ := uuid.NewV4() c.Set(CtxUUID, id) c.Header("X-Request-Id", id.String()) } }