本文整理匯總了Golang中github.com/MG-RAST/golib/goweb.Context類的典型用法代碼示例。如果您正苦於以下問題:Golang Context類的具體用法?Golang Context怎麽用?Golang Context使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了Context類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: ReadMany
// GET: /user
func (cr *UserController) ReadMany(cx *goweb.Context) {
// Log Request and check for Auth
LogRequest(cx.Request)
u, err := request.Authenticate(cx.Request)
if err != nil {
if err.Error() == e.NoAuth || err.Error() == e.UnAuth {
cx.RespondWithError(http.StatusUnauthorized)
return
} else {
logger.Error("[email protected]_Read: " + err.Error())
cx.RespondWithError(http.StatusInternalServerError)
return
}
}
if u.Admin {
users := user.Users{}
user.AdminGet(&users)
if len(users) > 0 {
cx.RespondWithData(users)
return
} else {
cx.RespondWithNotFound()
return
}
} else {
cx.RespondWithError(http.StatusUnauthorized)
return
}
}
示例2: ResourceDescription
func ResourceDescription(cx *goweb.Context) {
LogRequest(cx.Request)
r := resource{
R: []string{},
F: core.JobInfoIndexes,
U: apiUrl(cx) + "/",
D: siteUrl(cx) + "/",
Title: conf.TITLE,
C: conf.ADMIN_EMAIL,
I: "AWE",
T: core.Service,
S: core.QMgr.QueueStatus(),
V: conf.VERSION,
Time: time.Now().String(),
GitCommitHash: conf.GIT_COMMIT_HASH,
}
if core.Service == "server" {
r.R = []string{"job", "work", "client", "queue", "awf", "event"}
} else if core.Service == "proxy" {
r.R = []string{"client", "work"}
}
cx.WriteResponse(r, 200)
return
}
示例3: Read
// GET: /client/{id}
func (cr *ClientController) Read(id string, cx *goweb.Context) {
// Gather query params
query := &Query{Li: cx.Request.URL.Query()}
if query.Has("heartbeat") { //handle heartbeat
hbmsg, err := core.QMgr.ClientHeartBeat(id)
if err != nil {
cx.RespondWithErrorMessage(err.Error(), http.StatusBadRequest)
} else {
cx.RespondWithData(hbmsg)
}
return
}
LogRequest(cx.Request) //skip heartbeat in access log
client, err := core.QMgr.GetClient(id)
if err != nil {
if err.Error() == e.ClientNotFound {
cx.RespondWithErrorMessage(e.ClientNotFound, http.StatusBadRequest)
} else {
logger.Error("Error in GET client:" + err.Error())
cx.RespondWithError(http.StatusBadRequest)
}
return
}
cx.RespondWithData(client)
}
示例4: DeleteMany
// DELETE: /job?suspend, /job?zombie
func (cr *JobController) DeleteMany(cx *goweb.Context) {
LogRequest(cx.Request)
// Try to authenticate user.
u, err := request.Authenticate(cx.Request)
if err != nil && err.Error() != e.NoAuth {
cx.RespondWithErrorMessage(err.Error(), http.StatusUnauthorized)
return
}
// If no auth was provided, and anonymous delete is allowed, use the public user
if u == nil {
if conf.ANON_DELETE == true {
u = &user.User{Uuid: "public"}
} else {
cx.RespondWithErrorMessage(e.NoAuth, http.StatusUnauthorized)
return
}
}
// Gather query params
query := &Query{Li: cx.Request.URL.Query()}
if query.Has("suspend") {
num := core.QMgr.DeleteSuspendedJobsByUser(u)
cx.RespondWithData(fmt.Sprintf("deleted %d suspended jobs", num))
} else if query.Has("zombie") {
num := core.QMgr.DeleteZombieJobsByUser(u)
cx.RespondWithData(fmt.Sprintf("deleted %d zombie jobs", num))
} else {
cx.RespondWithError(http.StatusNotImplemented)
}
return
}
示例5: UpdateMany
// PUT: /client
func (cr *ClientController) UpdateMany(cx *goweb.Context) {
LogRequest(cx.Request)
// Try to authenticate user.
u, err := request.Authenticate(cx.Request)
if err != nil && err.Error() != e.NoAuth {
cx.RespondWithErrorMessage(err.Error(), http.StatusUnauthorized)
return
}
// If no auth was provided, and anonymous read is allowed, use the public user
if u == nil {
if conf.ANON_WRITE == true {
u = &user.User{Uuid: "public"}
} else {
cx.RespondWithErrorMessage(e.NoAuth, http.StatusUnauthorized)
return
}
}
// Gather query params
query := &Query{Li: cx.Request.URL.Query()}
if query.Has("resumeall") { //resume the suspended client
num := core.QMgr.ResumeSuspendedClientsByUser(u)
cx.RespondWithData(fmt.Sprintf("%d suspended clients resumed", num))
return
}
if query.Has("suspendall") { //resume the suspended client
num := core.QMgr.SuspendAllClientsByUser(u)
cx.RespondWithData(fmt.Sprintf("%d clients suspended", num))
return
}
cx.RespondWithError(http.StatusNotImplemented)
return
}
示例6: Update
// PUT: /work/{id} -> status update
func (cr *WorkController) Update(id string, cx *goweb.Context) {
// Log Request and check for Auth
LogRequest(cx.Request)
// Gather query params
query := &Query{Li: cx.Request.URL.Query()}
if query.Has("status") && query.Has("client") { //notify execution result: "done" or "fail"
notice := core.Notice{WorkId: id, Status: query.Value("status"), ClientId: query.Value("client"), Notes: ""}
if query.Has("report") { // if "report" is specified in query, parse performance statistics or errlog
if _, files, err := ParseMultipartForm(cx.Request); err == nil {
if _, ok := files["perf"]; ok {
core.QMgr.FinalizeWorkPerf(id, files["perf"].Path)
}
if _, ok := files["notes"]; ok {
if notes, err := ioutil.ReadFile(files["notes"].Path); err == nil {
notice.Notes = string(notes)
}
}
}
}
core.QMgr.NotifyWorkStatus(notice)
}
cx.RespondWithData("ok")
return
}
示例7: UpdateMany
// PUT: /logger
func (cr *LoggerController) UpdateMany(cx *goweb.Context) {
LogRequest(cx.Request)
// Try to authenticate user.
u, err := request.Authenticate(cx.Request)
if err != nil && err.Error() != e.NoAuth {
cx.RespondWithErrorMessage(err.Error(), http.StatusUnauthorized)
return
}
// must be admin user
if u == nil || u.Admin == false {
cx.RespondWithErrorMessage(e.NoAuth, http.StatusUnauthorized)
return
}
// Gather query params
query := &Query{Li: cx.Request.URL.Query()}
// currently can only reset debug level
if query.Has("debug") {
levelStr := query.Value("debug")
levelInt, err := strconv.Atoi(levelStr)
if err != nil {
cx.RespondWithErrorMessage("invalid debug level: "+err.Error(), http.StatusBadRequest)
}
conf.DEBUG_LEVEL = levelInt
logger.Event(event.DEBUG_LEVEL, "level="+levelStr+";user="+u.Username)
cx.RespondWithData(map[string]int{"debuglevel": conf.DEBUG_LEVEL})
return
}
cx.RespondWithError(http.StatusNotImplemented)
return
}
示例8: UpdateMany
// PUT: /queue
func (cr *QueueController) UpdateMany(cx *goweb.Context) {
LogRequest(cx.Request)
// Try to authenticate user.
u, err := request.Authenticate(cx.Request)
if err != nil && err.Error() != e.NoAuth {
cx.RespondWithErrorMessage(err.Error(), http.StatusUnauthorized)
return
}
// must be admin user
if u == nil || u.Admin == false {
cx.RespondWithErrorMessage(e.NoAuth, http.StatusUnauthorized)
return
}
// Gather query params
query := &Query{Li: cx.Request.URL.Query()}
if query.Has("resume") {
core.QMgr.ResumeQueue()
logger.Event(event.QUEUE_RESUME, "user="+u.Username)
cx.RespondWithData("work queue resumed")
return
}
if query.Has("suspend") {
core.QMgr.SuspendQueue()
logger.Event(event.QUEUE_SUSPEND, "user="+u.Username)
cx.RespondWithData("work queue suspended")
return
}
cx.RespondWithErrorMessage("requested queue operation not supported", http.StatusBadRequest)
return
}
示例9: AuthError
func AuthError(err error, cx *goweb.Context) {
if err.Error() == e.InvalidAuth {
cx.RespondWithErrorMessage("Invalid authorization header or content", http.StatusBadRequest)
return
}
logger.Error("Error at Auth: " + err.Error())
cx.RespondWithError(http.StatusInternalServerError)
return
}
示例10: RespondPrivateEnvInHeader
func RespondPrivateEnvInHeader(cx *goweb.Context, Envs map[string]string) (err error) {
env_stream, err := json.Marshal(Envs)
if err != nil {
return err
}
cx.ResponseWriter.Header().Set("Privateenv", string(env_stream[:]))
cx.Respond(nil, http.StatusOK, nil, cx)
return
}
示例11: Delete
// DELETE: /job/{id}
func (cr *JobController) Delete(id string, cx *goweb.Context) {
LogRequest(cx.Request)
if err := core.QMgr.DeleteJob(id); err != nil {
cx.RespondWithErrorMessage("fail to delete job: "+id, http.StatusBadRequest)
return
}
cx.RespondWithData("job deleted: " + id)
return
}
示例12: ReadMany
// GET: /queue
// get status from queue manager
func (cr *QueueController) ReadMany(cx *goweb.Context) {
LogRequest(cx.Request)
// Gather query params
// query := &Query{list: cx.Request.URL.Query()}
msg := core.QMgr.ShowStatus()
cx.RespondWithData(msg)
return
}
示例13: UpdateMany
// PUT: /job
func (cr *JobController) UpdateMany(cx *goweb.Context) {
LogRequest(cx.Request)
// Gather query params
query := &Query{Li: cx.Request.URL.Query()}
if query.Has("resumeall") { //resume the suspended job
num := core.QMgr.ResumeSuspendedJobs()
cx.RespondWithData(fmt.Sprintf("%d suspended jobs resumed", num))
return
}
cx.RespondWithError(http.StatusNotImplemented)
}
示例14: Read
// GET: /awf/{name}
// get a workflow by name, read-only
func (cr *AwfController) Read(id string, cx *goweb.Context) {
LogRequest(cx.Request)
// Load workunit by id
workflow, err := core.AwfMgr.GetWorkflow(id)
if err != nil {
cx.RespondWithErrorMessage(err.Error(), http.StatusBadRequest)
return
}
// Base case respond with workunit in json
cx.RespondWithData(workflow)
return
}
示例15: ReadMany
// GET: /queue
// get status from queue manager
func (cr *QueueController) ReadMany(cx *goweb.Context) {
LogRequest(cx.Request)
// Gather query params
// query := &Query{list: cx.Request.URL.Query()}
msg, err := json.Marshal(core.QMgr.ShowStatus())
if err == nil {
cx.RespondWithData(string(msg))
} else {
cx.RespondWithData("Err")
}
}