Esempio n. 1
0
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
	}
}
Esempio n. 2
0
func (scc *ServiceCtrlCmd) register(cmd string, handler func(scc *ServiceCtrlCmd, req *cmdproto.ScRequest) (interface{}, error)) {
	if _, ok := scc.cmdHandlers[cmd]; ok {
		cmdlog.EPrintf("duplicate serive ctrl cmd %s handler registered!\n", cmd)
		return
	}

	scc.cmdHandlers[cmd] = handler
}
Esempio n. 3
0
func (mc *MgoCmd) register(cmd string, handler func(mc *MgoCmd, req *cmdproto.MgoRequest) (interface{}, error)) {
	if _, ok := mc.cmdHandlers[cmd]; ok {
		cmdlog.EPrintf("duplicate mongo cmd %s handler registered!\n", cmd)
		return
	}

	mc.cmdHandlers[cmd] = handler
}
Esempio n. 4
0
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
	}
}