LoginSignup
24
18

More than 3 years have passed since last update.

appengineでcontextからとれるやつ

Last updated at Posted at 2017-03-28

GAEでcontextの状態で設定を変えたりしたいとき、contextからどんな値がとれるのか覚えられないのでまとめておく。
この記事ではStandard環境のgoのみを対象としている。

手順

go1.9 go111ランタイムでサービスを分けてstandard環境にデプロイ。以下の関数の返り値を比較する。
ソース: https://github.com/crifff/go-context-value/

func handler(w http.ResponseWriter, r *http.Request) {
    ctx := appengine.NewContext(r)

    v := map[string]interface{}{}

    v["AppID"] = appengine.AppID(ctx)
    v["Datacenter"] = appengine.Datacenter(ctx)
    v["DefaultVersionHostname"] = appengine.DefaultVersionHostname(ctx)
    v["ModuleName"] = appengine.ModuleName(ctx)
    v["RequestID"] = appengine.RequestID(ctx)
    v["VersionID"] = appengine.VersionID(ctx)
    v["ServerSoftware"] = appengine.ServerSoftware()
    v["InstanceID"] = appengine.InstanceID()
    v["IsSecondGen"] = appengine.IsSecondGen()
    v["IsFlex"] = appengine.IsFlex()
    v["IsStandard"] = appengine.IsStandard()
    v["IsAppEngine"] = appengine.IsAppEngine()
    v["IsDevAppServer"] = appengine.IsDevAppServer()
    v["ServiceAccount"], _ = appengine.ServiceAccount(ctx)

    b, err := json.Marshal(v)
    if err != nil {
        panic(err)
    }
    w.Header().Add("Content-Type", "text/json")
    fmt.Fprint(w, string(b))
}

それぞれ下記のURLでアクセスできる

go service version URL
go1.9 default v1(default) https://v1-dot-go-context-value.appspot.com
go1.9 default v2 https://v2-dot-go-context-value.appspot.com
go1.11 default go111 https://go111-dot-go-context-value.appspot.com
go1.9 alpha v1(default) https://v1-dot-alpha-dot-go-context-value.appspot.com
go1.9 default v2 https://v2-dot-alpha-dot-go-context-value.appspot.com
go1.11 alpha go111 https://go111-dot-alpha-dot-go-context-value.appspot.com

結果

Local(DevServer/go1.9)

{
AppID: "None",
Datacenter: "us1",
DefaultVersionHostname: "localhost:8080",
InstanceID: "d29a6f03ec0778baf8bc2a0fa774aacc02bb",
IsAppEngine: true,
IsDevAppServer: true,
IsFlex: false,
IsSecondGen: false,
IsStandard: true,
ModuleName: "default",
RequestID: "fedbc794ac72e67fce70bea0eabb781921f3eef6b8c487",
ServerSoftware: "Development/2.0",
ServiceAccount: "test@localhost",
VersionID: "None.364449680094579473"
}

Local(DevServer/go111)

{
AppID: "None",
Datacenter: "",
DefaultVersionHostname: "localhost:8080",
InstanceID: "a11fdc6301ffee05f7689e6b5f2dfe6aadfe",
IsAppEngine: false,
IsDevAppServer: true,
IsFlex: false,
IsSecondGen: false,
IsStandard: false,
ModuleName: "default",
RequestID: "3e5e032f5c5a1eedfe2d05da90cda8c088bf8af6fb7727ea5b71fee52c90c0b86cbc27ebc8ccefce8facfab2dc3cce",
ServerSoftware: "Development/2.0",
ServiceAccount: "test@localhost",
VersionID: "None.449244578024933454"
}

default/v1(go1.9)

{
AppID: "go-context-value",
Datacenter: "asia-northeast1-2",
DefaultVersionHostname: "go-context-value.appspot.com",
InstanceID: "00c61b117c4208063954f191b68b4874f42a81a4613cb2391f230faf7ff13535dfc90fc7183f",
IsAppEngine: true,
IsDevAppServer: false,
IsFlex: false,
IsSecondGen: false,
IsStandard: true,
ModuleName: "default",
RequestID: "5d2729b600ff0c7ed2bd2a31060001627e676f2d636f6e746578742d76616c756500017631000100",
ServerSoftware: "Google App Engine/1.9.71",
ServiceAccount: "go-context-value@appspot.gserviceaccount.com",
VersionID: "v1.419522998664490749"
}

default/v2(go1.9)

{
AppID: "go-context-value",
Datacenter: "asia-northeast1-2",
DefaultVersionHostname: "go-context-value.appspot.com",
InstanceID: "00c61b117c9ca8a71316870ce43989f5b9191979daae14874331e8df0c0bbccde1b5ee7fe269",
IsAppEngine: true,
IsDevAppServer: false,
IsFlex: false,
IsSecondGen: false,
IsStandard: true,
ModuleName: "default",
RequestID: "5d2729c200ff0e992090f0456d0001627e676f2d636f6e746578742d76616c756500017632000100",
ServerSoftware: "Google App Engine/1.9.71",
ServiceAccount: "go-context-value@appspot.gserviceaccount.com",
VersionID: "v2.419523017757684430"
}

