本文整理汇总了Golang中github.com/coduno/api/util/passenger.FromContext函数的典型用法代码示例。如果您正苦于以下问题:Golang FromContext函数的具体用法?Golang FromContext怎么用?Golang FromContext使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了FromContext函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的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
}
开发者ID:tudorgergely,项目名称:api,代码行数:35,代码来源:challenge.go
示例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
}
开发者ID:pbochis,项目名称:api,代码行数:28,代码来源:user.go
示例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
}
开发者ID:tudorgergely,项目名称:api,代码行数:34,代码来源:task.go
示例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
}
}
开发者ID:flowlo,项目名称:coduno-api,代码行数:25,代码来源:task.go
示例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
}
开发者ID:tudorgergely,项目名称:api,代码行数:34,代码来源:access_token.go
示例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.")
}
}
开发者ID:tudorgergely,项目名称:api,代码行数:28,代码来源:submission.go
示例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
}
开发者ID:pbochis,项目名称:api,代码行数:31,代码来源:task.go
示例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
}
开发者ID:flowlo,项目名称:coduno-api,代码行数:26,代码来源:cookie.go
示例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)
}
开发者ID:tudorgergely,项目名称:api,代码行数:30,代码来源:result.go
示例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
}
开发者ID:tudorgergely,项目名称:api,代码行数:29,代码来源:challenge.go
示例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
}
开发者ID:flowlo,项目名称:coduno-api,代码行数:25,代码来源:profile.go
示例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
}
开发者ID:flowlo,项目名称:coduno-api,代码行数:54,代码来源:submission.go
示例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
}
开发者ID:flowlo,项目名称:coduno-api,代码行数:52,代码来源:challenge.go
示例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
}
}
开发者ID:tudorgergely,项目名称:api,代码行数:14,代码来源:user.go
示例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
}
开发者ID:flowlo,项目名称:coduno-api,代码行数:50,代码来源:profile.go
示例16: GetResult
func GetResult(ctx context.Context, w http.ResponseWriter, r *http.Request) (int, error) {
if r.Method != "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
}
var u model.User
if err = datastore.Get(ctx, p.User, &u); err != nil {
return http.StatusInternalServerError, nil
}
if u.Company == nil && !util.HasParent(p.User, resultKey) {
return http.StatusUnauthorized, nil
}
if result.Finished.Equal(time.Time{}) {
if util.HasParent(p.User, resultKey) {
return createFinalResult(ctx, w, *result.Key(resultKey))
}
var challenge model.Challenge
if err := datastore.Get(ctx, result.Challenge, &challenge); err != nil {
return http.StatusInternalServerError, err
}
if u.Company != nil && result.Started.Add(challenge.Duration).Before(time.Now()) {
return createFinalResult(ctx, w, *result.Key(resultKey))
}
}
json.NewEncoder(w).Encode(result.Key(resultKey))
return http.StatusOK, nil
}
开发者ID:flowlo,项目名称:coduno-api,代码行数:45,代码来源:result.go
示例17: FinalSubmission
// FinalSubmission makes the last submission final.
func FinalSubmission(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 resultKey *datastore.Key
if resultKey, err = datastore.DecodeKey(mux.Vars(r)["resultKey"]); err != nil {
return http.StatusInternalServerError, err
}
if !util.HasParent(p.User, resultKey) {
return http.StatusBadRequest, errors.New("cannot submit answer for other users")
}
var index int
if index, err = strconv.Atoi(mux.Vars(r)["index"]); err != nil {
return http.StatusInternalServerError, err
}
if len(r.URL.Query()["submissionKey"]) == 0 {
return http.StatusOK, nil
}
var submissionKey *datastore.Key
if submissionKey, err = datastore.DecodeKey(r.URL.Query()["submissionKey"][0]); err != nil {
return http.StatusInternalServerError, err
}
var result model.Result
if err = datastore.Get(ctx, resultKey, &result); err != nil {
return http.StatusInternalServerError, err
}
result.FinalSubmissions[index] = submissionKey
if _, err = result.Put(ctx, resultKey); err != nil {
return http.StatusInternalServerError, err
}
w.Write([]byte("OK"))
return
}
开发者ID:flowlo,项目名称:coduno-api,代码行数:46,代码来源:submission.go
示例18: WhoAmI
func WhoAmI(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 user model.User
if err := datastore.Get(ctx, p.UserKey, &user); err != nil {
return http.StatusInternalServerError, err
}
json.NewEncoder(w).Encode(user.Key(p.UserKey))
return http.StatusOK, nil
}
开发者ID:tudorgergely,项目名称:api,代码行数:18,代码来源:user.go
示例19: GetUser
func GetUser(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 user model.User
if err = datastore.Get(ctx, userKey, &user); err != nil {
return http.StatusInternalServerError, err
}
json.NewEncoder(w).Encode(user.Key(userKey))
return
}
开发者ID:pbochis,项目名称:api,代码行数:19,代码来源:user.go
示例20: GetCompanyByUser
func GetCompanyByUser(ctx context.Context, w http.ResponseWriter, r *http.Request) (int, error) {
if !util.CheckMethod(r, "GET") {
return http.StatusMethodNotAllowed, nil
}
p, ok := passenger.FromContext(ctx)
if !ok {
return http.StatusUnauthorized, nil
}
key := p.UserKey.Parent()
if key == 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, key, &company); err != nil {
return http.StatusInternalServerError, err
}
json.NewEncoder(w).Encode(company.Key(key))
return http.StatusOK, nil
}
开发者ID:tudorgergely,项目名称:api,代码行数:21,代码来源:user.go
注:本文中的github.com/coduno/api/util/passenger.FromContext函数示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论