本文整理汇总了Golang中github.com/coduno/api/util/passenger.FromContext函数的典型用法代码示例。如果您正苦于以下问题:Golang FromContext函数的具体用法?Golang FromContext怎么用?Golang FromContext使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了FromContext函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: ChallengeByKey
// ChallengeByKey loads a challenge by key.
func ChallengeByKey(ctx context.Context, w http.ResponseWriter, r *http.Request) (status int, err error) {
if r.Method != "GET" {
return http.StatusMethodNotAllowed, nil
}
p, ok := passenger.FromContext(ctx)
if !ok {
return http.StatusUnauthorized, nil
}
key, err := datastore.DecodeKey(mux.Vars(r)["key"])
if err != nil {
return http.StatusInternalServerError, err
}
var challenge model.Challenge
err = datastore.Get(ctx, key, &challenge)
if err != nil {
return http.StatusInternalServerError, err
}
e := json.NewEncoder(w)
if parent := p.UserKey.Parent(); parent == nil {
// The current user is a coder so we must also create a result.
e.Encode(challenge.Key(key))
} else {
// TODO(pbochis): If a company representativemakes the request
// we also include Tasks in the response.
e.Encode(challenge.Key(key))
}
return http.StatusOK, nil
}
示例2: GetCompanyByUser
func GetCompanyByUser(ctx context.Context, w http.ResponseWriter, r *http.Request) (int, error) {
if r.Method != "GET" {
return http.StatusMethodNotAllowed, nil
}
p, ok := passenger.FromContext(ctx)
if !ok {
return http.StatusUnauthorized, nil
}
var u model.User
if err := datastore.Get(ctx, p.User, &u); err != nil {
return http.StatusInternalServerError, nil
}
if u.Company == nil {
return http.StatusUnauthorized, nil
}
// The account is associated with a company, so we return it.
var company model.Company
if err := datastore.Get(ctx, u.Company, &company); err != nil {
return http.StatusInternalServerError, err
}
json.NewEncoder(w).Encode(company.Key(u.Company))
return http.StatusOK, nil
}
示例3: Tasks
func Tasks(ctx context.Context, w http.ResponseWriter, r *http.Request) (status int, err error) {
p, ok := passenger.FromContext(ctx)
if !ok {
return http.StatusUnauthorized, nil
}
if r.Method != "GET" {
return http.StatusMethodNotAllowed, nil
}
// User is a coder
if p.UserKey.Parent() == nil {
return http.StatusUnauthorized, nil
}
var codeTasks model.CodeTasks
codeTaskKeys, err := model.NewQueryForCodeTask().
GetAll(ctx, &codeTasks)
if err != nil {
return http.StatusInternalServerError, err
}
tasks := make([]model.KeyedTask, len(codeTasks))
for i := range codeTasks {
tasks[i] = model.KeyedTask{
Task: &codeTasks[i].Task,
Key: codeTaskKeys[i],
}
}
json.NewEncoder(w).Encode(tasks)
return http.StatusOK, nil
}
示例4: Tasks
func Tasks(ctx context.Context, w http.ResponseWriter, r *http.Request) (status int, err error) {
p, ok := passenger.FromContext(ctx)
if !ok {
return http.StatusUnauthorized, nil
}
var u model.User
if err = datastore.Get(ctx, p.User, &u); err != nil {
return http.StatusInternalServerError, err
}
// User is a coder
if u.Company == nil {
return http.StatusUnauthorized, nil
}
switch r.Method {
case "GET":
return getAllTasks(ctx, w, r)
case "POST":
return createTask(ctx, w, r)
default:
return http.StatusMethodNotAllowed, nil
}
}
示例5: AccessTokens
// AccessTokens will create new AccessTokens for the user.
func AccessTokens(ctx context.Context, w http.ResponseWriter, r *http.Request) (status int, err error) {
if r.Method != "POST" {
return http.StatusMethodNotAllowed, nil
}
p, ok := passenger.FromContext(ctx)
if !ok {
return http.StatusUnauthorized, nil
}
var body model.AccessToken
if err = json.NewDecoder(r.Body).Decode(&body); err != nil {
return http.StatusBadRequest, err
}
value, err := p.IssueToken(ctx, &body)
var result = struct {
Value string
Creation time.Time
Expiry time.Time
Description string
}{
Value: value,
Creation: body.Creation,
Expiry: body.Expiry,
Description: body.Description,
}
json.NewEncoder(w).Encode(result)
return
}
示例6: PostSubmission
// PostSubmission creates a new submission.
func PostSubmission(ctx context.Context, w http.ResponseWriter, r *http.Request) (status int, err error) {
p, ok := passenger.FromContext(ctx)
if !ok {
return http.StatusUnauthorized, nil
}
if r.Method != "POST" {
return http.StatusMethodNotAllowed, nil
}
resultKey, err := datastore.DecodeKey(mux.Vars(r)["resultKey"])
if !util.HasParent(p.UserKey, resultKey) {
return http.StatusBadRequest, errors.New("cannot submit answer for other users")
}
taskKey, err := datastore.DecodeKey(mux.Vars(r)["taskKey"])
// Note: When more task kinds are added, see controllers.CreateFinalResult.
switch taskKey.Kind() {
case model.CodeTaskKind:
return runner.HandleCodeSubmission(ctx, w, r, resultKey, taskKey)
// TODO(victorbalan, flowlo): Use correct kind when possible.
case "QuestionTask":
return http.StatusInternalServerError, errors.New("question submissions are not yet implemented")
default:
return http.StatusBadRequest, errors.New("Unknown submission kind.")
}
}
示例7: Tasks
func Tasks(ctx context.Context, w http.ResponseWriter, r *http.Request) (status int, err error) {
if r.Method != "GET" {
return http.StatusMethodNotAllowed, nil
}
p, ok := passenger.FromContext(ctx)
if !ok {
return http.StatusUnauthorized, nil
}
var u model.User
if err = datastore.Get(ctx, p.User, &u); err != nil {
return http.StatusInternalServerError, nil
}
// User is a coder
if u.Company == nil {
return http.StatusUnauthorized, nil
}
var tasks model.Tasks
taskKeys, err := model.NewQueryForTask().
GetAll(ctx, &tasks)
if err != nil {
return http.StatusInternalServerError, err
}
json.NewEncoder(w).Encode(tasks.Key(taskKeys))
return http.StatusOK, nil
}
示例8: putCookie
func putCookie(ctx context.Context, w http.ResponseWriter, r *http.Request) (int, error) {
p, ok := passenger.FromContext(ctx)
if !ok {
return http.StatusUnauthorized, nil
}
token := &model.Token{
Description: "Login from " + r.RemoteAddr,
}
value, err := p.IssueToken(ctx, token)
if err != nil {
return http.StatusInternalServerError, err
}
http.SetCookie(w, &http.Cookie{
Name: "token",
Value: value,
Secure: !appengine.IsDevAppServer(),
HttpOnly: true,
Expires: token.Expiry,
})
w.Write([]byte("OK"))
return http.StatusOK, nil
}
示例9: GetResult
func GetResult(ctx context.Context, w http.ResponseWriter, r *http.Request) (int, error) {
if !util.CheckMethod(r, "GET") {
return http.StatusMethodNotAllowed, nil
}
resultKey, err := datastore.DecodeKey(mux.Vars(r)["resultKey"])
if err != nil {
return http.StatusBadRequest, err
}
var result model.Result
if err := datastore.Get(ctx, resultKey, &result); err != nil {
return http.StatusInternalServerError, nil
}
p, ok := passenger.FromContext(ctx)
if !ok {
return http.StatusUnauthorized, nil
}
if p.UserKey.Parent() != nil {
json.NewEncoder(w).Encode(result.Key(resultKey))
return http.StatusOK, nil
}
if !util.HasParent(resultKey, p.UserKey) {
return http.StatusUnauthorized, nil
}
return createFinalResult(ctx, w, resultKey, result)
}
示例10: GetChallengesForCompany
// GetChallengesForCompany queries all the challenges defined by a company.
func GetChallengesForCompany(ctx context.Context, w http.ResponseWriter, r *http.Request) (status int, err error) {
if r.Method != "GET" {
return http.StatusMethodNotAllowed, nil
}
_, ok := passenger.FromContext(ctx)
if !ok {
return http.StatusUnauthorized, nil
}
key, err := datastore.DecodeKey(mux.Vars(r)["key"])
if err != nil {
return http.StatusInternalServerError, err
}
var challenges model.Challenges
keys, err := model.NewQueryForChallenge().
Ancestor(key).
GetAll(ctx, &challenges)
if err != nil {
return http.StatusInternalServerError, err
}
json.NewEncoder(w).Encode(challenges.Key(keys))
return http.StatusOK, nil
}
示例11: GetProfileForUser
func GetProfileForUser(ctx context.Context, w http.ResponseWriter, r *http.Request) (status int, err error) {
_, ok := passenger.FromContext(ctx)
if !ok {
return http.StatusUnauthorized, nil
}
var userKey *datastore.Key
if userKey, err = datastore.DecodeKey(mux.Vars(r)["key"]); err != nil {
return http.StatusInternalServerError, err
}
var profiles []model.Profile
keys, err := model.NewQueryForProfile().
Ancestor(userKey).
Limit(1).
GetAll(ctx, &profiles)
if err != nil {
return http.StatusInternalServerError, err
}
if len(keys) != 1 {
return http.StatusNotFound, nil
}
json.NewEncoder(w).Encode(profiles[0].Key(keys[0]))
return
}
示例12: GetTestResultsForSubmission
func GetTestResultsForSubmission(ctx context.Context, w http.ResponseWriter, r *http.Request) (status int, err error) {
if r.Method != "GET" {
return http.StatusMethodNotAllowed, nil
}
// TODO(victorbalan): Check if user is company or if user is parent of result
// else return http.StatusUnauthorized
_, ok := passenger.FromContext(ctx)
if !ok {
return http.StatusUnauthorized, nil
}
submissionKey, err := datastore.DecodeKey(mux.Vars(r)["key"])
if err != nil {
return http.StatusNotFound, err
}
keys, err := datastore.NewQuery("").
Ancestor(submissionKey).
Filter("__key__ >", submissionKey).
KeysOnly().
GetAll(ctx, nil)
if err != nil {
return http.StatusInternalServerError, err
}
if len(keys) == 0 {
json.NewEncoder(w).Encode([]string{})
return http.StatusOK, nil
}
switch keys[0].Kind() {
case model.JunitTestResultKind:
var results model.JunitTestResults
_, err = datastore.NewQuery(keys[0].Kind()).
Ancestor(submissionKey).
GetAll(ctx, &results)
if err != nil {
return http.StatusInternalServerError, err
}
json.NewEncoder(w).Encode(results)
case model.DiffTestResultKind:
var results model.DiffTestResults
_, err = datastore.NewQuery(keys[0].Kind()).
Ancestor(submissionKey).
GetAll(ctx, &results)
if err != nil {
return http.StatusInternalServerError, err
}
json.NewEncoder(w).Encode(results)
default:
w.Write([]byte("[]"))
}
return http.StatusOK, nil
}
示例13: CreateChallenge
// CreateChallenge will put a new entity of kind Challenge to Datastore.
func CreateChallenge(ctx context.Context, w http.ResponseWriter, r *http.Request) (status int, err error) {
if r.Method != "POST" {
return http.StatusMethodNotAllowed, nil
}
p, ok := passenger.FromContext(ctx)
if !ok {
return http.StatusUnauthorized, nil
}
var u model.User
if err := datastore.Get(ctx, p.User, &u); err != nil {
return http.StatusInternalServerError, err
}
if u.Company == nil {
return http.StatusUnauthorized, nil
}
var body = struct {
model.Assignment
Tasks []string
}{}
if err = json.NewDecoder(r.Body).Decode(&body); err != nil {
return http.StatusBadRequest, err
}
keys := make([]*datastore.Key, len(body.Tasks))
for i := range body.Tasks {
key, err := datastore.DecodeKey(body.Tasks[i])
if err != nil {
return http.StatusInternalServerError, err
}
keys[i] = key
}
challenge := model.Challenge{
Assignment: body.Assignment,
Resulter: int64(logic.Average),
Tasks: keys,
}
key, err := challenge.PutWithParent(ctx, u.Company)
if err != nil {
return http.StatusInternalServerError, err
}
json.NewEncoder(w).Encode(challenge.Key(key))
return http.StatusOK, nil
}
示例14: User
func User(ctx context.Context, w http.ResponseWriter, r *http.Request) (status int, err error) {
p, ok := passenger.FromContext(ctx)
if !ok {
return http.StatusUnauthorized, nil
}
switch r.Method {
case "POST":
return createUser(ctx, w, r)
case "GET":
return getUsers(p, ctx, w, r)
default:
return http.StatusMethodNotAllowed, nil
}
}
示例15: GetChallengesForProfile
func GetChallengesForProfile(ctx context.Context, w http.ResponseWriter, r *http.Request) (status int, err error) {
if r.Method != "GET" {
return http.StatusMethodNotAllowed, nil
}
_, ok := passenger.FromContext(ctx)
if !ok {
return http.StatusUnauthorized, nil
}
var profileKey *datastore.Key
if profileKey, err = datastore.DecodeKey(mux.Vars(r)["key"]); err != nil {
return http.StatusInternalServerError, err
}
q := model.NewQueryForResult().
Ancestor(profileKey)
if finished := r.URL.Query()["finished"]; len(finished) > 0 && finished[0] == "true" {
q = q.Filter("Finished >", time.Time{})
}
if order := r.URL.Query()["order"]; len(order) > 0 && order[0] != "" {
q = q.Order(order[0])
}
if limitQuery := r.URL.Query()["limit"]; len(limitQuery) > 0 {
if limit, err := strconv.Atoi(limitQuery[0]); err != nil {
return http.StatusInternalServerError, err
} else {
q = q.Limit(limit)
}
}
var results model.Results
if _, err = q.GetAll(ctx, &results); err != nil {
return http.StatusInternalServerError, err
}
challengeKeys := make([]*datastore.Key, len(results))
for i, val := range results {
challengeKeys[i] = val.Challenge
}
challenges := make(model.Challenges, len(challengeKeys))
if err = datastore.GetMulti(ctx, challengeKeys, challenges); err != nil {
return http.StatusInternalServerError, err
}
json.NewEncoder(w).Encode(challenges.Key(challengeKeys))
return
}