Пример #1
0
// ResolveUrl replaces the host name with the ip address. Supports ipv4 and ipv6.
// If use in the place of host a path or a scheme for sockets, file or unix,
// ResolveUrl will only copy the url.
func ResolveUrl(url *url.URL) (*url.URL, error) {
	if url.Scheme == "file" || url.Scheme == "socket" || url.Scheme == "unix" {
		return utilUrl.Copy(url), nil
	}
	if len(url.Host) > 0 && url.Host[0] == '/' {
		return utilUrl.Copy(url), nil
	}
	if len(url.Host) >= 3 && url.Host[1] == ':' && url.Host[2] == '/' {
		return utilUrl.Copy(url), nil
	}
	r, err := regexp.Compile(`.*\(.*\)`)
	if err != nil {
		return nil, e.New(err)
	}
	mysqlNotation := r.FindAllString(url.Host, 1)
	if len(mysqlNotation) >= 1 {
		return utilUrl.Copy(url), nil
	}

	out := utilUrl.Copy(url)

	host, err := Resolve(url.Host)
	if err != nil {
		return nil, e.Forward(err)
	}
	out.Host = host
	return out, nil
}
Пример #2
0
func (c *client) do(method, path, params string, body []byte) (int, []byte, error) {
	url := utilUrl.Copy(c.url)
	url.Path += path
	url.RawQuery = params

	var buf io.Reader
	if len(body) > 0 {
		buf = bytes.NewBuffer(body)
	}

	req, err := http.NewRequest(method, url.String(), buf)
	if err != nil {
		return 0, nil, e.New("can't create request")
	}

	if c.url.User != nil {
		req.Header.Set("Authorization", authorizationHeader(url.User.String()))
	}
	req.Header.Set("Content-Type", "application/json")

	resp, err := HttpClient.Do(req)
	if err != nil {
		return 0, nil, e.Push(err, "can't put")
	}
	defer resp.Body.Close()

	if resp.StatusCode < http.StatusOK || resp.StatusCode >= http.StatusMultipleChoices {
		return resp.StatusCode, nil, e.New("wrong status code - %v: %v", resp.StatusCode, resp.Status)
	}

	var data []byte
	data, _ = ioutil.ReadAll(resp.Body)

	return resp.StatusCode, data, nil
}
Пример #3
0
// NewCouch creates and initializes a new instance of the Couch struct.
func NewCouch(url *url.URL, dbname string) *Couch {
	url = utilUrl.Copy(url)
	url.Path += "/" + dbname + "/"
	return &Couch{
		client: &client{url},
	}
}
Пример #4
0
// PingCouch tests if the database is online and operational.
func PingCouch(url *url.URL) error {
	url = utilUrl.Copy(url)
	url.Scheme = "http"

	err := couch.CreateDB(url, DbName)
	if err != nil && !e.Equal(err, couch.ErrDbExist) {
		return e.Forward(err)
	}
	defer couch.DeleteDB(url, DbName)

	c := couch.NewCouch(url, DbName)

	for i := 0; i < 10; i++ {
		t := couch.TestStruct{
			Id:   strconv.FormatInt(int64(i), 10),
			Data: i,
		}
		id, _, err := c.Put(t)
		if err != nil {
			return e.Forward(err)
		}
		if id != t.Id {
			return e.New("wrong id (%v)", id)
		}
	}
	di, err := couch.InfoDB(url, DbName)
	if err != nil {
		return e.Forward(err)
	}
	if di.Db_name != DbName {
		return e.New("wrong db name")
	}
	if di.Doc_count != 10 {
		return e.New("wrong document count (%v)", di.Doc_count)
	}
	for i := 0; i < 10; i++ {
		t := new(couch.TestStruct)
		err := c.Get(strconv.FormatInt(int64(i), 10), "", t)
		if err != nil {
			return e.Forward(err)
		}
		if t.Data != i {
			return e.New("retrieved wrong document (%v)", t.Data)
		}
	}
	_, err = c.Delete("9", "")
	if err != nil {
		return e.Forward(err)
	}
	t := new(couch.TestStruct)
	err = c.Get("9", "", t)
	if err != nil && !e.Equal(err, couch.ErrCantGetDoc) {
		return e.Forward(err)
	}
	err = couch.DeleteDB(url, DbName)
	if err != nil {
		return e.Forward(err)
	}
	_, err = couch.InfoDB(url, DbName)
	if err != nil && !e.Equal(err, couch.ErrDbNotFound) {
		return e.Push(err, "database not deleted")
	}
	return nil
}