default/go111(go1.11)

{
AppID: "go-context-value",
Datacenter: "asia-northeast1-2",
DefaultVersionHostname: "go-context-value.appspot.com",
InstanceID: "00c61b117c9602f1817444427716f2f9a42f11dc0a7eadedb977e814a3c7abd2c8678a90975b",
IsAppEngine: true,
IsDevAppServer: false,
IsFlex: false,
IsSecondGen: true,
IsStandard: true,
ModuleName: "default",
RequestID: "5d2729d100ff0361c42aa165df0001627e676f2d636f6e746578742d76616c75650001676f313131000100",
ServerSoftware: "standard",
ServiceAccount: "go-context-value@appspot.gserviceaccount.com",
VersionID: "go111.419522942073841896"
}

alpha/v1(go1.9)

{
AppID: "go-context-value",
Datacenter: "asia-northeast1-2",
DefaultVersionHostname: "go-context-value.appspot.com",
InstanceID: "00c61b117c486572bdd3d8471df1650b2b6687c501a1ae8b3f6ebcfb3f85d069ef1e42b96db0",
IsAppEngine: true,
IsDevAppServer: false,
IsFlex: false,
IsSecondGen: false,
IsStandard: true,
ModuleName: "alpha",
RequestID: "5d2729fd00ff0c6827e7d0795d0001627e676f2d636f6e746578742d76616c75650001616c7068613a7631000100",
ServerSoftware: "Google App Engine/1.9.71",
ServiceAccount: "go-context-value@appspot.gserviceaccount.com",
VersionID: "v1.419523038922272815"
}

alpha/v2(go1.9)

{
AppID: "go-context-value",
Datacenter: "asia-northeast1-2",
DefaultVersionHostname: "go-context-value.appspot.com",
InstanceID: "00c61b117cd83d41491a86632082d3fafb8f58e7edef707922a03b181a8addf116aad79ac1e6",
IsAppEngine: true,
IsDevAppServer: false,
IsFlex: false,
IsSecondGen: false,
IsStandard: true,
ModuleName: "alpha",
RequestID: "5d2729fe00ff082562e2c9b5cd0001627e676f2d636f6e746578742d76616c75650001616c7068613a7632000100",
ServerSoftware: "Google App Engine/1.9.71",
ServiceAccount: "go-context-value@appspot.gserviceaccount.com",
VersionID: "v2.419523046912807332"
}

alpha/go111(go1.11)

{
AppID: "go-context-value",
Datacenter: "asia-northeast1-2",
DefaultVersionHostname: "go-context-value.appspot.com",
InstanceID: "00c61b117ca653f6fcc15941dabb4167698307fa5e2a632c0f3a93aeea51e59da3eddc238997",
IsAppEngine: true,
IsDevAppServer: false,
IsFlex: false,
IsSecondGen: true,
IsStandard: true,
ModuleName: "alpha",
RequestID: "5d2729fe00ff0efb11ff0034ad970001627e676f2d636f6e746578742d76616c75650001616c7068613a676f313131000100",
ServerSoftware: "standard",
ServiceAccount: "go-context-value@appspot.gserviceaccount.com",
VersionID: "go111.419523079476164322"
}

まとめ

AppID

常にアプリケーション名(プロジェクトID)を返す。
アプリケーション名(プロジェクトID)はデプロイ時に必須なので必ず任意の値が入る。
ただしローカル環境でアプリケーション名(プロジェクトID)の指定がない場合は None

Datacenter

常に作成時のリージョンを返す

IsAppEngine

LocalでもAppEngine上でも常に真

IsDevAppServer

Localのみ真

IsFlex

今回はStandardでしか検証していない全て偽

IsSecondGen

go111ランタイムで真

IsStandard

今回はStandardでしか検証していないので全て真

DefaultVersionHostname

バージョン、モジュールによらず常に基本となるホスト名を返す [アプリケーションID].appspot.com

ModuleName

常に現在のモジュール名を返す。
指定がない場合は default

RequestID

リクエストに対し一意なIDを返す。

ServerSoftware

go1.9以前はSDKのバージョンを含む文字列を返すがgo111ランタイムでは standard しか返さない。 IsSecondGenはこれで判別されている。ローカルは Development/2.0 を返す。

ServiceAccount

デフォルトのサービスアカウント名を返す。ローカルは test@localhost を返す。

VersionID

現在のバージョンとデプロイ毎に変化するバージョン番号が結合した文字列を返す [現在のバージョン名].[バージョン番号]
バージョン名はデプロイ時に必須なので必ず任意の値が入る。
ただしローカル環境で指定しない場合は None

24
18
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
24
18