示例#1
0
func (vh *VirtualHost) Route(req *http.Request) (reverse.Endpoint, error) {
	if vh.store == nil {
		return nil, fmt.Errorf("virtual hosts not loaded.")
	}
	vh.RLock()
	defer vh.RUnlock()

	hostname := strings.Split(strings.ToLower(req.Host), ":")[0]
	matcher, exists := vh.hosts[hostname]
	if !exists {
		val, err := vh.store.Get(path.Join("/", hostname), true)
		if err != nil {
			return nil, err
		}

		var url string
		if err := util.Convert(val, &url); err != nil {
			return nil, err
		}

		endpoint, err := reverse.ParseUrl(url)
		if err != nil {
			return nil, err
		}

		vh.hosts[hostname] = endpoint
		return endpoint, nil
	}
	return matcher, nil
}
示例#2
0
func (vh *VirtualHost) SetHost(hostname, url string) error {
	if vh.store == nil {
		return fmt.Errorf("virtual hosts not loaded.")
	}
	vh.Lock()
	defer vh.Unlock()

	hostname = strings.ToLower(hostname)

	endpoint, err := reverse.ParseUrl(url)
	if err != nil {
		return err
	}

	if err := vh.store.Put(path.Join("/", hostname), url); err != nil {
		return err
	}

	vh.hosts[hostname] = endpoint
	vh.Info("virtual hosts set (%s) upstream to (%s)", hostname, url)
	return nil
}