func (mc *MgoCmd) Init(config interface{}) (err error) { mc.MgoCmdConfig = config.(*MgoCmdConfig) cmdlog.Printf("MgoCmd Init config :(%+v)\n", mc.MgoCmdConfig) mc.cmdHandlers = make(map[string]func(mc *MgoCmd, req *cmdproto.MgoRequest) (interface{}, error)) mc.cmdReqPool = make(chan *cmdproto.MgoRequest, mc.MgoReqPoolSize) //allocates fixed size request pool. for i := 0; i < mc.MgoReqPoolSize; i++ { mgoReq := &cmdproto.MgoRequest{} mc.cmdReqPool <- mgoReq } dialInfo := &mgo.DialInfo{Addrs: mc.MgoAddrs, Timeout: (500 * time.Millisecond)} mc.mgoSession, err = mgo.DialWithInfo(dialInfo) if err != nil { cmdlog.EPrintln(err.Error()) return err } mc.stopDiskMonCh = make(chan bool) mc.register("dbStats", statsHandler) mc.register("diskMonB", diskMonStartHandler) mc.register("diskMonE", diskMonEndHandler) mc.register("dbMonB", dbMonStartHandler) //mc.register("dbMonE", dbMonEndHandler) cmdlog.Printf("MgoCmd Init ok\n") return nil }
func (scc *ServiceCtrlCmd) Init(config interface{}) (err error) { scc.ScCmdConfig = config.(*ScCmdConfig) cmdlog.Printf("ServiceCtrlCmd Init config :(%+v)\n", scc.ScCmdConfig) scc.cmdHandlers = make(map[string]func(scc *ServiceCtrlCmd, req *cmdproto.ScRequest) (interface{}, error)) scc.cmdReqPool = make(chan *cmdproto.ScRequest, scc.ScRequestPoolSize) //allocates fixed size request pool. for i := 0; i < scc.ScRequestPoolSize; i++ { scc.cmdReqPool <- new(cmdproto.ScRequest) } //todo: scc.register("start", startHandler) scc.register("stop", stopHandler) scc.register("monitor", monitorHandler) cmdlog.Printf("ServiceCtrlCmd Init ok\n") return nil }
func (sc *SystemCmd) ServeHTTP(w http.ResponseWriter, req *http.Request) { data, err := ioutil.ReadAll(req.Body) if err != nil { cmdlog.EPrintf("%s\n", err.Error()) http.Error(w, err.Error(), http.StatusInternalServerError) return } cmdlog.Printf("%s\n", string(data)) defer req.Body.Close() sysReq := sc.getAvalibleReq() defer sc.recycle(sysReq) err = json.Unmarshal(data, sysReq) if err != nil { cmdlog.EPrintf("%s\n", err.Error()) http.Error(w, err.Error(), http.StatusInternalServerError) return } op := strings.ToLower(sysReq.Op) if handler, ok := sc.cmdHandlers[op]; ok { res, err := handler(sc, sysReq) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } w.Header().Set("Content-Type", "text/plain; charset=utf-8") w.WriteHeader(http.StatusOK) switch res.(type) { case string: fmt.Fprintln(w, res.(string)) case []byte: fmt.Fprintln(w, string(res.([]byte))) case []string: fmt.Fprintln(w, res.([]string)) case int: fmt.Fprintln(w, res.(int)) case io.ReadCloser: stdout := res.(io.ReadCloser) defer stdout.Close() reader := bufio.NewReader(stdout) for { line, err := reader.ReadBytes('\n') if err != nil { cmdlog.EPrintf("err:%s\n", err.Error()) break } // fmt.Println("%s is executing...", sysReq.Op) fmt.Fprintf(w, "%s", string(line)) w.(http.Flusher).Flush() } } } else { cmdlog.EPrintln("method not implemented") http.Error(w, fmt.Sprintf("server do not support command %s", sysReq.Op), http.StatusNotImplemented) return } }
func (sc *SystemCmd) Init(config interface{}) (err error) { sc.SysCmdConfig = config.(*SysCmdConfig) cmdlog.Printf("SystemCmd Init config :(%+v)\n", sc.SysCmdConfig) sc.cmdHandlers = make(map[string]func(sc *SystemCmd, req *cmdproto.SysRequest) (interface{}, error)) sc.cmdReqPool = make(chan *cmdproto.SysRequest, sc.SysRequestPoolSize) //allocates fixed size request pool. for i := 0; i < sc.SysRequestPoolSize; i++ { sc.cmdReqPool <- new(cmdproto.SysRequest) } sc.diskMonCh = make(chan bool) sc.diskMonState = 0 //todo: sc.register("monitor", monitorHandler) sc.register("syscmd", syscmdHandler) cmdlog.Printf("SystemCmd Init ok\n") return nil }
func (scc *ServiceCtrlCmd) ServeHTTP(w http.ResponseWriter, req *http.Request) { data, err := ioutil.ReadAll(req.Body) if err != nil { cmdlog.EPrintf("%s\n", err.Error()) http.Error(w, err.Error(), http.StatusInternalServerError) return } cmdlog.Printf("%s\n", string(data)) defer req.Body.Close() scReq := scc.getAvalibleReq() defer scc.recycle(scReq) err = json.Unmarshal(data, scReq) if err != nil { cmdlog.EPrintf("%s\n", err.Error()) http.Error(w, err.Error(), http.StatusInternalServerError) return } if handler, ok := scc.cmdHandlers[scReq.Op]; ok { res, err := handler(scc, scReq) if err != nil { cmdlog.EPrintf("%s\n", err.Error()) http.Error(w, err.Error(), http.StatusInternalServerError) return } w.Header().Set("Content-Type", "text/plain; charset=utf-8") w.WriteHeader(http.StatusOK) //fmt.Fprintln(w, error) switch res.(type) { case string: fmt.Fprintln(w, res.(string)) case []byte: fmt.Fprintln(w, string(res.([]byte))) case []string: fmt.Fprintln(w, res.([]string)) case int: fmt.Println(w, res.(int)) } } else { cmdlog.EPrintln("method not implemented") http.Error(w, fmt.Sprintf("server do not support command %s", scReq.Op), http.StatusNotImplemented) return } }