forked from mtusi2026/mtusi2026lab6
32 lines
547 B
Go
32 lines
547 B
Go
package middleware
|
|
|
|
import (
|
|
"os"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
const InstanceIDHeader = "X-Instance-Id"
|
|
|
|
var instanceID = resolveInstanceID()
|
|
|
|
func resolveInstanceID() string {
|
|
if v := os.Getenv("INSTANCE_ID"); v != "" {
|
|
return v
|
|
}
|
|
if v, err := os.Hostname(); err == nil && v != "" {
|
|
return v
|
|
}
|
|
return uuid.NewString()
|
|
}
|
|
|
|
func Instance() gin.HandlerFunc {
|
|
return func(c *gin.Context) {
|
|
c.Writer.Header().Set(InstanceIDHeader, instanceID)
|
|
c.Next()
|
|
}
|
|
}
|
|
|
|
func GetInstanceID() string { return instanceID }
|