示例#1
0
文件: tasks.go 项目: altlinux/webery
// :WEBAPI:
// {
//   "url": "{schema}://{host}/api/v1/tasks/{taskid}",
//   "method": "POST",
//   "arguments": [
//     {"name": "taskid", "type": "integer", "description": "task number"}
//   ],
//   "description": "Updates existing task"
// }
func TaskUpdateHandler(ctx context.Context, w http.ResponseWriter, r *http.Request) {
	p, ok := ctx.Value("http.request.query.params").(*url.Values)
	if !ok {
		ahttp.HTTPResponse(w, http.StatusInternalServerError, "Unable to obtain params from context")
		return
	}

	st, ok := ctx.Value("app.database").(db.Session)
	if !ok {
		ahttp.HTTPResponse(w, http.StatusInternalServerError, "Unable to obtain database from context")
		return
	}

	msg, err := ioutil.ReadAll(r.Body)
	if err != nil {
		ahttp.HTTPResponse(w, http.StatusBadRequest, "Unable to read body: %s", err)
		return
	}
	logger.GetHTTPEntry(ctx).WithFields(nil).Infof("TaskUpdateHandler: Request body: %s", string(msg))

	ev := task.NewTaskEvent()
	if err = json.Unmarshal(msg, ev); err != nil {
		ahttp.HTTPResponse(w, http.StatusBadRequest, "Invalid JSON: %s", err)
		return
	}

	taskID := task.MakeID(util.ToInt64(p.Get("task")))

	t, err := task.Read(st, taskID)
	if err != nil {
		if db.IsNotFound(err) {
			ahttp.HTTPResponse(w, http.StatusNotFound, "Not found")
		} else {
			ahttp.HTTPResponse(w, http.StatusInternalServerError, "Unable to read: %v", err)
		}
		return
	}

	t.TaskID.Set(util.ToInt64(p.Get("task")))

	if !t.TimeCreate.IsDefined() {
		t.TimeCreate.Set(time.Now().Unix())
	}

	fillTask(t, ev)
	logger.GetHTTPEntry(ctx).WithFields(nil).Infof("TaskUpdateHandler: Task: %+v", t)

	if !writeTask(ctx, w, t) {
		return
	}

	ahttp.HTTPResponse(w, http.StatusOK, "OK")
}
示例#2
0
文件: tasks.go 项目: altlinux/webery
// :WEBAPI:
// {
//   "url": "{schema}://{host}/api/v1/tasks",
//   "method": "POST",
//   "description": "Creates new task"
// }
func TaskCreateHandler(ctx context.Context, w http.ResponseWriter, r *http.Request) {
	cfg, ok := ctx.Value("app.config").(*config.Config)
	if !ok {
		ahttp.HTTPResponse(w, http.StatusInternalServerError, "Unable to obtain config from context")
		return
	}

	msg, err := ioutil.ReadAll(r.Body)
	if err != nil {
		ahttp.HTTPResponse(w, http.StatusBadRequest, "Unable to read body: %s", err)
		return
	}
	logger.GetHTTPEntry(ctx).WithFields(nil).Infof("TaskCreateHandler: Request body: %s", string(msg))

	ev := task.NewTaskEvent()
	if err = json.Unmarshal(msg, ev); err != nil {
		ahttp.HTTPResponse(w, http.StatusBadRequest, "Invalid JSON: %s", err)
		return
	}

	t := task.New()
	fillTask(t, ev)

	t.TimeCreate.Set(time.Now().Unix())

	logger.GetHTTPEntry(ctx).WithFields(nil).Infof("TaskCreateHandler: Task: %+v", t)

	if v, ok := t.Repo.Get(); ok {
		if !util.InSliceString(v, cfg.Builder.Repos) {
			ahttp.HTTPResponse(w, http.StatusBadRequest, "Unknown repo")
			return
		}
	} else {
		ahttp.HTTPResponse(w, http.StatusBadRequest, "repo: mandatory field is not specified")
		return
	}

	if !t.Owner.IsDefined() {
		ahttp.HTTPResponse(w, http.StatusBadRequest, "owner: mandatory field is not specified")
		return
	}

	if !writeTask(ctx, w, t) {
		return
	}

	ahttp.HTTPResponse(w, http.StatusOK, "OK")
}