• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Golang context.FromC函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Golang中github.com/goji/context.FromC函数的典型用法代码示例。如果您正苦于以下问题:Golang FromC函数的具体用法?Golang FromC怎么用?Golang FromC使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



在下文中一共展示了FromC函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。

示例1: DelWorker

// Delete accepts a request to delete a worker
// from the pool.
//
//     DELETE /sudo/api/workers/:id
//
func DelWorker(c web.C, w http.ResponseWriter, r *http.Request) {
	ctx := context.FromC(c)
	pool := pool.FromContext(ctx)
	uuid := c.URLParams["id"]

	server, err := datastore.GetServer(ctx, uuid)
	if err != nil {
		w.WriteHeader(http.StatusNotFound)
		return
	}

	err = datastore.DelServer(ctx, server)
	if err != nil {
		w.WriteHeader(http.StatusInternalServerError)
		return
	}

	for _, worker := range pool.List() {
		if worker.(*docker.Docker).UUID == uuid {
			pool.Deallocate(worker)
			w.WriteHeader(http.StatusNoContent)
			return
		}
	}
	w.WriteHeader(http.StatusNotFound)
}
开发者ID:drone,项目名称:drone-dart,代码行数:31,代码来源:worker.go


示例2: CreatePerson

// CreatePerson accepts a request to add a new person.
//
//     POST /api/people
//
func CreatePerson(c web.C, w http.ResponseWriter, r *http.Request) {
	var (
		ctx = context.FromC(c)
	)

	// Unmarshal the person from the payload
	defer r.Body.Close()
	in := struct {
		Name string `json:"name"`
	}{}
	if err := json.NewDecoder(r.Body).Decode(&in); err != nil {
		http.Error(w, err.Error(), http.StatusBadRequest)
		return
	}

	// Validate input
	if len(in.Name) < 1 {
		http.Error(w, "no name given", http.StatusBadRequest)
		return
	}

	// Create our 'normal' model.
	person := &model.Person{Name: in.Name}
	err := datastore.CreatePerson(ctx, person)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	w.WriteHeader(http.StatusCreated)
	json.NewEncoder(w).Encode(person)
}
开发者ID:jmptrader,项目名称:go-webapp-skeleton,代码行数:36,代码来源:person.go


示例3: PostWorker

// PostWorker accepts a request to allocate a new
// worker to the pool.
//
//     POST /api/workers
//
func PostWorker(c web.C, w http.ResponseWriter, r *http.Request) {
	ctx := context.FromC(c)
	pool := pool.FromContext(ctx)
	node := r.FormValue("address")
	pool.Allocate(docker.NewHost(node))
	w.WriteHeader(http.StatusOK)
}
开发者ID:carnivalmobile,项目名称:drone,代码行数:12,代码来源:worker.go


示例4: GetCC

// GetCC accepts a request to retrieve the latest build
// status for the given repository from the datastore and
// in CCTray XML format.
//
//     GET /api/badge/:host/:owner/:name/cc.xml
//
func GetCC(c web.C, w http.ResponseWriter, r *http.Request) {
	var ctx = context.FromC(c)
	var (
		host  = c.URLParams["host"]
		owner = c.URLParams["owner"]
		name  = c.URLParams["name"]
	)

	w.Header().Set("Content-Type", "application/xml")

	repo, err := datastore.GetRepoName(ctx, host, owner, name)
	if err != nil {
		w.WriteHeader(http.StatusNotFound)
		return
	}
	commits, err := datastore.GetCommitList(ctx, repo, 1, 0)
	if err != nil || len(commits) == 0 {
		w.WriteHeader(http.StatusNotFound)
		return
	}

	var link = httputil.GetURL(r) + "/" + repo.Host + "/" + repo.Owner + "/" + repo.Name
	var cc = model.NewCC(repo, commits[0], link)
	xml.NewEncoder(w).Encode(cc)
}
开发者ID:zankard,项目名称:drone,代码行数:31,代码来源:badge.go


示例5: PutRegion

