1 // Copyright 2011 Google Inc. All rights reserved.
2 // Use of this source code is governed by the Apache 2.0
3 // license that can be found in the LICENSE file.
15 netcontext "golang.org/x/net/context"
18 // These functions are implementations of the wrapper functions
19 // in ../appengine/identity.go. See that file for commentary.
22 hDefaultVersionHostname = "X-AppEngine-Default-Version-Hostname"
23 hRequestLogId = "X-AppEngine-Request-Log-Id"
24 hDatacenter = "X-AppEngine-Datacenter"
27 func ctxHeaders(ctx netcontext.Context) http.Header {
32 return c.Request().Header
35 func DefaultVersionHostname(ctx netcontext.Context) string {
36 return ctxHeaders(ctx).Get(hDefaultVersionHostname)
39 func RequestID(ctx netcontext.Context) string {
40 return ctxHeaders(ctx).Get(hRequestLogId)
43 func Datacenter(ctx netcontext.Context) string {
44 if dc := ctxHeaders(ctx).Get(hDatacenter); dc != "" {
47 // If the header isn't set, read zone from the metadata service.
48 // It has the format projects/[NUMERIC_PROJECT_ID]/zones/[ZONE]
49 zone, err := getMetadata("instance/zone")
51 log.Printf("Datacenter: %v", err)
54 parts := strings.Split(string(zone), "/")
58 return parts[len(parts)-1]
61 func ServerSoftware() string {
62 // TODO(dsymonds): Remove fallback when we've verified this.
63 if s := os.Getenv("SERVER_SOFTWARE"); s != "" {
66 if s := os.Getenv("GAE_ENV"); s != "" {
69 return "Google App Engine/1.x.x"
72 // TODO(dsymonds): Remove the metadata fetches.
74 func ModuleName(_ netcontext.Context) string {
75 if s := os.Getenv("GAE_MODULE_NAME"); s != "" {
78 if s := os.Getenv("GAE_SERVICE"); s != "" {
81 return string(mustGetMetadata("instance/attributes/gae_backend_name"))
84 func VersionID(_ netcontext.Context) string {
85 if s1, s2 := os.Getenv("GAE_MODULE_VERSION"), os.Getenv("GAE_MINOR_VERSION"); s1 != "" && s2 != "" {
88 if s1, s2 := os.Getenv("GAE_VERSION"), os.Getenv("GAE_DEPLOYMENT_ID"); s1 != "" && s2 != "" {
91 return string(mustGetMetadata("instance/attributes/gae_backend_version")) + "." + string(mustGetMetadata("instance/attributes/gae_backend_minor_version"))
94 func InstanceID() string {
95 if s := os.Getenv("GAE_MODULE_INSTANCE"); s != "" {
98 if s := os.Getenv("GAE_INSTANCE"); s != "" {
101 return string(mustGetMetadata("instance/attributes/gae_backend_instance"))
104 func partitionlessAppID() string {
105 // gae_project has everything except the partition prefix.
106 if appID := os.Getenv("GAE_LONG_APP_ID"); appID != "" {
109 if project := os.Getenv("GOOGLE_CLOUD_PROJECT"); project != "" {
112 return string(mustGetMetadata("instance/attributes/gae_project"))
115 func fullyQualifiedAppID(_ netcontext.Context) string {
116 if s := os.Getenv("GAE_APPLICATION"); s != "" {
119 appID := partitionlessAppID()
121 part := os.Getenv("GAE_PARTITION")
123 part = string(mustGetMetadata("instance/attributes/gae_partition"))
127 appID = part + "~" + appID
132 func IsDevAppServer() bool {
133 return os.Getenv("RUN_WITH_DEVAPPSERVER") != ""