github.com/aarzilli/tools@v0.0.0-20151123112009-0d27094f75e0/appengine/util_appengine/util.go (about) 1 // Package util_appengine reveals if requests come from appengine or plain http servers; 2 // and if the gae development server is running. 3 package util_appengine 4 5 /* 6 separated from common utils, because non-app engine projects 7 can not use the util package otherwise 8 */ 9 10 import ( 11 "fmt" 12 "net/http" 13 14 "golang.org/x/net/context" 15 "google.golang.org/appengine" 16 ) 17 18 import "os" 19 import "strings" 20 21 // http://regex101.com/ 22 23 // IsLocalEnviron tells us, if we are on the 24 // local development server, or on the google app engine cloud maschine 25 func IsLocalEnviron() bool { 26 27 return appengine.IsDevAppServer() 28 29 s := os.TempDir() 30 s = strings.ToLower(s) 31 if s[0:2] == "c:" || s[0:2] == "d:" { 32 // we are on windoofs - we are NOT on GAE 33 return true 34 } 35 return false 36 37 } 38 39 // 40 func SafelyExtractGaeCtxError(r *http.Request) (context.Context, error) { 41 if r == nil { 42 return nil, fmt.Errorf("Request is not appengine - request is nil") 43 } 44 c := checkPanicking(r) 45 if c != nil { 46 return c, nil 47 } else { 48 return nil, fmt.Errorf("Request is not appengine") 49 } 50 } 51 52 // Same as SafelyExtractGaeCtxError(), but without an error 53 func SafelyExtractGaeContext(r *http.Request) context.Context { 54 if r == nil { 55 return nil 56 } 57 c := checkPanicking(r) 58 return c 59 } 60 61 func checkPanicking(r *http.Request) context.Context { 62 defer func() { 63 recover() 64 }() 65 c := appengine.NewContext(r) 66 return c 67 }