// PutRegion accepts a request to retrieve information about a particular region.
//
//     PUT /api/regions/:region
//
func PutRegion(c web.C, w http.ResponseWriter, r *http.Request) {
	var (
		ctx    = context.FromC(c)
		idStr  = c.URLParams["region"]
		region model.Region
	)

	id, err := strconv.ParseInt(idStr, 10, 64)
	if err != nil {
		w.WriteHeader(http.StatusBadRequest)
		return
	}

	if !regionFromRequest(c, w, r, &region) {
		return
	}
	region.ID = id

	err = datastore.UpdateRegion(ctx, &region)
	if err != nil {
		log.FromContext(ctx).WithField("err", err).Error("Error updating region")
		w.WriteHeader(http.StatusNotFound)
		return
	}

	json.NewEncoder(w).Encode(&region)
}
开发者ID:andrew-d,项目名称:flypaper,代码行数:31,代码来源:region.go


示例6: SetRepo

// SetRepo is a middleware function that retrieves
// the repository and stores in the context.
func SetRepo(c *web.C, h http.Handler) http.Handler {
	fn := func(w http.ResponseWriter, r *http.Request) {
		var (
			ctx   = context.FromC(*c)
			host  = c.URLParams["host"]
			owner = c.URLParams["owner"]
			name  = c.URLParams["name"]
			user  = ToUser(c)
		)

		repo, err := datastore.GetRepoName(ctx, host, owner, name)
		switch {
		case err != nil && user == nil:
			w.WriteHeader(http.StatusUnauthorized)
			return
		case err != nil && user != nil:
			w.WriteHeader(http.StatusNotFound)
			return
		}
		role, _ := datastore.GetPerm(ctx, user, repo)
		RepoToC(c, repo)
		RoleToC(c, role)
		h.ServeHTTP(w, r)
	}
	return http.HandlerFunc(fn)
}
开发者ID:carnivalmobile,项目名称:drone,代码行数:28,代码来源:repo.go


示例7: GetBuildPage

// GetBuildPage accepts a request to retrieve the build
// output page for the package version, channel and SDK
// from the datastore in JSON format.
//
// If the SDK is not provided, the system will lookup
// the latest SDK version for this package.
//
//     GET /:name/:number/:channel/:sdk
//
func GetBuildPage(c web.C, w http.ResponseWriter, r *http.Request) {
	ctx := context.FromC(c)
	name := c.URLParams["name"]
	number := c.URLParams["number"]
	channel := c.URLParams["channel"]
	sdk := c.URLParams["sdk"]

	// If no SDK is provided we should use the most recent
	// SDK number associated with the Package version.
	var build *resource.Build
	var err error
	if len(sdk) == 0 {
		build, err = datastore.GetBuildLatest(ctx, name, number, channel)
	} else {
		build, err = datastore.GetBuild(ctx, name, number, channel, sdk)
	}

	// If the error is not nil then we can
	// display some sort of NotFound page.
	if err != nil {
		http.Error(w, "Not Found", http.StatusNotFound)
		return
	}

	BuildTempl.Execute(w, struct {
		Build *resource.Build
		Error error
	}{build, err})
}
开发者ID:drone,项目名称:drone-dart,代码行数:38,代码来源:pages.go


示例8: PostUserSync

// PostUserSync accepts a request to post user sync
//
//     POST /api/user/sync
//
func PostUserSync(c web.C, w http.ResponseWriter, r *http.Request) {
	var ctx = context.FromC(c)
	var user = ToUser(c)
	if user == nil {
		w.WriteHeader(http.StatusUnauthorized)
		return
	}

	var remote = remote.Lookup(user.Remote)
	if remote == nil {
		w.WriteHeader(http.StatusNotFound)
		return
	}

	if user.Syncing {
		w.WriteHeader(http.StatusConflict)
		return
	}

	user.Syncing = true
	if err := datastore.PutUser(ctx, user); err != nil {
		w.WriteHeader(http.StatusNotFound)
		return
	}

	go sync.SyncUser(ctx, user, remote)
	w.WriteHeader(http.StatusNoContent)
	return
}
开发者ID:carnivalmobile,项目名称:drone,代码行数:33,代码来源:user.go


示例9: PutUser

