示例#1
0
文件: local.go 项目: jcantrill/geard
func (h *localTransport) RemoteJobFor(locator transport.Locator, j interface{}) (job jobs.Job, err error) {
	if locator != transport.Local {
		return h.remote.RemoteJobFor(locator, j)
	}

	job, err = jobs.JobFor(j)
	if err != nil {
		return
	}
	return
}
示例#2
0
文件: server.go 项目: jhadvig/geard
func (conf *HttpConfiguration) handleWithMethod(method JobHandler) func(*rest.ResponseWriter, *rest.Request) {
	return func(w *rest.ResponseWriter, r *rest.Request) {
		match := r.Header.Get("If-Match")
		segments := strings.Split(match, ",")
		for i := range segments {
			if strings.HasPrefix(segments[i], "api=") {
				if segments[i][4:] != ApiVersion() {
					http.Error(w, fmt.Sprintf("Current API version %s does not match requested %s", ApiVersion(), segments[i][4:]), http.StatusPreconditionFailed)
					return
				}
			}
		}

		context := &jobs.JobContext{}

		requestId := r.Header.Get("X-Request-Id")
		if requestId == "" {
			context.Id = jobs.NewRequestIdentifier()
		} else {
			id, err := jobs.NewRequestIdentifierFromString(requestId)
			if err != nil {
				http.Error(w, "X-Request-Id must be a 32 character hexadecimal string", http.StatusBadRequest)
				return
			}
			context.Id = id
		}

		// parse the incoming request into an object
		jobRequest, errh := method(context, r)
		if errh != nil {
			serveRequestError(w, apiRequestError{errh, errh.Error(), http.StatusBadRequest})
			return
		}

		// find the job implementation for that request
		job, errj := jobs.JobFor(jobRequest)
		if errj != nil {
			serveRequestError(w, apiRequestError{errj, errj.Error(), http.StatusBadRequest})
			return
		}

		// determine the type of the request
		acceptHeader := r.Header.Get("Accept")
		overrideAcceptHeader := r.Header.Get("X-Accept")
		if overrideAcceptHeader != "" {
			acceptHeader = overrideAcceptHeader
		}

		// setup the appropriate mode
		mode := ResponseJson
		if acceptHeader == "text/plain" {
			mode = ResponseTable
		}
		canStream := didClientRequestStreamableResponse(acceptHeader)
		response := NewHttpJobResponse(w.ResponseWriter, !canStream, mode)

		// queue / handle the request
		wait, errd := conf.Dispatcher.Dispatch(context.Id, job, response)
		if errd == jobs.ErrRanToCompletion {
			http.Error(w, errd.Error(), http.StatusNoContent)
			return
		} else if errd != nil {
			serveRequestError(w, apiRequestError{errd, errd.Error(), http.StatusServiceUnavailable})
			return
		}
		<-wait
	}
}
示例#3
0
文件: server.go 项目: kshi0310/geard
func (conf *HttpConfiguration) handleWithMethod(method JobHandler) func(*rest.ResponseWriter, *rest.Request) {
	return func(w *rest.ResponseWriter, r *rest.Request) {
		context := &HttpContext{}

		context.ApiVersion = r.Header.Get("X-Api-Version")

		requestId := r.Header.Get("X-Request-Id")
		if requestId == "" {
			context.Id = jobs.NewRequestIdentifier()
		} else {
			id, err := jobs.NewRequestIdentifierFromString(requestId)
			if err != nil {
				http.Error(w, "X-Request-Id must be a 32 character hexadecimal string", http.StatusBadRequest)
				return
			}
			context.Id = id
		}

		// parse the incoming request into an object
		jobRequest, errh := method(conf, context, r)
		if errh != nil {
			serveRequestError(w, apiRequestError{errh, errh.Error(), http.StatusBadRequest})
			return
		}

		// find the job implementation for that request
		job, errj := jobs.JobFor(jobRequest)
		if errj != nil {
			if errj == jobs.ErrNoJobForRequest {
				serveRequestError(w, apiRequestError{errj, fmt.Sprintf("The requested job %s has no registered implementation", reflect.TypeOf(jobRequest)), http.StatusBadRequest})
			}
			serveRequestError(w, apiRequestError{errj, errj.Error(), http.StatusBadRequest})
			return
		}

		// determine the type of the request
		acceptHeader := r.Header.Get("Accept")
		overrideAcceptHeader := r.Header.Get("X-Accept")
		if overrideAcceptHeader != "" {
			acceptHeader = overrideAcceptHeader
		}

		// setup the appropriate mode
		mode := client.ResponseJson
		if acceptHeader == "text/plain" {
			mode = client.ResponseTable
		}
		canStream := didClientRequestStreamableResponse(acceptHeader)
		response := NewHttpJobResponse(w.ResponseWriter, !canStream, mode)

		// queue / handle the request
		wait, errd := conf.Dispatcher.Dispatch(context.Id, job, response)
		if errd == jobs.ErrRanToCompletion {
			http.Error(w, errd.Error(), http.StatusNoContent)
			return
		} else if errd != nil {
			serveRequestError(w, apiRequestError{errd, errd.Error(), http.StatusServiceUnavailable})
			return
		}
		<-wait
	}
}