本文整理匯總了Golang中github.com/influxdata/kapacitor/services/httpd.HttpError函數的典型用法代碼示例。如果您正苦於以下問題:Golang HttpError函數的具體用法?Golang HttpError怎麽用?Golang HttpError使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了HttpError函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: handleTest
func (s *Service) handleTest(w http.ResponseWriter, r *http.Request) {
name := s.nameFromPath(r.URL.Path)
if name == "" {
httpd.HttpError(w, "must provide service name", true, http.StatusBadRequest)
return
}
test, ok := s.testers[name]
if !ok {
httpd.HttpError(w, fmt.Sprintf("service %q not found", name), true, http.StatusNotFound)
return
}
options := test.TestOptions()
if options != nil {
if err := json.NewDecoder(r.Body).Decode(options); err != nil {
httpd.HttpError(w, fmt.Sprint("failed to decode JSON body:", err), true, http.StatusBadRequest)
return
}
}
result := ServiceTestResult{}
err := test.Test(options)
if err != nil {
result.Message = err.Error()
} else {
result.Success = true
}
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(result)
}
示例2: handleTemplate
func (ts *Service) handleTemplate(w http.ResponseWriter, r *http.Request) {
id, err := ts.templateIDFromPath(r.URL.Path)
if err != nil {
httpd.HttpError(w, err.Error(), true, http.StatusBadRequest)
return
}
raw, err := ts.templates.Get(id)
if err != nil {
httpd.HttpError(w, err.Error(), true, http.StatusNotFound)
return
}
scriptFormat := r.URL.Query().Get("script-format")
switch scriptFormat {
case "":
scriptFormat = "formatted"
case "formatted", "raw":
default:
httpd.HttpError(w, fmt.Sprintf("invalid script-format parameter %q", scriptFormat), true, http.StatusBadRequest)
return
}
t, err := ts.convertTemplate(raw, scriptFormat)
if err != nil {
httpd.HttpError(w, err.Error(), true, http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
w.Write(httpd.MarshalJSON(t, true))
}
示例3: handleDeleteRecording
func (s *Service) handleDeleteRecording(w http.ResponseWriter, r *http.Request) {
rid, err := s.recordingIDFromPath(r.URL.Path)
if err != nil {
httpd.HttpError(w, err.Error(), true, http.StatusBadRequest)
return
}
recording, err := s.recordings.Get(rid)
if err == ErrNoRecordingExists {
w.WriteHeader(http.StatusNoContent)
return
}
if err != nil {
httpd.HttpError(w, err.Error(), true, http.StatusInternalServerError)
return
}
err = s.recordings.Delete(rid)
if err != nil {
httpd.HttpError(w, err.Error(), true, http.StatusInternalServerError)
return
}
ds, err := parseDataSourceURL(recording.DataURL)
if err != nil {
httpd.HttpError(w, err.Error(), true, http.StatusInternalServerError)
return
}
err = ds.Remove()
if err != nil {
httpd.HttpError(w, err.Error(), true, http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusNoContent)
}
示例4: handleRecordBatch
func (s *Service) handleRecordBatch(w http.ResponseWriter, req *http.Request) {
var opt kclient.RecordBatchOptions
dec := json.NewDecoder(req.Body)
err := dec.Decode(&opt)
if err != nil {
httpd.HttpError(w, err.Error(), true, http.StatusBadRequest)
return
}
if opt.ID == "" {
opt.ID = uuid.NewV4().String()
}
if !validID.MatchString(opt.ID) {
httpd.HttpError(w, fmt.Sprintf("recording ID must contain only letters, numbers, '-', '.' and '_'. %q", opt.ID), true, http.StatusBadRequest)
return
}
if opt.Start.IsZero() {
httpd.HttpError(w, "must provide start time", true, http.StatusBadRequest)
return
}
if opt.Stop.IsZero() {
opt.Stop = time.Now()
}
t, err := s.TaskStore.Load(opt.Task)
if err != nil {
httpd.HttpError(w, err.Error(), true, http.StatusNotFound)
return
}
dataUrl := s.dataURLFromID(opt.ID, batchEXT)
recording := Recording{
ID: opt.ID,
DataURL: dataUrl.String(),
Type: BatchRecording,
Date: time.Now(),
Status: Running,
}
err = s.recordings.Create(recording)
if err != nil {
httpd.HttpError(w, err.Error(), true, http.StatusInternalServerError)
return
}
go func(recording Recording) {
ds, _ := parseDataSourceURL(dataUrl.String())
err := s.doRecordBatch(ds, t, opt.Start, opt.Stop)
s.updateRecordingResult(recording, ds, err)
}(recording)
w.WriteHeader(http.StatusCreated)
w.Write(httpd.MarshalJSON(convertRecording(recording), true))
}
示例5: handleRecordQuery
func (s *Service) handleRecordQuery(w http.ResponseWriter, req *http.Request) {
var opt kclient.RecordQueryOptions
dec := json.NewDecoder(req.Body)
err := dec.Decode(&opt)
if err != nil {
httpd.HttpError(w, err.Error(), true, http.StatusBadRequest)
return
}
if opt.ID == "" {
opt.ID = uuid.NewV4().String()
}
if !validID.MatchString(opt.ID) {
httpd.HttpError(w, fmt.Sprintf("recording ID must contain only letters, numbers, '-', '.' and '_'. %q", opt.ID), true, http.StatusBadRequest)
return
}
if opt.Query == "" {
httpd.HttpError(w, "must provide query", true, http.StatusBadRequest)
return
}
var dataUrl url.URL
var typ RecordingType
switch opt.Type {
case kclient.StreamTask:
dataUrl = s.dataURLFromID(opt.ID, streamEXT)
typ = StreamRecording
case kclient.BatchTask:
dataUrl = s.dataURLFromID(opt.ID, batchEXT)
typ = BatchRecording
}
recording := Recording{
ID: opt.ID,
DataURL: dataUrl.String(),
Type: typ,
Date: time.Now(),
Status: Running,
}
err = s.recordings.Create(recording)
if err != nil {
httpd.HttpError(w, err.Error(), true, http.StatusInternalServerError)
return
}
go func(recording Recording) {
ds, _ := parseDataSourceURL(dataUrl.String())
err := s.doRecordQuery(ds, opt.Query, typ, opt.Cluster)
s.updateRecordingResult(recording, ds, err)
}(recording)
w.WriteHeader(http.StatusCreated)
w.Write(httpd.MarshalJSON(convertRecording(recording), true))
}
示例6: handleDeleteTemplate
func (ts *Service) handleDeleteTemplate(w http.ResponseWriter, r *http.Request) {
id, err := ts.templateIDFromPath(r.URL.Path)
if err != nil {
httpd.HttpError(w, err.Error(), true, http.StatusBadRequest)
return
}
err = ts.templates.Delete(id)
if err != nil {
httpd.HttpError(w, err.Error(), true, http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusNoContent)
}
示例7: handleTask
func (ts *Service) handleTask(w http.ResponseWriter, r *http.Request) {
name := r.URL.Query().Get("name")
if name == "" {
httpd.HttpError(w, "must pass task name", true, http.StatusBadRequest)
return
}
labels := false
labelsStr := r.URL.Query().Get("labels")
if labelsStr != "" {
var err error
labels, err = strconv.ParseBool(labelsStr)
if err != nil {
httpd.HttpError(w, "invalid labels value:", true, http.StatusBadRequest)
return
}
}
raw, err := ts.LoadRaw(name)
if err != nil {
httpd.HttpError(w, err.Error(), true, http.StatusNotFound)
return
}
executing := ts.TaskMaster.IsExecuting(name)
errMsg := raw.Error
dot := ""
task, err := ts.Load(name)
if err == nil {
if executing {
dot = ts.TaskMaster.ExecutingDot(name, labels)
} else {
dot = string(task.Dot())
}
} else {
errMsg = err.Error()
}
info := TaskInfo{
Name: name,
Type: raw.Type,
DBRPs: raw.DBRPs,
TICKscript: raw.TICKscript,
Dot: dot,
Enabled: ts.IsEnabled(name),
Executing: executing,
Error: errMsg,
}
w.Write(httpd.MarshalJSON(info, true))
}
示例8: handleGetConfig
func (s *Service) handleGetConfig(w http.ResponseWriter, r *http.Request) {
if !s.enabled {
httpd.HttpError(w, "config override service is not enabled", true, http.StatusForbidden)
return
}
section, element, hasSection, hasElement := sectionAndElementFromPath(r.URL.Path, configBasePath)
config, err := s.getConfig(section)
if err != nil {
httpd.HttpError(w, fmt.Sprint("failed to resolve current config:", err), true, http.StatusInternalServerError)
return
}
if hasSection && section == "" {
httpd.HttpError(w, "section not specified, do you have an extra trailing '/'?", true, http.StatusBadRequest)
return
}
if !hasSection {
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(config)
} else if section != "" {
sec, ok := config.Sections[section]
if !ok {
httpd.HttpError(w, fmt.Sprint("unknown section: ", section), true, http.StatusNotFound)
return
}
if hasElement {
var elementEntry client.ConfigElement
// Find specified element
elementKey := s.elementKeys[section]
found := false
for _, e := range sec.Elements {
if (element == "" && elementKey == "") || e.Options[elementKey] == element {
elementEntry = e
found = true
break
}
}
if found {
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(elementEntry)
} else {
httpd.HttpError(w, fmt.Sprintf("unknown section/element: %s/%s", section, element), true, http.StatusNotFound)
return
}
} else {
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(sec)
}
}
}
示例9: handleListTests
func (s *Service) handleListTests(w http.ResponseWriter, r *http.Request) {
tests := ServiceTests{
Link: serviceTestsLink,
}
pattern := r.URL.Query().Get("pattern")
if pattern == "" {
pattern = "*"
}
for name, test := range s.testers {
if ok, err := filepath.Match(pattern, name); err != nil {
httpd.HttpError(w, fmt.Sprintf("bad pattern: %v", err), true, http.StatusBadRequest)
return
} else if ok {
options := test.TestOptions()
tests.Services = append(tests.Services, ServiceTest{
Link: s.serviceTestLink(name),
Name: name,
Options: options,
})
}
}
sort.Sort(tests.Services)
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(tests)
}
示例10: handleSubscriptions
// Refresh the subscriptions linking for all clusters.
func (s *Service) handleSubscriptions(w http.ResponseWriter, r *http.Request) {
err := s.LinkSubscriptions()
if err != nil {
httpd.HttpError(w, fmt.Sprintf("failed to link subscriptions: %s", err.Error()), true, http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusNoContent)
}
示例11: handleDisable
func (ts *Service) handleDisable(w http.ResponseWriter, r *http.Request) {
name := r.URL.Query().Get("name")
err := ts.Disable(name)
if err != nil {
httpd.HttpError(w, err.Error(), true, http.StatusInternalServerError)
return
}
}
示例12: handleRecordStream
func (s *Service) handleRecordStream(w http.ResponseWriter, r *http.Request) {
var opt kclient.RecordStreamOptions
dec := json.NewDecoder(r.Body)
err := dec.Decode(&opt)
if err != nil {
httpd.HttpError(w, err.Error(), true, http.StatusBadRequest)
return
}
if opt.ID == "" {
opt.ID = uuid.NewV4().String()
}
if !validID.MatchString(opt.ID) {
httpd.HttpError(w, fmt.Sprintf("recording ID must contain only letters, numbers, '-', '.' and '_'. %q", opt.ID), true, http.StatusBadRequest)
return
}
t, err := s.TaskStore.Load(opt.Task)
if err != nil {
httpd.HttpError(w, err.Error(), true, http.StatusNotFound)
return
}
dataUrl := s.dataURLFromID(opt.ID, streamEXT)
recording := Recording{
ID: opt.ID,
DataURL: dataUrl.String(),
Type: StreamRecording,
Date: time.Now(),
Status: Running,
}
err = s.recordings.Create(recording)
if err != nil {
httpd.HttpError(w, err.Error(), true, http.StatusInternalServerError)
return
}
// Spawn routine to perform actual recording.
go func(recording Recording) {
ds, _ := parseDataSourceURL(dataUrl.String())
err := s.doRecordStream(opt.ID, ds, opt.Stop, t.DBRPs, t.Measurements())
s.updateRecordingResult(recording, ds, err)
}(recording)
w.WriteHeader(http.StatusCreated)
w.Write(httpd.MarshalJSON(convertRecording(recording), true))
}
示例13: runOut
func (h *HTTPOutNode) runOut([]byte) error {
hndl := func(w http.ResponseWriter, req *http.Request) {
h.mu.RLock()
defer h.mu.RUnlock()
if b, err := json.Marshal(h.result); err != nil {
httpd.HttpError(
w,
err.Error(),
true,
http.StatusInternalServerError,
)
} else {
w.Write(b)
}
}
p := path.Join("/task", h.et.Task.Name, h.c.Endpoint)
r := []httpd.Route{{
Name: h.Name(),
Method: "GET",
Pattern: p,
HandlerFunc: hndl,
}}
h.endpoint = h.et.tm.HTTPDService.URL() + p
func() {
h.mu.Lock()
defer h.mu.Unlock()
h.routes = r
}()
err := h.et.tm.HTTPDService.AddRoutes(r)
if err != nil {
return err
}
switch h.Wants() {
case pipeline.StreamEdge:
for p, ok := h.ins[0].NextPoint(); ok; p, ok = h.ins[0].NextPoint() {
h.timer.Start()
row := models.PointToRow(p)
h.updateResultWithRow(p.Group, row)
h.timer.Stop()
}
case pipeline.BatchEdge:
for b, ok := h.ins[0].NextBatch(); ok; b, ok = h.ins[0].NextBatch() {
h.timer.Start()
row := models.BatchToRow(b)
h.updateResultWithRow(b.Group, row)
h.timer.Stop()
}
}
return nil
}
示例14: handleReplay
func (s *Service) handleReplay(w http.ResponseWriter, req *http.Request) {
id, err := s.replayIDFromPath(req.URL.Path)
if err != nil {
httpd.HttpError(w, err.Error(), true, http.StatusBadRequest)
return
}
replay, err := s.replays.Get(id)
if err != nil {
httpd.HttpError(w, "could not find replay: "+err.Error(), true, http.StatusNotFound)
return
}
if replay.Status == Running {
w.WriteHeader(http.StatusAccepted)
} else {
w.WriteHeader(http.StatusOK)
}
w.Write(httpd.MarshalJSON(convertReplay(replay), true))
}
示例15: handleDeleteReplay
func (s *Service) handleDeleteReplay(w http.ResponseWriter, req *http.Request) {
id, err := s.replayIDFromPath(req.URL.Path)
if err != nil {
httpd.HttpError(w, err.Error(), true, http.StatusBadRequest)
return
}
//TODO: Cancel running replays
s.replays.Delete(id)
w.WriteHeader(http.StatusNoContent)
}