// PutUser accepts a request to update the currently
// authenticated User profile.
//
//     PUT /api/user
//
func PutUser(c web.C, w http.ResponseWriter, r *http.Request) {
	var ctx = context.FromC(c)
	var user = ToUser(c)
	if user == nil {
		w.WriteHeader(http.StatusUnauthorized)
		return
	}

	// unmarshal the repository from the payload
	defer r.Body.Close()
	in := model.User{}
	if err := json.NewDecoder(r.Body).Decode(&in); err != nil {
		w.WriteHeader(http.StatusBadRequest)
		return
	}

	// update the user email
	if len(in.Email) != 0 {
		user.SetEmail(in.Email)
	}
	// update the user full name
	if len(in.Name) != 0 {
		user.Name = in.Name
	}

	// update the database
	if err := datastore.PutUser(ctx, user); err != nil {
		w.WriteHeader(http.StatusInternalServerError)
		return
	}

	json.NewEncoder(w).Encode(user)
}
开发者ID:ngpestelos,项目名称:drone,代码行数:38,代码来源:user.go


示例10: PostWorker

// PostWorker accepts a request to allocate a new
// worker to the pool.
//
//     POST /sudo/api/workers
//
func PostWorker(c web.C, w http.ResponseWriter, r *http.Request) {
	ctx := context.FromC(c)
	pool := pool.FromContext(ctx)
	server := resource.Server{}

	// read the worker data from the body
	defer r.Body.Close()
	if err := json.NewDecoder(r.Body).Decode(&server); err != nil {
		println(err.Error())
		w.WriteHeader(http.StatusBadRequest)
		return
	}

	// add the worker to the database
	err := datastore.PutServer(ctx, &server)
	if err != nil {
		println(err.Error())
		w.WriteHeader(http.StatusInternalServerError)
		return
	}

	// create a new worker from the Docker client
	client, err := docker.NewCert(server.Host, []byte(server.Cert), []byte(server.Key))
	if err != nil {
		w.WriteHeader(http.StatusInternalServerError)
		return
	}

	// append user-friendly data to the host
	client.Host = client.Host

	pool.Allocate(client)
	w.WriteHeader(http.StatusOK)
}
开发者ID:drone,项目名称:drone-dart,代码行数:39,代码来源:worker.go


示例11: PostCommit

// PostHook accepts a post-commit hook and parses the payload
// in order to trigger a build. The payload is specified to the
// remote system (ie GitHub) and will therefore get parsed by
// the appropriate remote plugin.
//
//     POST /api/repos/{host}/{owner}/{name}/branches/{branch}/commits/{commit}
//
func PostCommit(c web.C, w http.ResponseWriter, r *http.Request) {
	var ctx = context.FromC(c)
	var (
		branch = c.URLParams["branch"]
		hash   = c.URLParams["commit"]
		host   = c.URLParams["host"]
		repo   = ToRepo(c)
		remote = remote.Lookup(host)
	)

	commit, err := datastore.GetCommitSha(ctx, repo, branch, hash)
	if err != nil {
		w.WriteHeader(http.StatusNotFound)
		return
	}

	if commit.Status == model.StatusStarted ||
		commit.Status == model.StatusEnqueue {
		w.WriteHeader(http.StatusConflict)
		return
	}

	commit.Status = model.StatusEnqueue
	commit.Started = 0
	commit.Finished = 0
	commit.Duration = 0
	if err := datastore.PutCommit(ctx, commit); err != nil {
		w.WriteHeader(http.StatusInternalServerError)
		return
	}

	owner, err := datastore.GetUser(ctx, repo.UserID)
	if err != nil {
		w.WriteHeader(http.StatusBadRequest)
		return
	}

	// Request a new token and update
	user_token, err := remote.GetToken(owner)
	if user_token != nil {
		owner.Access = user_token.AccessToken
		owner.Secret = user_token.RefreshToken
		owner.TokenExpiry = user_token.Expiry
		datastore.PutUser(ctx, owner)
	} else if err != nil {
		w.WriteHeader(http.StatusBadRequest)
		return
	}

	// drop the items on the queue
	go worker.Do(ctx, &worker.Work{
		User:   owner,
		Repo:   repo,
		Commit: commit,
		Host:   httputil.GetURL(r),
	})

	w.WriteHeader(http.StatusOK)
}
开发者ID:quartethealth,项目名称:drone,代码行数:66,代码来源:commit.go


示例12: WsUser

