本文整理匯總了Golang中github.com/goji/context.FromC函數的典型用法代碼示例。如果您正苦於以下問題:Golang FromC函數的具體用法?Golang FromC怎麽用?Golang FromC使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了FromC函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的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)
}
示例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)
}
示例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)
}
示例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)
}
示例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, ®ion) {
return
}
region.ID = id
err = datastore.UpdateRegion(ctx, ®ion)
if err != nil {
log.FromContext(ctx).WithField("err", err).Error("Error updating region")
w.WriteHeader(http.StatusNotFound)
return
}
json.NewEncoder(w).Encode(®ion)
}
示例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)
}
示例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})
}
示例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
}
示例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)
}
示例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)
}
示例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)
}
示例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)
}
示例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)
}
示例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()
}
示例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)
}