Ejemplo n.º 1
0
/*
Execute command depend on type that has been declared before
*/
func (c *Command) Exec() (string, error) {
	var (
		res     string
		e       error
		httpRes *http.Response
	)
	res = "initial"

	e = fmt.Errorf("Command %s %s can't be executed. No valid implementation can be found")

	if c.Type == CommandType_Local {
		ps := []string{}
		if c.CommandParms != nil {
			ps = c.CommandParms
		}

		res, e = toolkit.RunCommand(c.CommandText, ps...)

	} else if c.Type == CommandType_SSH {

		ps := []string{c.CommandText}
		res, e = c.SshClient.RunCommandSsh(ps...)

	} else if c.Type == CommandType_REST {
		if c.RESTAuthType == RESTAuthType_None {
			httpRes, e = toolkit.HttpCall(c.RESTUrl, c.RESTMethod, nil, nil)
		} else if c.RESTAuthType == RESTAuthType_Basic {
			var config = map[string]interface{}{"auth": "basic", "user": c.RESTUser, "password": c.RESTPassword}
			httpRes, e = toolkit.HttpCall(c.RESTUrl, c.RESTMethod, nil, config)
			// httpRes, e = toolkit.HttpCall(c.RESTUrl, c.RESTMethod, nil, true, c.RESTUser, c.RESTPassword)
		}
		res = toolkit.HttpContentString(httpRes)
	}
	return res, e
}
Ejemplo n.º 2
0
func TestClose(t *testing.T) {
	r, e := call("/stop")
	if e != nil {
		t.Errorf("Fail: %s", e.Error())
	} else {
		t.Logf("Respond: %s", toolkit.HttpContentString(r))
	}
}
Ejemplo n.º 3
0
func TestServer(t *testing.T) {
	r, e := call("/")
	if e != nil {
		t.Errorf("Fail: %s", e.Error())
		return
	}

	if r.StatusCode != 200 {
		t.Errorf("Error: %s", e.Error())
		return
	}

	str := toolkit.HttpContentString(r)
	if str != "Welcome to Sebar Server" {
		t.Errorf("Invalid return")
	}
}
Ejemplo n.º 4
0
func (w *WebGrabberController) FetchContent(r *knot.WebContext) interface{} {
	r.Config.OutputType = knot.OutputJson

	payload := new(colonycore.WebGrabber)
	err := r.GetPayload(payload)
	if err != nil {
		return helper.CreateResult(false, nil, err.Error())
	}

	param := toolkit.M{} //.Set("formvalues", payload.Parameter)
	res, err := toolkit.HttpCall(payload.URL, payload.CallType, nil, param)
	if err != nil {
		return helper.CreateResult(false, nil, err.Error())
	}

	data := toolkit.HttpContentString(res)
	return helper.CreateResult(true, data, "")
}
Ejemplo n.º 5
0
func TestServer(t *testing.T) {
	r, e := call("/")
	if e != nil {
		t.Errorf("Fail: %s", e.Error())
		return
	}

	if r.StatusCode != 200 {
		t.Errorf("Error: %s", e.Error())
		return
	}

	str := toolkit.HttpContentString(r)
	want := "Welcome to Knot Server"
	if str != want {
		t.Errorf("Invalid return. Expecting %s got %s", want, str)
	}
}
Ejemplo n.º 6
0
Archivo: ping.go Proyecto: arfian/live
func (p *Ping) checkHttpBody() error {
	r, e := toolkit.HttpCall(p.Host, "GET", nil, false, "", "")
	if e != nil {
		return e
	}
	body := toolkit.HttpContentString(r)
	if p.HttpBodyType == HttpBody_Contains {
		if !strings.Contains(body, p.HttpBodySearch) {
			return fmt.Errorf("Phrase %s could not be found on response body", p.HttpBodySearch)
		}
	} else if p.HttpBodyType == HttpBody_Equals {
		if body != p.HttpBodySearch {
			return fmt.Errorf("Response is not valid. Expecting for %s", p.HttpBodySearch)
		}
	} else {
		return fmt.Errorf("Invalid parameter")
	}
	return nil
}
Ejemplo n.º 7
0
func main() {
	url := "http://www.dce.com.cn/PublicWeb/MainServlet"

	formvalues := toolkit.M{}.Set("Pu00231_Input.trade_date", "20151214").
		Set("Pu00231_Input.variety", "i").
		Set("Pu00231_Input.trade_type", "0").
		Set("Submit", "Go").
		Set("action", "Pu00231_result")

	config := toolkit.M{}.Set("formvalues", formvalues)

	r, e := toolkit.HttpCall(url, "POST", nil, config)
	if e != nil {
		fmt.Printf("ERROR:\n%s\n", e.Error())
		return
	}

	fmt.Printf("Result:\n%s\n", toolkit.HttpContentString(r))
}
Ejemplo n.º 8
0
func (w *WebGrabberController) FetchContent(r *knot.WebContext) interface{} {
	r.Config.OutputType = knot.OutputJson

	payload := struct {
		URL      string
		Method   string
		Payloads toolkit.M
	}{}
	err := r.GetPayload(&payload)
	if err != nil {
		return helper.CreateResult(false, nil, err.Error())
	}

	res, err := toolkit.HttpCall(payload.URL, payload.Method, nil, payload.Payloads)
	if err != nil {
		return helper.CreateResult(false, nil, err.Error())
	}

	data := toolkit.HttpContentString(res)
	return helper.CreateResult(true, data, "")
}
Ejemplo n.º 9
0
func (a *ConfigurationController) GetURL(k *knot.WebContext) interface{} {

	d := struct {
		URL       string
		Method    string
		Parameter tk.M
	}{}
	e := k.GetPayload(&d)
	if e != nil {
		fmt.Println(e)
	}
	k.Config.OutputType = knot.OutputJson
	fmt.Println(d)
	r, e := tk.HttpCall(d.URL, d.Method, nil, tk.M{}.Set("formvalues", d.Parameter))
	fmt.Println(e)
	if e != nil {
		return e.Error()
	} else {
		return tk.HttpContentString(r)
	}
}
Ejemplo n.º 10
0
func (s *Subscriber) Start(address string) error {
	s.Address = address
	s.Actions = map[string]FnTrigger{}
	s.MessageQues = map[string]*MessageQue{}
	broadcasterUrl := s.BroadcasterAddress + "/nodeadd?node=" + s.Address
	r, e := toolkit.HttpCall(broadcasterUrl, "GET", nil, nil)
	if e != nil {
		return fmt.Errorf("Unable to contact Broadcaster Server. %s", e.Error())
	}
	if sOk := toolkit.HttpContentString(r); sOk != "OK" {
		return fmt.Errorf("Unable to add %s as subsriber to %s. Error: %s", s.Address, s.BroadcasterAddress, sOk)
	}

	//-- inform subscriber if newkey is available to be collected
	s.Route("/newkey", func(k *knot.WebContext) interface{} {
		result := toolkit.NewResult()
		k.Config.OutputType = knot.OutputJson
		msg := new(MessageQue)
		e := k.GetPayload(&msg)
		if e != nil {
			result.Message = e.Error()
			result.Status = toolkit.Status_NOK
		} else {
			msg.Data = nil
			msg.Collected = false
			_, exist := s.MessageQues[msg.Key]
			if !exist {
				s.messageKeys = append(s.messageKeys, msg.Key)
			}
			s.MessageQues[msg.Key] = msg
			//s.messageKeys = append(s.messageKeys, msg.Key)
		}
		return result
	})

	s.Route("/getmsg", func(k *knot.WebContext) interface{} {
		k.Config.OutputType = knot.OutputJson
		key := k.Query("key")
		result := s.getMsgAsResult(key)
		return result
	})

	//-- replicate message to subscribe
	s.Route("/msg", func(k *knot.WebContext) interface{} {
		k.Config.OutputType = knot.OutputJson
		result := toolkit.NewResult()
		var payload Message
		eDecode := k.GetPayload(&payload)
		if eDecode != nil {
			result.Status = toolkit.Status_NOK
			result.Message = fmt.Sprintf("Subscriber Message Decode Error %s", eDecode.Error())
		} else {
			result.Data = payload
		}
		return result
	})

	//-- stop the server
	s.Route("/stop", func(k *knot.WebContext) interface{} {
		defer k.Server.Stop()
		k.Config.OutputType = knot.OutputJson
		result := new(toolkit.Result)
		result.Status = "OK"
		return result
	})

	go func() {
		s.Server.Listen()
	}()
	return nil
}