// WsUser will upgrade the connection to a Websocket and will stream
// all events to the browser pertinent to the authenticated user. If the user
// is not authenticated, only public events are streamed.
func WsUser(c web.C, w http.ResponseWriter, r *http.Request) {
	var ctx = context.FromC(c)
	var user = ToUser(c)

	// upgrade the websocket
	ws, err := upgrader.Upgrade(w, r, nil)
	if err != nil {
		w.WriteHeader(http.StatusBadRequest)
		return
	}

	// register a channel for global events
	channel := pubsub.Register(ctx, "_global")
	sub := channel.Subscribe()

	ticker := time.NewTicker(pingPeriod)
	defer func() {
		ticker.Stop()
		sub.Close()
		ws.Close()
	}()

	go func() {
		for {
			select {
			case msg := <-sub.Read():
				work, ok := msg.(*worker.Work)
				if !ok {
					break
				}

				// user must have read access to the repository
				// in order to pass this message along
				if role, err := datastore.GetPerm(ctx, user, work.Repo); err != nil || role.Read == false {
					break
				}

				ws.SetWriteDeadline(time.Now().Add(writeWait))
				err := ws.WriteJSON(work)
				if err != nil {
					ws.Close()
					return
				}
			case <-sub.CloseNotify():
				ws.Close()
				return
			case <-ticker.C:
				ws.SetWriteDeadline(time.Now().Add(writeWait))
				err := ws.WriteMessage(websocket.PingMessage, []byte{})
				if err != nil {
					ws.Close()
					return
				}
			}
		}
	}()

	readWebsocket(ws)
}
开发者ID:ngpestelos,项目名称:drone,代码行数:62,代码来源:ws.go


示例13: PostUser

// PostUser accepts a request to create a new user in the
// system. The created user account is returned in JSON
// format if successful.
//
//     POST /api/users/:host/:login
//
func PostUser(c web.C, w http.ResponseWriter, r *http.Request) {
	var ctx = context.FromC(c)
	var (
		host  = c.URLParams["host"]
		login = c.URLParams["login"]
	)
	var remote = remote.Lookup(host)
	if remote == nil {
		w.WriteHeader(http.StatusNotFound)
		return
	}

	// not sure I love this, but POST now flexibly accepts the oauth_token for
	// GitHub as either application/x-www-form-urlencoded OR as applcation/json
	// with this format:
	//    { "oauth_token": "...." }
	var oauthToken string
	switch cnttype := r.Header.Get("Content-Type"); cnttype {
	case "application/json":
		var out interface{}
		err := json.NewDecoder(r.Body).Decode(&out)
		if err == nil {
			if val, ok := out.(map[string]interface{})["oauth_token"]; ok {
				oauthToken = val.(string)
			}
		}
	case "application/x-www-form-urlencoded":
		oauthToken = r.PostForm.Get("oauth_token")
	default:
		// we don't recognize the content-type, but it isn't worth it
		// to error here
		log.Printf("PostUser(%s) Unknown 'Content-Type': %s)", r.URL, cnttype)
	}
	account := model.NewUser(host, login, "", oauthToken)

	if err := datastore.PostUser(ctx, account); err != nil {
		w.WriteHeader(http.StatusBadRequest)
		return
	}

	// borrowed this concept from login.go. upon first creation we
	// may trying syncing the user's repositories.
	account.Syncing = account.IsStale()
	if err := datastore.PutUser(ctx, account); err != nil {
		log.Println(err)
		w.WriteHeader(http.StatusBadRequest)
		return
	}

	if account.Syncing {
		log.Println("sync user account.", account.Login)

		// sync inside a goroutine
		go sync.SyncUser(ctx, account, remote)
	}

	json.NewEncoder(w).Encode(account)
}
开发者ID:Intellifora,项目名称:drone,代码行数:64,代码来源:users.go


示例14: main

func main() {
	extdirect.Provider.RegisterAction(reflect.TypeOf(Db{}))
	goji.Get(extdirect.Provider.URL, extdirect.API(extdirect.Provider))
	goji.Post(extdirect.Provider.URL, func(c web.C, w http.ResponseWriter, r *http.Request) {
		extdirect.ActionsHandlerCtx(extdirect.Provider)(gcontext.FromC(c), w, r)
	})
	goji.Use(gojistatic.Static("public", gojistatic.StaticOptions{SkipLogging: true}))
	goji.Serve()
}
开发者ID:nbgo,项目名称:extdirect,代码行数:9,代码来源:main.go


