本文整理匯總了Golang中github.com/MG-RAST/AWE/vendor/github.com/MG-RAST/golib/goweb.Context.RespondWithData方法的典型用法代碼示例。如果您正苦於以下問題:Golang Context.RespondWithData方法的具體用法?Golang Context.RespondWithData怎麽用?Golang Context.RespondWithData使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類github.com/MG-RAST/AWE/vendor/github.com/MG-RAST/golib/goweb.Context
的用法示例。
在下文中一共展示了Context.RespondWithData方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: Delete
// DELETE: /client/{id}
func (cr *ClientController) Delete(id string, 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_DELETE == true {
u = &user.User{Uuid: "public"}
} else {
cx.RespondWithErrorMessage(e.NoAuth, http.StatusUnauthorized)
return
}
}
if err := core.QMgr.DeleteClientByUser(id, u); err != nil {
cx.RespondWithErrorMessage(err.Error(), http.StatusBadRequest)
} else {
cx.RespondWithData("client deleted")
}
return
}
示例2: 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
}
示例3: Delete
// DELETE: /job/{id}
func (cr *JobController) Delete(id string, 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)
}
// 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
}
}
if err = core.QMgr.DeleteJobByUser(id, u); err != nil {
if err == mgo.ErrNotFound {
cx.RespondWithNotFound()
return
} else if err.Error() == e.UnAuth {
cx.RespondWithErrorMessage(e.UnAuth, http.StatusUnauthorized)
return
} else {
cx.RespondWithErrorMessage("fail to delete job: "+id, http.StatusBadRequest)
return
}
}
cx.RespondWithData("job deleted: " + id)
return
}
示例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: 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
cg, err := request.AuthenticateClientGroup(cx.Request)
if err != nil {
if err.Error() == e.NoAuth || err.Error() == e.UnAuth || err.Error() == e.InvalidAuth {
if conf.CLIENT_AUTH_REQ == true {
cx.RespondWithError(http.StatusUnauthorized)
return
}
} else {
logger.Error("[email protected]: " + err.Error())
cx.RespondWithError(http.StatusInternalServerError)
return
}
}
hbmsg, err := core.QMgr.ClientHeartBeat(id, cg)
if err != nil {
cx.RespondWithErrorMessage(err.Error(), http.StatusBadRequest)
} else {
cx.RespondWithData(hbmsg)
}
return
}
LogRequest(cx.Request) //skip heartbeat in access log
// 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_READ == true {
u = &user.User{Uuid: "public"}
} else {
cx.RespondWithErrorMessage(e.NoAuth, http.StatusUnauthorized)
return
}
}
client, err := core.QMgr.GetClientByUser(id, u)
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)
return
}
示例6: CreateWithId
// POST: /cgroup/{name}
func (cr *ClientGroupController) CreateWithId(name string, 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 ANON_CG_WRITE is true, use the public user.
// Otherwise if no auth was provided or user is not an admin, and ANON_CG_WRITE is false, throw an error.
// Otherwise, proceed with creation of the clientgroup with the user.
if u == nil && conf.ANON_CG_WRITE == true {
u = &user.User{Uuid: "public"}
} else if u == nil || !u.Admin {
if conf.ANON_CG_WRITE == false {
cx.RespondWithErrorMessage(e.UnAuth, http.StatusUnauthorized)
return
}
}
cg, err := core.CreateClientGroup(name, u)
if err != nil {
cx.RespondWithErrorMessage(err.Error(), http.StatusBadRequest)
return
}
cx.RespondWithData(cg)
return
}
示例7: 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
}
示例8: 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
}
示例9: Read
// GET: /cgroup/{id}
func (cr *ClientGroupController) Read(id string, 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 ANON_CG_READ is true, use the public user.
// Otherwise if no auth was provided, throw an error.
// Otherwise, proceed with retrieval of the clientgroup using the user.
if u == nil {
if conf.ANON_CG_READ == true {
u = &user.User{Uuid: "public"}
} else {
cx.RespondWithErrorMessage(e.UnAuth, http.StatusUnauthorized)
return
}
}
// Load clientgroup by id
cg, err := core.LoadClientGroup(id)
if err != nil {
if err == mgo.ErrNotFound {
cx.RespondWithNotFound()
} else {
// In theory the db connection could be lost between
// checking user and load but seems unlikely.
cx.RespondWithErrorMessage("clientgroup id not found:"+id, http.StatusBadRequest)
}
return
}
// User must have read permissions on clientgroup or be clientgroup owner or be an admin or the clientgroup is publicly readable.
// The other possibility is that public read of clientgroups is enabled and the clientgroup is publicly readable.
rights := cg.Acl.Check(u.Uuid)
public_rights := cg.Acl.Check("public")
if (u.Uuid != "public" && (cg.Acl.Owner == u.Uuid || rights["read"] == true || u.Admin == true || public_rights["read"] == true)) ||
(u.Uuid == "public" && conf.ANON_CG_READ == true && public_rights["read"] == true) {
cx.RespondWithData(cg)
return
}
cx.RespondWithErrorMessage(e.UnAuth, http.StatusUnauthorized)
return
}
示例10: Create
// POST: /client - register a new client
func (cr *ClientController) Create(cx *goweb.Context) {
// Log Request and check for Auth
LogRequest(cx.Request)
cg, err := request.AuthenticateClientGroup(cx.Request)
if err != nil {
if err.Error() == e.NoAuth || err.Error() == e.UnAuth || err.Error() == e.InvalidAuth {
if conf.CLIENT_AUTH_REQ == true {
cx.RespondWithError(http.StatusUnauthorized)
return
}
} else {
logger.Error("[email protected]: " + err.Error())
cx.RespondWithError(http.StatusInternalServerError)
return
}
}
// Parse uploaded form
_, files, err := ParseMultipartForm(cx.Request)
if err != nil {
if err.Error() != "request Content-Type isn't multipart/form-data" {
logger.Error("Error parsing form: " + err.Error())
cx.RespondWithError(http.StatusBadRequest)
return
}
}
client, err := core.QMgr.RegisterNewClient(files, cg)
if err != nil {
msg := "Error in registering new client:" + err.Error()
logger.Error(msg)
cx.RespondWithErrorMessage(msg, http.StatusBadRequest)
return
}
//log event about client registration (CR)
logger.Event(event.CLIENT_REGISTRATION, "clientid="+client.Id+";name="+client.Name+";host="+client.Host+";group="+client.Group+";instance_id="+client.InstanceId+";instance_type="+client.InstanceType+";domain="+client.Domain)
cx.RespondWithData(client)
return
}
示例11: ReadMany
// GET: /client
func (cr *ClientController) ReadMany(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_READ == true {
u = &user.User{Uuid: "public"}
} else {
cx.RespondWithErrorMessage(e.NoAuth, http.StatusUnauthorized)
return
}
}
clients := core.QMgr.GetAllClientsByUser(u)
query := &Query{Li: cx.Request.URL.Query()}
filtered := []*core.Client{}
if query.Has("busy") {
for _, client := range clients {
if len(client.Current_work) > 0 {
filtered = append(filtered, client)
}
}
} else {
filtered = clients
}
cx.RespondWithData(filtered)
return
}
示例12: Update
// PUT: /client/{id} -> status update
func (cr *ClientController) Update(id string, 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("subclients") { //update the number of subclients for a proxy
if count, err := strconv.Atoi(query.Value("subclients")); err != nil {
cx.RespondWithError(http.StatusNotImplemented)
} else {
core.QMgr.UpdateSubClientsByUser(id, count, u)
cx.RespondWithData("ok")
}
return
}
if query.Has("suspend") { //resume the suspended client
if err := core.QMgr.SuspendClientByUser(id, u); err != nil {
cx.RespondWithErrorMessage(err.Error(), http.StatusBadRequest)
} else {
cx.RespondWithData("client suspended")
}
return
}
if query.Has("resume") { //resume the suspended client
if err := core.QMgr.ResumeClientByUser(id, u); err != nil {
cx.RespondWithErrorMessage(err.Error(), http.StatusBadRequest)
} else {
cx.RespondWithData("client resumed")
}
return
}
cx.RespondWithError(http.StatusNotImplemented)
return
}
示例13: Update
// PUT: /work/{id} -> status update
func (cr *WorkController) Update(id string, cx *goweb.Context) {
LogRequest(cx.Request)
// Gather query params
query := &Query{Li: cx.Request.URL.Query()}
if !query.Has("client") {
cx.RespondWithErrorMessage("This request type requires the client=clientid parameter.", http.StatusBadRequest)
return
}
// Check auth
cg, err := request.AuthenticateClientGroup(cx.Request)
if err != nil {
if err.Error() == e.NoAuth || err.Error() == e.UnAuth || err.Error() == e.InvalidAuth {
if conf.CLIENT_AUTH_REQ == true {
cx.RespondWithError(http.StatusUnauthorized)
return
}
} else {
logger.Error("[email protected]: " + err.Error())
cx.RespondWithError(http.StatusInternalServerError)
return
}
}
// check that clientgroup auth token matches group of client
clientid := query.Value("client")
client, ok := core.QMgr.GetClient(clientid)
if !ok {
cx.RespondWithErrorMessage(e.ClientNotFound, http.StatusBadRequest)
return
}
if cg != nil && client.Group != cg.Name {
cx.RespondWithErrorMessage("Clientgroup name in token does not match that in the client configuration.", http.StatusBadRequest)
return
}
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("computetime") {
if comptime, err := strconv.Atoi(query.Value("computetime")); err == nil {
notice.ComputeTime = comptime
}
}
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)
}
}
if _, ok := files["stdout"]; ok {
core.QMgr.SaveStdLog(id, "stdout", files["stdout"].Path)
}
if _, ok := files["stderr"]; ok {
core.QMgr.SaveStdLog(id, "stderr", files["stderr"].Path)
}
if _, ok := files["worknotes"]; ok {
core.QMgr.SaveStdLog(id, "worknotes", files["worknotes"].Path)
}
}
}
core.QMgr.NotifyWorkStatus(notice)
}
cx.RespondWithData("ok")
return
}
示例14: Read
// GET: /work/{id}
// get a workunit by id, read-only
func (cr *WorkController) Read(id string, cx *goweb.Context) {
LogRequest(cx.Request)
// Gather query params
query := &Query{Li: cx.Request.URL.Query()}
if (query.Has("datatoken") || query.Has("privateenv")) && query.Has("client") {
cg, err := request.AuthenticateClientGroup(cx.Request)
if err != nil {
if err.Error() == e.NoAuth || err.Error() == e.UnAuth || err.Error() == e.InvalidAuth {
if conf.CLIENT_AUTH_REQ == true {
cx.RespondWithError(http.StatusUnauthorized)
return
}
} else {
logger.Error("[email protected]: " + err.Error())
cx.RespondWithError(http.StatusInternalServerError)
return
}
}
// check that clientgroup auth token matches group of client
clientid := query.Value("client")
client, ok := core.QMgr.GetClient(clientid)
if !ok {
cx.RespondWithErrorMessage(e.ClientNotFound, http.StatusBadRequest)
return
}
if cg != nil && client.Group != cg.Name {
cx.RespondWithErrorMessage("Clientgroup name in token does not match that in the client configuration.", http.StatusBadRequest)
return
}
if query.Has("datatoken") { //a client is requesting data token for this job
token, err := core.QMgr.FetchDataToken(id, clientid)
if err != nil {
cx.RespondWithErrorMessage("error in getting token for job "+id, http.StatusBadRequest)
return
}
//cx.RespondWithData(token)
RespondTokenInHeader(cx, token)
return
}
if query.Has("privateenv") { //a client is requesting data token for this job
envs, err := core.QMgr.FetchPrivateEnv(id, clientid)
if err != nil {
cx.RespondWithErrorMessage("error in getting token for job "+id, http.StatusBadRequest)
return
}
//cx.RespondWithData(token)
RespondPrivateEnvInHeader(cx, envs)
return
}
}
// 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_READ == true {
u = &user.User{Uuid: "public"}
} else {
cx.RespondWithErrorMessage(e.NoAuth, http.StatusUnauthorized)
return
}
}
jobid, err := core.GetJobIdByWorkId(id)
if err != nil {
cx.RespondWithErrorMessage(err.Error(), http.StatusBadRequest)
return
}
job, err := core.LoadJob(jobid)
if err != nil {
cx.RespondWithErrorMessage(err.Error(), http.StatusBadRequest)
return
}
// User must have read permissions on job or be job owner or be an admin
rights := job.Acl.Check(u.Uuid)
if job.Acl.Owner != u.Uuid && rights["read"] == false && u.Admin == false {
cx.RespondWithErrorMessage(e.UnAuth, http.StatusUnauthorized)
return
}
if query.Has("report") { //retrieve report: stdout or stderr or worknotes
reportmsg, err := core.QMgr.GetReportMsg(id, query.Value("report"))
if err != nil {
cx.RespondWithErrorMessage(err.Error(), http.StatusBadRequest)
return
}
cx.RespondWithData(reportmsg)
//.........這裏部分代碼省略.........
示例15: ReadMany
// GET: /work
// checkout a workunit with earliest submission time
// to-do: to support more options for workunit checkout
func (cr *WorkController) ReadMany(cx *goweb.Context) {
LogRequest(cx.Request)
// Gather query params
query := &Query{Li: cx.Request.URL.Query()}
if !query.Has("client") { //view workunits
// 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_READ == true {
u = &user.User{Uuid: "public"}
} else {
cx.RespondWithErrorMessage(e.NoAuth, http.StatusUnauthorized)
return
}
}
// get pagination options
limit := conf.DEFAULT_PAGE_SIZE
offset := 0
order := "info.submittime"
direction := "desc"
if query.Has("limit") {
limit, err = strconv.Atoi(query.Value("limit"))
if err != nil {
cx.RespondWithErrorMessage(err.Error(), http.StatusBadRequest)
return
}
}
if query.Has("offset") {
offset, err = strconv.Atoi(query.Value("offset"))
if err != nil {
cx.RespondWithErrorMessage(err.Error(), http.StatusBadRequest)
return
}
}
if query.Has("order") {
order = query.Value("order")
}
if query.Has("direction") {
direction = query.Value("direction")
}
var workunits []*core.Workunit
if query.Has("state") {
workunits = core.QMgr.ShowWorkunitsByUser(query.Value("state"), u)
} else {
workunits = core.QMgr.ShowWorkunitsByUser("", u)
}
// if using query syntax then do pagination and sorting
if query.Has("query") {
filtered_work := []core.Workunit{}
sorted_work := core.WorkunitsSortby{order, direction, workunits}
sort.Sort(sorted_work)
skip := 0
count := 0
for _, w := range sorted_work.Workunits {
if skip < offset {
skip += 1
continue
}
filtered_work = append(filtered_work, *w)
count += 1
if count == limit {
break
}
}
cx.RespondWithPaginatedData(filtered_work, limit, offset, len(sorted_work.Workunits))
return
} else {
cx.RespondWithData(workunits)
return
}
}
cg, err := request.AuthenticateClientGroup(cx.Request)
if err != nil {
if err.Error() == e.NoAuth || err.Error() == e.UnAuth || err.Error() == e.InvalidAuth {
if conf.CLIENT_AUTH_REQ == true {
cx.RespondWithError(http.StatusUnauthorized)
return
}
} else {
logger.Error("[email protected]: " + err.Error())
cx.RespondWithError(http.StatusInternalServerError)
return
}
}
//.........這裏部分代碼省略.........