func handlePut(res http.ResponseWriter, req *http.Request) { ctx := appengine.NewContext(req) hc := &http.Client{ Transport: &oauth2.Transport{ Source: google.AppEngineTokenSource(ctx, storage.ScopeFullControl), Base: &urlfetch.Transport{Context: ctx}, }, } cctx := cloud.NewContext(appengine.AppID(ctx), hc) rdr, hdr, err := req.FormFile("form-upload-file") if err != nil { http.Error(res, "ERROR RECEIVING FILE: "+err.Error(), 500) return } writer := storage.NewWriter(cctx, bucketName, hdr.Filename) io.Copy(writer, rdr) err = writer.Close() if err != nil { http.Error(res, "ERROR WRITING TO BUCKET: "+err.Error(), 500) return } }
func fileHandler(c *gin.Context) { username := c.Param("username") title := c.Param("title") filename := c.Param("fileName") gaeContext := appengine.NewContext(c.Request) hc := &http.Client{ Transport: &CloudStorageTransport{&oauth2.Transport{ Source: google.AppEngineTokenSource(gaeContext, storage.ScopeFullControl), Base: &urlfetch.Transport{Context: gaeContext}, }}, } bucketName := "balde_de_bits" bucketFile := username + "/" + title + "/" + filename log.Errorf(gaeContext, "ID ->>> %v", appengine.AppID(gaeContext)) log.Errorf(gaeContext, "File name ->>> %v", bucketFile) ctx := cloud.NewContext(appengine.AppID(gaeContext), hc) wc := storage.NewWriter(ctx, bucketName, bucketFile) if strings.Contains(filename, "m3u8") { wc.ContentType = "application/x-mpegURL" wc.CacheControl = "max-age:0" } else if strings.Contains(filename, "ts") { wc.ContentType = "video/MP2T" } else if strings.Contains(filename, "jpg") { wc.ContentType = "image/jpeg" } defer wc.Close() bytesWritten, err := io.Copy(wc, c.Request.Body) if err != nil { log.Errorf(gaeContext, "Writing to cloud storage failed. %v", err.Error()) c.JSON(200, gin.H{ "response": "< FAILED >", }) return } log.Errorf(gaeContext, "Wrote < %v > bytes for file < %v >", bytesWritten, filename) c.JSON(200, gin.H{ "response": "< worked >", }) }
func file(ctx context.Context, w http.ResponseWriter, r *http.Request) { fname := r.URL.Query().Get("f") n := r.URL.Query().Get("n") lineno, _ := strconv.Atoi(n) f, err := ioutil.ReadFile(fname) if err != nil { serveError(w, err) return } fp := make(map[int]string) for k, v := range strings.Split(string(f), "\n") { fp[k+1] = v } v := struct { Env map[string]string Filename string Lineno int Fp map[int]string }{ Env: map[string]string{ "APPLICATION_ID": appengine.AppID(ctx), }, Filename: fname, Lineno: lineno, Fp: fp, } _ = templates.ExecuteTemplate(w, "file", v) }
func getCloudContext(aeCtx context.Context) (context.Context, error) { data, err := ioutil.ReadFile("gcs.xxjson") if err != nil { return nil, err } conf, err := google.JWTConfigFromJSON( data, storage.ScopeFullControl, ) if err != nil { return nil, err } tokenSource := conf.TokenSource(aeCtx) hc := &http.Client{ Transport: &oauth2.Transport{ Source: tokenSource, Base: &urlfetch.Transport{Context: aeCtx}, }, } return cloud.NewContext(appengine.AppID(aeCtx), hc), nil }
// datasets returns a list with the IDs of all the Big Query datasets visible // with the given context. func datasets(ctx context.Context) ([]string, error) { // Create a new authenticated HTTP client over urlfetch. hc, err := google.DefaultClient(ctx, bigquery.BigqueryScope) if err != nil { return nil, fmt.Errorf("could not create http client: %v", err) } // Create the BigQuery service. bq, err := bigquery.New(hc) if err != nil { return nil, fmt.Errorf("could not create service: %v", err) } // Get the current application ID, which is the same as the project ID. projectID := appengine.AppID(ctx) // Return a list of IDs. var ids []string datasets, err := bq.Datasets.List(projectID).Do() if err != nil { return nil, fmt.Errorf("could not list datasets for %q: %v", projectID, err) } for _, d := range datasets.Datasets { ids = append(ids, d.Id) } return ids, nil }
// Clone creates a local copy of the repository accessible at // github.com/user/repo with token, in a system temp directory. func clone(c context.Context, repoOwner, repoName, userName, token string) (repository.Repo, error) { dir, err := ioutil.TempDir("", fmt.Sprintf("%s-%s", userName, repoName)) if err != nil { return nil, err } cloneCmd := exec.Command("git", "clone", makeRemoteURL(userName, token, repoOwner, repoName), dir) if _, err := cloneCmd.CombinedOutput(); err != nil { return nil, err } repo, err := repository.NewGitRepo(dir) if err != nil { return nil, err } if err := repo.PullNotes(remoteName, notesRefPattern); err != nil { return nil, err } fetchCmd := exec.Command("git", "fetch", "origin", fetchSpec) fetchCmd.Dir = dir if _, err := fetchCmd.CombinedOutput(); err != nil { return nil, err } configUserCmd := exec.Command("git", "config", "--local", "--add", "user.name", "Github Mirror") configUserCmd.Dir = dir if _, err := configUserCmd.CombinedOutput(); err != nil { return nil, err } userEmail := appengine.AppID(c) + "@appspot.gserviceaccount.com" configEmailCmd := exec.Command("git", "config", "--local", "--add", "user.email", userEmail) configEmailCmd.Dir = dir if _, err := configEmailCmd.CombinedOutput(); err != nil { return nil, err } return repo, nil }
// datasets returns a list with the ids of all the Big Query datasets visible // with the given context. func datasets(ctx context.Context) ([]string, error) { // create a new authenticated HTTP client over urlfetch. client := &http.Client{ Transport: &oauth2.Transport{ Source: google.AppEngineTokenSource(ctx, bigquery.BigqueryScope), Base: &urlfetch.Transport{Context: ctx}, }, } // create the BigQuery service. bq, err := bigquery.New(client) if err != nil { return nil, fmt.Errorf("could not create service: %v", err) } // obtain the current application id, the BigQuery id is the same. appID := appengine.AppID(ctx) // prepare the list of ids. var ids []string datasets, err := bq.Datasets.List(appID).Do() if err != nil { return nil, fmt.Errorf("could not list datasets for %q: %v", appID, err) } for _, d := range datasets.Datasets { ids = append(ids, d.Id) } return ids, nil }
func insert(datasetId string, tableName string, ctx context.Context, json map[string]bigquery.JsonValue) { client := &http.Client{ Transport: &oauth2.Transport{ Source: google.AppEngineTokenSource(ctx, bigquery.BigqueryScope), Base: &urlfetch.Transport{Context: ctx}, }, } bq, err := bigquery.New(client) if err != nil { fmt.Errorf("could not create service: %v", err) return } appID := appengine.AppID(ctx) request := new(bigquery.TableDataInsertAllRequest) rows := make([]*bigquery.TableDataInsertAllRequestRows, 1) rows[0] = new(bigquery.TableDataInsertAllRequestRows) rows[0].Json = json request.Rows = rows _, errr := bq.Tabledata.InsertAll(appID, datasetId, tableName, request).Do() if errr != nil { fmt.Errorf("could not insert: %v", err) return } }
func view(w http.ResponseWriter, r *http.Request, m map[string]interface{}) { cntr := 1 tplAdder, tplExec := tplx.FuncTplBuilder(w, r) tplAdder("n_html_title", "Application, Module and Instance Info", nil) tplAdder("n_cont_1", "<pre>{{.}}</pre>", instance_mgt.GetStatic().String()) tplAdder("n_cont_2", "<p>{{.}} views</p>", cntr) tplAdder("n_cont_0", ` <p>AppID is `+appengine.AppID(appengine.NewContext(r))+`</p> <p>On the development server, call <a href='/instance-info/collect' target='collect' >collect</a> first.</p> <p><a href='/instance-info/`+instance_mgt.GetStatic().InstanceID+`'>specific url</a></p> `, "") tplExec(w, r) /* Requests are routed randomly accross instances Following is just a futile try to register an instance specific handler. It is only useful, when we request an instance specifically via specific hostname */ SuppressPanicUponDoubleRegistration( w, r, "/instance-info/"+instance_mgt.GetStatic().InstanceID, loghttp.Adapter(view)) }
// handler is the main demo entry point that calls the GCS operations. func handler(w http.ResponseWriter, r *http.Request) { if r.URL.Path != "/" { http.NotFound(w, r) return } c := appengine.NewContext(r) if bucket == "" { var err error if bucket, err = file.DefaultBucketName(c); err != nil { log.Errorf(c, "failed to get default GCS bucket name: %v", err) return } } hc := &http.Client{ Transport: &oauth2.Transport{ Source: google.AppEngineTokenSource(c, storage.ScopeFullControl), // Note that the App Engine urlfetch service has a limit of 10MB uploads and // 32MB downloads. // See https://cloud.google.com/appengine/docs/go/urlfetch/#Go_Quotas_and_limits // for more information. Base: &urlfetch.Transport{Context: c}, }, } ctx := cloud.NewContext(appengine.AppID(c), hc) w.Header().Set("Content-Type", "text/plain; charset=utf-8") fmt.Fprintf(w, "Demo GCS Application running from Version: %v\n", appengine.VersionID(c)) fmt.Fprintf(w, "Using bucket name: %v\n\n", bucket) d := &demo{ c: c, w: w, ctx: ctx, } n := "demo-testfile-go" d.createFile(n) d.readFile(n) d.copyFile(n) d.statFile(n) d.createListFiles() d.listBucket() d.listBucketDirMode() d.defaultACL() d.putDefaultACLRule() d.deleteDefaultACLRule() d.bucketACL() d.putBucketACLRule() d.deleteBucketACLRule() d.acl(n) d.putACLRule(n) d.deleteACLRule(n) d.deleteFiles() if d.failed { io.WriteString(w, "\nDemo failed.\n") } else { io.WriteString(w, "\nDemo succeeded.\n") } }
func DeleteMetric(c context.Context, metric string) (err error) { service, err := NewMonitoringService(c) if err != nil { return } _, err = service.MetricDescriptors.Delete(appengine.AppID(c), metric).Do() return }
func fullSubscription(c context.Context, s string) string { re, _ := regexp.Compile(`projects/([\w-]+)/subscriptions/([\w-]+)`) if re.MatchString(s) { return s } else { return fmt.Sprintf("projects/%s/subscriptions/%s", appengine.AppID(c), s) } }
func (session *Session) DatastoreInfo() (err error) { c, err := session.Context() if err != nil { return } log.Printf("App ID %q", appengine.AppID(c)) return }
func getCloudContext(gae context.Context) context.Context { hc := &http.Client{ Transport: &oauth2.Transport{ Source: google.AppEngineTokenSource(gae, storage.ScopeFullControl), Base: &urlfetch.Transport{Context: gae}, }, } return cloud.NewContext(appengine.AppID(gae), hc) }
func (cfg *AppengineStoreConfig) Generate(c context.Context) files.FileStore { hc := &http.Client{ Transport: &oauth2.Transport{ Source: google.AppEngineTokenSource(c, storage.ScopeFullControl), Base: &urlfetch.Transport{Context: c}, }, } ctx := cloud.WithContext(c, appengine.AppID(c), hc) return &AppengineStore{cfg, ctx} }
func auth(r *http.Request) (context.Context, error) { c := appengine.NewContext(r) client, err := google.DefaultClient(c, storage.ScopeFullControl) if err != nil { return nil, err } ctx := cloud.NewContext(appengine.AppID(c), client) return ctx, nil }
func alertAdmins(c context.Context, key *datastore.Key, entity Lockable, reason string) error { sender := "locker@" + appengine.AppID(c) + ".appspot.com" msg := &mail.Message{ Sender: sender, Subject: reason, Body: fmt.Sprintf("key: %s, entity: %#v", key.String(), entity), } return mail.SendToAdmins(c, msg) }
// Get an auth context for logging RPC. func cloudAuthContext(r *http.Request) (context.Context, error) { c := appengine.NewContext(r) hc := &http.Client{ Transport: &oauth2.Transport{ Source: google.AppEngineTokenSource(c, logging.Scope), Base: &urlfetch.Transport{Context: c}, }, } return cloud.WithContext(c, appengine.AppID(c), hc), nil }
func httpClient(r *http.Request) *http.Client { c := appengine.NewContext(r) return &http.Client{ Transport: &httputil.Transport{ Token: github.Token, ClientID: github.ClientID, ClientSecret: github.ClientSecret, Base: &urlfetch.Transport{Context: c, Deadline: 10 * time.Second}, UserAgent: fmt.Sprintf("%s (+http://%s/-/bot)", appengine.AppID(c), r.Host), }, } }
// handler is the main demo entry point that calls the GCS operations. func handler(w http.ResponseWriter, r *http.Request) { if r.URL.Path != "/" { http.NotFound(w, r) return } c := appengine.NewContext(r) bucketName, err := file.DefaultBucketName(c) if err != nil { c.Errorf("failed to get default GCS bucket name: %v", err) return } config := google.NewAppEngineConfig(c, storage.ScopeFullControl) ctx := cloud.NewContext(appengine.AppID(c), &http.Client{Transport: config.NewTransport()}) w.Header().Set("Content-Type", "text/plain; charset=utf-8") fmt.Fprintf(w, "Demo GCS Application running from Version: %v\n", appengine.VersionID(c)) fmt.Fprintf(w, "Using bucket name: %v\n\n", bucketName) d := &demo{ c: c, w: w, ctx: ctx, bucket: bucketName, } n := "demo-testfile-go" d.createFile(n) d.readFile(n) d.copyFile(n) d.statFile(n) d.createListFiles() d.listBucket() d.listBucketDirMode() d.defaultACL() d.putDefaultACLRule() d.deleteDefaultACLRule() d.bucketACL() d.putBucketACLRule() d.deleteBucketACLRule() d.acl(n) d.putACLRule(n) d.deleteACLRule(n) d.deleteFiles() if d.failed { io.WriteString(w, "\nDemo failed.\n") } else { io.WriteString(w, "\nDemo succeeded.\n") } }
func ListMetric(c context.Context) (metrics []*cloudmonitoring.MetricDescriptor, err error) { service, err := NewMonitoringService(c) if err != nil { return } r := cloudmonitoring.ListMetricDescriptorsRequest{Kind: "cloudmonitoring#listMetricDescriptorsRequest"} resp, err := service.MetricDescriptors.List(appengine.AppID(c), &r).Do() if err != nil { return } metrics = resp.Metrics return }
func httpClient(r *http.Request) *http.Client { ctx, _ := context.WithTimeout(appengine.NewContext(r), 10*time.Second) github := httputil.NewAuthTransportFromEnvironment(nil) return &http.Client{ Transport: &httputil.AuthTransport{ Token: github.Token, ClientID: github.ClientID, ClientSecret: github.ClientSecret, Base: &urlfetch.Transport{Context: ctx}, UserAgent: fmt.Sprintf("%s (+http://%s/-/bot)", appengine.AppID(ctx), r.Host), }, } }
// Stop puts the instance in the TERMINATED state, but does not delete it. func (vm *VM) Stop(c context.Context) (err error) { log.Debugf(c, "Stopping instance ...") service, err := newComputeService(c) if err != nil { return err } project := appengine.AppID(c) op, err := service.Instances.Stop(project, vm.Instance.Zone, vm.Instance.Name).Do() if err != nil { return err } // TODO(ronoaldo): check the operation result for operation errors. return vm.waitUntilDone(service, project, op) }
//Diagnostics shows a bunch of app engine's information for the app/project //useful for figuring out which version of an app is serving func Diagnostics(w http.ResponseWriter, r *http.Request) { c := appengine.NewContext(r) out := map[string]interface{}{ "App ID": appengine.AppID(c), "Instance ID": appengine.InstanceID(), "Default Version Hostname": appengine.DefaultVersionHostname(c), "Version ID": appengine.VersionID(c), "Datacenter": appengine.Datacenter(c), "Module Name": appengine.ModuleName(c), "Server Software": appengine.ServerSoftware(), } templates.Load(w, "diagnostics", out) return }
func checkReferer(c context.Context) error { if appengine.IsDevAppServer() { return nil } r := endpoints.HTTPRequest(c).Referer() u, err := url.Parse(r) if err != nil { return endpoints.NewUnauthorizedError("couldn't extract domain from referer") } if u.Host != appengine.AppID(c)+".appspot.com" { return endpoints.NewUnauthorizedError("referer unauthorized") } return nil }
// PublicIP returns the current instance IP. The value is cached in-memory, // so it may return stale results. func (vm *VM) PublicIP(c context.Context) string { if vm.ip == "" { project := appengine.AppID(c) service, err := newComputeService(c) if err != nil { log.Errorf(c, "Error initializing service: %v", err) return "" } instance, err := service.Instances.Get(project, vm.Instance.Zone, vm.Instance.Name).Do() if err != nil { log.Errorf(c, "Error fetching instance IP: %v", err) return "" } vm.ip = findNatIP(c, instance) } return vm.ip }
// linkToPrimary sends HttpRPC to the primary to register myself as a replica. func linkToPrimary(c context.Context, ticket ServiceLinkTicket, initiatedBy string) error { headers := make(map[string]string) headers["Content-Type"] = "application/octet-stream" protocol := "https" if appengine.IsDevAppServer() { headers["X-Appengine-Inbound-Appid"] = appengine.AppID(c) protocol = "http" } linkReq := &ServiceLinkRequest{ Ticket: ticket.GetTicket(), ReplicaUrl: proto.String(fmt.Sprintf("%s://%s", protocol, appengine.DefaultVersionHostname(c))), InitiatedBy: proto.String(initiatedBy), } buf, err := proto.Marshal(linkReq) if err != nil { return err } req, err := http.NewRequest("POST", fmt.Sprintf("%s/auth_service/api/v1/internal/link_replica", ticket.GetPrimaryUrl()), bytes.NewReader(buf)) if err != nil { return err } for key, value := range headers { req.Header.Add(key, value) } client := urlfetch.Client(c) resp, err := client.Do(req) if err != nil { return err } if resp.StatusCode != 200 { return fmt.Errorf("got status code %v; want 200", resp.StatusCode) } respBody := new(bytes.Buffer) respBody.ReadFrom(resp.Body) linkResp := &ServiceLinkResponse{} if err = proto.Unmarshal(respBody.Bytes(), linkResp); err != nil { return err } if linkResp.GetStatus() != ServiceLinkResponse_SUCCESS { return fmt.Errorf("Request to the primary failed with status %d", linkResp.GetStatus()) } return nil }
func handler(w http.ResponseWriter, r *http.Request) { c := appengine.NewContext(r) hc := &http.Client{ Transport: &oauth2.Transport{ Source: google.AppEngineTokenSource(c, storage.ScopeFullControl), Base: &urlfetch.Transport{Context: c}, }, } ctx := cloud.NewContext(appengine.AppID(c), hc) d := &demo{ c: c, w: w, ctx: ctx, } d.listBucket() }
func NewConfig(context context.Context) Config { appID := appengine.AppID(context) log.Infof(context, "AppID: %v", appID) if appengine.IsDevAppServer() { return DevConfig() } if appID == ProductionAppID { return ProductionConfig() } if appID == StagingAppID { return StagingConfig() } panic("Could not resolve environment configuration") }
func getCloudContext(aeCtx context.Context, credentials string) (context.Context, error) { conf, err := google.JWTConfigFromJSON( []byte(credentials), storage.ScopeFullControl, ) if err != nil { return nil, err } tokenSource := conf.TokenSource(aeCtx) hc := &http.Client{ Transport: &oauth2.Transport{ Source: tokenSource, Base: &urlfetch.Transport{Context: aeCtx}, }, } return cloud.NewContext(appengine.AppID(aeCtx), hc), nil }