示例15: GetFeed

// GetFeed accepts a request to retrieve a feed
// of the latest builds in JSON format.
//
//     GET /api/feed
//
func GetFeed(c web.C, w http.ResponseWriter, r *http.Request) {
	ctx := context.FromC(c)
	pkg, err := datastore.GetFeed(ctx)
	if err != nil {
		w.WriteHeader(http.StatusNotFound)
		return
	}
	json.NewEncoder(w).Encode(pkg)
}
开发者ID:Guoshusheng,项目名称:drone-dart,代码行数:14,代码来源:feed.go


示例16: GetUserList

// GetUserList accepts a request to retrieve all users
// from the datastore and return encoded in JSON format.
//
//     GET /api/users
//
func GetUserList(c web.C, w http.ResponseWriter, r *http.Request) {
	var ctx = context.FromC(c)

	users, err := datastore.GetUserList(ctx)
	if err != nil {
		w.WriteHeader(http.StatusInternalServerError)
		return
	}
	json.NewEncoder(w).Encode(users)
}
开发者ID:Intellifora,项目名称:drone,代码行数:15,代码来源:users.go


示例17: GetChannel

// GetChannel accepts a request to retrieve the latest
// SDK version and revision for the specified channel.
//
//     GET /api/channel/:name
//
func GetChannel(c web.C, w http.ResponseWriter, r *http.Request) {
	ctx := context.FromC(c)
	name := c.URLParams["channel"]
	channel, err := datastore.GetChannel(ctx, name)
	if err != nil {
		w.WriteHeader(http.StatusNotFound)
		return
	}
	json.NewEncoder(w).Encode(channel)
}
开发者ID:Guoshusheng,项目名称:drone-dart,代码行数:15,代码来源:channel.go


示例18: SetUser

// SetUser is a middleware function that retrieves
// the currently authenticated user from the request
// and stores in the context.
func SetUser(c *web.C, h http.Handler) http.Handler {
	fn := func(w http.ResponseWriter, r *http.Request) {
		var ctx = context.FromC(*c)
		var user = session.GetUser(ctx, r)
		if user != nil && user.ID != 0 {
			UserToC(c, user)
		}
		h.ServeHTTP(w, r)
	}
	return http.HandlerFunc(fn)
}
开发者ID:quartethealth,项目名称:drone,代码行数:14,代码来源:user.go


示例19: GetCommitList

// GetCommitList accepts a request to retrieve a list
// of recent commits by Repo, and retur in JSON format.
//
//     GET /api/repos/:host/:owner/:name/commits
//
func GetCommitList(c web.C, w http.ResponseWriter, r *http.Request) {
	var ctx = context.FromC(c)
	var repo = ToRepo(c)

	commits, err := datastore.GetCommitList(ctx, repo)
	if err != nil {
		w.WriteHeader(http.StatusNotFound)
		return
	}

	json.NewEncoder(w).Encode(commits)
}
开发者ID:carnivalmobile,项目名称:drone,代码行数:17,代码来源:commit.go


示例20: GetBuildLatest

// GetBuildLatest accepts a request to retrieve the build
// details for the package version, channel and latest SDK
// from the datastore in JSON format.
//
//     GET /api/packages/:name/:number/channel/:channel
//
func GetBuildLatest(c web.C, w http.ResponseWriter, r *http.Request) {
	ctx := context.FromC(c)
	name := c.URLParams["name"]
	number := c.URLParams["number"]
	channel := c.URLParams["channel"]

	build, err := datastore.GetBuildLatest(ctx, name, number, channel)
	if err != nil {
		w.WriteHeader(http.StatusNotFound)
		return
	}
	json.NewEncoder(w).Encode(build)
}
开发者ID:drone,项目名称:drone-dart,代码行数:19,代码来源:build.go



注:本文中的github.com/goji/context.FromC函数示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Golang bolt.Open函数代码示例发布时间:2022-05-23
下一篇:
Golang tracelog.Trace函数代码示例发布时间:2022-05-23
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap