bosun.org@v0.0.0-20210513094433-e25bc3e69a1f/cmd/bosun/web/save.go (about) 1 package web 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "net/http" 7 8 "bosun.org/cmd/bosun/conf" 9 "github.com/MiniProfiler/go/miniprofiler" 10 ) 11 12 func SaveConfig(t miniprofiler.Timer, w http.ResponseWriter, r *http.Request) (interface{}, error) { 13 data := struct { 14 Config string 15 Diff string 16 User string 17 Message string 18 Other []string 19 }{} 20 decoder := json.NewDecoder(r.Body) 21 if err := decoder.Decode(&data); err != nil { 22 return nil, err 23 } 24 if data.User != "" && !userCanOverwriteUsername(r) { 25 http.Error(w, "Not Authorized to set User", 400) 26 return nil, nil 27 } else if data.User == "" { 28 data.User = getUsername(r) 29 } 30 err := schedule.RuleConf.SaveRawText(data.Config, data.Diff, data.User, data.Message, data.Other...) 31 if err != nil { 32 return nil, err 33 } 34 fmt.Fprint(w, "save successful") 35 return nil, nil 36 } 37 38 func DiffConfig(t miniprofiler.Timer, w http.ResponseWriter, r *http.Request) (interface{}, error) { 39 data := struct { 40 Config string 41 Message string 42 User string 43 Other []string 44 }{} 45 if data.User != "" && !userCanOverwriteUsername(r) { 46 http.Error(w, "Not Authorized to set User", 400) 47 return nil, nil 48 } else if data.User == "" { 49 data.User = getUsername(r) 50 } 51 decoder := json.NewDecoder(r.Body) 52 if err := decoder.Decode(&data); err != nil { 53 return nil, err 54 } 55 diff, err := schedule.RuleConf.RawDiff(data.Config) 56 if err != nil { 57 return nil, err 58 } 59 fmt.Fprint(w, diff) 60 return nil, nil 61 } 62 63 func ConfigRunningHash(t miniprofiler.Timer, w http.ResponseWriter, r *http.Request) (interface{}, error) { 64 hash := schedule.RuleConf.GetHash() 65 return struct { 66 Hash string 67 }{ 68 hash, 69 }, nil 70 } 71 72 func BulkEdit(t miniprofiler.Timer, w http.ResponseWriter, r *http.Request) (interface{}, error) { 73 bulkEdit := conf.BulkEditRequest{} 74 decoder := json.NewDecoder(r.Body) 75 if err := decoder.Decode(&bulkEdit); err != nil { 76 return nil, err 77 } 78 err := schedule.RuleConf.BulkEdit(bulkEdit) 79 if err != nil { 80 return nil, err 81 } 82 fmt.Fprint(w, "edit successful") 83 return nil, nil 84 } 85 86 func SaveEnabled(t miniprofiler.Timer, w http.ResponseWriter, r *http.Request) (interface{}, error) { 87 return schedule.SystemConf.SaveEnabled(), nil 88 }