Example #1
0
func newMediaClient(req *wcg.Request) (*media.Client, error) {
	const fallbackServer = "speedland-ng-dev.appspot.com"
	kind := entities.OAuth2ClientSettings
	if lib.IsOnLocalGAE() || req.IsTest() {
		kind = kind.NewRemote(fallbackServer)
	}
	_, settings, err := kind.Get().Key(OAuth2ClientKey).One(req)
	if err != nil {
		return nil, err
	}
	if settings == nil {
		if lib.IsOnLocalGAE() || req.IsTest() {
			return nil, fmt.Errorf("OAuthClient `%s` is not configured on %s", OAuth2ClientKey, fallbackServer)
		}
		return nil, fmt.Errorf("OAuthClient `%s` is not configured", OAuth2ClientKey)
	}
	return media.NewClient(
		settings.(*models.OAuth2ClientSettings).GenClient(urlfetch.NewContext(req)),
	), nil
}
Example #2
0
func serverInit(req *wcg.Request) {
	if lib.IsOnLocalGAE() {
		req.Logger.Infof("Loading ./configs/local/server_configs.json...")
		if err := configs.ConfigureFromFile(req, "./configs/local/server_configs.json", false); err != nil {
			req.Logger.Warnf("Failed: %v", err)
		}
		req.Logger.Infof("Loading ./configs/local/gates.json...")
		if err := gates.ConfigureFromFile(req, "./configs/local/gates.json", false); err != nil {
			req.Logger.Warnf("Failed: %v", err)
		}
	}
}
Example #3
0
// PushTask to push a task into the queue.
func (queue *PushQueue) PushTask(req *wcg.Request, urlPath string, form url.Values) error {
	var queueName string
	if req.IsTest() || lib.IsOnLocalGAE() {
		queueName = "default"
	} else {
		queueName = queue.Name
	}
	if _, err := taskqueue.Add(gae.NewContext(req), taskqueue.NewPOSTTask(urlPath, form), queueName); err != nil {
		req.Logger.Errorf("[Queue] Error adding a task (%s) into the queue (%q): %v", urlPath, queueName, err)
		return err
	}
	return nil
}
Example #4
0
func newRemoteAPIContext(req *wcg.Request, host string) (context.Context, error) {
	// TODO: migrate file to datastore?
	if !lib.IsOnLocalGAE() && !req.IsTest() {
		return nil, fmt.Errorf("RemoteAPI use on GAE is not supported yet.")
	}

	var hc *http.Client
	var err error
	const defaultCredentialEnvKey = "GOOGLE_APPLICATION_CREDENTIALS"
	if os.Getenv(defaultCredentialEnvKey) != "" {
		hc, err = google.DefaultClient(gae.NewContext(req), remoteCtxScopes...)
	} else {
		return nil, ErrNoRemoteAPIKeyIsConfigured
	}
	if err != nil {
		return nil, err
	}
	return remote_api.NewRemoteContext(host, hc)
}
Example #5
0
func (cacher *ImageCacher) getAlbumName() string {
	var s string
	if cacher.req.IsTest() {
		if lib.IsCI() {
			s = "test@ci"
		} else {
			hostname, _ := os.Hostname()
			s = fmt.Sprintf("test@%s", hostname)
		}
	} else if lib.IsOnLocalGAE() {
		hostname, _ := os.Hostname()
		s = fmt.Sprintf("local@%s", hostname)
	} else {
		s = fmt.Sprintf("GAE@%s", appengine.AppID(gae.NewContext(cacher.req)))
	}
	if cacher.kind.Namespace != "" {
		s = fmt.Sprintf("%s:%s", s, cacher.kind.Namespace)
	}
	return s
}
Example #6
0
func (email *EmailNotification) Deliver(req *wcg.Request) error {
	sender := configs.GetValue(req, "notifications.sender")
	if sender == "" {
		return fmt.Errorf("notifications.sender is empty.")
	}
	msg := &mail.Message{
		Sender:  sender,
		To:      email.To,
		Subject: email.Subject,
		Body:    email.Body,
	}
	if !lib.IsProduction() {
		msg.Subject = fmt.Sprintf("[Non-Prod]%s", msg.Subject)
	}
	if err := gae.SendMail(req, msg); err != nil {
		return err
	}
	if lib.IsOnLocalGAE() {
		req.Logger.Debugf("Email Content:\n%s", email.Body)
	}
	return nil
}
Example #7
0
func setupAPIDatastore(app *server.App) {
	var API = app.API()

	if lib.IsOnLocalGAE() {
		API.GET("/datastore/cleanup/:kind.json",
			server.Handler(func(req *wcg.Request) response.Response {
				k := entities.FindKind(req.Param("kind"), req.Query("ns"))
				if k == nil {
					return response.NotFound(req)
				}
				num := k.DeleteQuery().MustCommit(req)
				return response.NewJSONResponse(map[string]int{
					"entities": num,
				})
			}),
		)
	}

	API.GET("/datastore/export/:kind.json",
		middleware.APITokenOnly(),
		server.Handler(func(req *wcg.Request) response.Response {
			k := entities.FindKind(req.Param("kind"), req.Query("ns"))
			if k == nil {
				return response.NotFound(req)
			}
			// TODO: export with gzip
			return middleware.EntityStreaming(
				k.Query().Order(fmt.Sprintf("-%s", k.TimestampFieldName)).Limit(
					wcg.ParseInt(req.Query("limit"), 100, -1, math.MaxInt32),
				),
				true,
			).Handle(req)
		}))

	API.POST("/datastore/import/:kind.json",
		middleware.APITokenOnly(),
		server.Handler(func(req *wcg.Request) response.Response {
			k := entities.FindKind(req.Param("kind"), req.Query("ns"))
			if k == nil {
				return response.NotFound(req)
			}
			var line = 0
			var total = 0
			var errors = make([]*entityImportError, 0)
			var list = make([]interface{}, 0, 100)
			var keys = make([]*datastore.Key, 0, 100)
			var buff []byte
			var err error
			reader := bufio.NewReaderSize(req.HTTPRequest().Body, 4096)
			for err != io.EOF {
				buff, err = reader.ReadBytes('\n')
				line += 1
				if err != nil && err != io.EOF {
					return response.InternalServerError(req, err)
				}
				buff = bytes.TrimSpace(buff)
				if len(buff) == 0 { // skip empty row
					continue
				}
				var row entityImportRow
				if err2 := json.Unmarshal(buff, &row); err2 != nil {
					errors = append(errors, &entityImportError{
						Line:  line,
						Error: fmt.Sprintf("%v - (row: %q)", err2, buff),
					})
					continue
				}
				ent, err2 := k.NewEntityFromJSON([]byte(row.Entity))
				if err2 != nil {
				}
				total = total + 1
				keys = append(keys, buildKey(req, row.Key))
				list = append(list, ent)
				if len(list) == 100 {
					k.PutMulti().DatastoreKeys(keys...).DoNotUpdateTimestamp(true).MustUpdate(req, list)
					list = make([]interface{}, 0, 100)
					keys = make([]*datastore.Key, 0, 100)
				}
			}
			if len(list) > 0 {
				k.PutMulti().DatastoreKeys(keys...).DoNotUpdateTimestamp(true).MustUpdate(req, list)
			}
			return response.NewJSONResponse(&entityImportResult{
				Total:  total,
				Lines:  line - 1,
				Errors: errors,
			})
		}))
}