Ejemplo n.º 1
0
func (c *Client) GetFrontend(fk engine.FrontendKey) (*engine.Frontend, error) {
	response, err := c.Get(c.endpoint("frontends", fk.Id), url.Values{})
	if err != nil {
		return nil, err
	}
	return engine.FrontendFromJSON(c.Registry.GetRouter(), response)
}
Ejemplo n.º 2
0
func (n *ng) parseFrontends(node *etcd.Node, skipMiddlewares ...bool) ([]engine.FrontendSpec, error) {
	frontendSpecs := make([]engine.FrontendSpec, len(node.Nodes))
	for idx, node := range node.Nodes {
		frontendId := suffix(node.Key)
		for _, node := range node.Nodes {
			switch suffix(node.Key) {
			case "frontend":
				frontend, err := engine.FrontendFromJSON(n.registry.GetRouter(), []byte(node.Value), frontendId)
				if err != nil {
					return nil, err
				}
				frontendSpecs[idx].Frontend = *frontend
			case "middlewares":
				if len(skipMiddlewares) == 1 && skipMiddlewares[0] {
					break
				}
				middlewares := make([]engine.Middleware, len(node.Nodes))
				for idx, node := range node.Nodes {
					middlewareId := suffix(node.Key)
					middleware, err := engine.MiddlewareFromJSON([]byte(node.Value), n.registry.GetSpec, middlewareId)
					if err != nil {
						return nil, err
					}
					middlewares[idx] = *middleware
				}
				frontendSpecs[idx].Middlewares = middlewares
			}
		}
		if frontendSpecs[idx].Frontend.Id != frontendId {
			return nil, fmt.Errorf("Frontend %s parameters missing", frontendId)
		}
	}
	return frontendSpecs, nil
}
Ejemplo n.º 3
0
func (n *ng) GetFrontend(key engine.FrontendKey) (*engine.Frontend, error) {
	frontendKey := n.path("frontends", key.Id, "frontend")

	bytes, err := n.getVal(frontendKey)
	if err != nil {
		return nil, err
	}
	return engine.FrontendFromJSON(n.registry.GetRouter(), []byte(bytes), key.Id)
}
Ejemplo n.º 4
0
func parseFrontendPack(router router.Router, v []byte) (*engine.Frontend, time.Duration, error) {
	var fp frontendReadPack
	if err := json.Unmarshal(v, &fp); err != nil {
		return nil, 0, err
	}
	if len(fp.Frontend) == 0 {
		return nil, 0, &scroll.MissingFieldError{Field: "Frontend"}
	}
	f, err := engine.FrontendFromJSON(router, fp.Frontend)
	if err != nil {
		return nil, 0, err
	}

	var ttl time.Duration
	if fp.TTL != "" {
		ttl, err = time.ParseDuration(fp.TTL)
		if err != nil {
			return nil, 0, err
		}
	}
	return f, ttl, nil
}
Ejemplo n.º 5
0
func (n *ng) parseFrontends(keyValues []*mvccpb.KeyValue, skipMiddlewares ...bool) ([]engine.FrontendSpec, error) {
	frontendSpecs := []engine.FrontendSpec{}
	for _, keyValue := range keyValues {
		if frontendIds := frontendIdRegex.FindStringSubmatch(string(keyValue.Key)); len(frontendIds) == 2 {
			frontendId := frontendIds[1]
			frontend, err := engine.FrontendFromJSON(n.registry.GetRouter(), []byte(keyValue.Value), frontendId)
			if err != nil {
				return nil, err
			}

			frontendSpec := engine.FrontendSpec{
				Frontend: *frontend,
			}

			if len(skipMiddlewares) != 1 || !skipMiddlewares[0] {
				//get all keys under this frontend
				subKeyValues := filterByPrefix(keyValues, string(keyValue.Key)) //Get all keys below this frontend "/vulcand/frontends/foo/*"

				middlewares := []engine.Middleware{}
				for _, subKeyValue := range subKeyValues {
					if middlewareId := suffix(string(subKeyValue.Key)); suffix(prefix(string(subKeyValue.Key))) == "middlewares" {
						middleware, err := engine.MiddlewareFromJSON([]byte(subKeyValue.Value), n.registry.GetSpec, middlewareId)
						if err != nil {
							return nil, err
						}
						middlewares = append(middlewares, *middleware)
					}
				}

				frontendSpec.Middlewares = middlewares
			}

			frontendSpecs = append(frontendSpecs, frontendSpec)
		}
	}

	return frontendSpecs, nil
}