Exemple #1
0
func moduleInstStartHandler(w http.ResponseWriter, req *http.Request) {

	w.Header().Set("Content-Type", "application/json; charset=UTF-8")

	vars := mux.Vars(req)
	id := vars["id"]
	if id == "" {
		w.Write(getJSONError("The URL should include the module instance ID", nil))
		log.Printf("The URL should include the module instance ID\n")
		return
	}

	idInt, idIntErr := strconv.ParseInt(id, 10, 32)

	if idIntErr != nil {
		w.Write(getJSONError("Error parsing id", idIntErr))
		log.Printf("Error parsing id ERR: %v\n", idIntErr)
		return
	}

	idInt32 := int32(idInt)

	modInst, modInstErr := module.GetModuleInstance(idInt32)
	if modInstErr != nil {
		w.Write(getJSONError("Unknown module id", modInstErr))
		log.Printf("Unknown module id ERR: %v\n", modInstErr)
		return
	}

	jsonBuf, jsonBufErr := json.MarshalIndent(modInst, "", "  ")
	if jsonBufErr != nil {
		w.Write(getJSONError("Error marshalling object to json", jsonBufErr))
		log.Printf("Error marshalling object to json ERR: %v\n", jsonBufErr)
		return
	}

	if modInst.IsStarted {
		w.Write(jsonBuf)
		return
	}

	startErr := module.StartModule(idInt32)

	if startErr != nil {
		w.Write(getJSONError("Error starting moudle", startErr))
		log.Printf("Error starting module ERR: %v\n", startErr)
		return
	}

	jsonBufErr = nil
	jsonBuf, jsonBufErr = json.MarshalIndent(modInst, "", "  ")
	if jsonBufErr != nil {
		w.Write(getJSONError("Error marshalling object to json", jsonBufErr))
		log.Printf("Error marshalling object to json ERR: %v\n", jsonBufErr)
		return
	}

	w.Write(jsonBuf)
}
Exemple #2
0
func moduleInstStartHandler(w http.ResponseWriter, req *http.Request) {

	w.Header().Set("Content-Type", "application/json; charset=UTF-8")

	vars := mux.Vars(req)
	name := vars["name"]
	if name == "" {
		w.Write(getJSONError("The URL should include the module instance name", nil))
		log.Printf("The URL should include the module instance name\n")
		return
	}

	modInst, modInstErr := module.GetModuleInstance(name)
	if modInstErr != nil {
		w.Write(getJSONError("Unknown module name", modInstErr))
		log.Printf("Unknown module name ERR: %v\n", modInstErr)
		return
	}

	jsonBuf, jsonBufErr := json.MarshalIndent(modInst, "", "  ")
	if jsonBufErr != nil {
		w.Write(getJSONError("Error marshalling object to json", jsonBufErr))
		log.Printf("Error marshalling object to json ERR: %v\n", jsonBufErr)
		return
	}

	if modInst.IsStarted {
		w.Write(jsonBuf)
		return
	}

	startErr := module.StartModule(name)

	if startErr != nil {
		w.Write(getJSONError("Error starting moudle", startErr))
		log.Printf("Error starting module ERR: %v\n", startErr)
		return
	}

	jsonBufErr = nil
	jsonBuf, jsonBufErr = json.MarshalIndent(modInst, "", "  ")
	if jsonBufErr != nil {
		w.Write(getJSONError("Error marshalling object to json", jsonBufErr))
		log.Printf("Error marshalling object to json ERR: %v\n", jsonBufErr)
		return
	}

	w.Write(jsonBuf)
}