Ejemplo n.º 1
0
func (c *Client) GetMiddleware(mk engine.MiddlewareKey) (*engine.Middleware, error) {
	data, err := c.Get(c.endpoint("frontends", mk.FrontendKey.Id, "middlewares", mk.Id), url.Values{})
	if err != nil {
		return nil, err
	}
	return engine.MiddlewareFromJSON(data, c.Registry.GetSpec)
}
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) GetMiddleware(key engine.MiddlewareKey) (*engine.Middleware, error) {
	mKey := n.path("frontends", key.FrontendKey.Id, "middlewares", key.Id)
	bytes, err := n.getVal(mKey)
	if err != nil {
		return nil, err
	}
	return engine.MiddlewareFromJSON([]byte(bytes), n.registry.GetSpec, key.Id)
}
Ejemplo n.º 4
0
func parseMiddlewarePack(v []byte, r *plugin.Registry) (*engine.Middleware, time.Duration, error) {
	var mp middlewareReadPack
	if err := json.Unmarshal(v, &mp); err != nil {
		return nil, 0, err
	}
	if len(mp.Middleware) == 0 {
		return nil, 0, &scroll.MissingFieldError{Field: "Middleware"}
	}
	f, err := engine.MiddlewareFromJSON(mp.Middleware, r.GetSpec)
	if err != nil {
		return nil, 0, err
	}
	var ttl time.Duration
	if mp.TTL != "" {
		ttl, err = time.ParseDuration(mp.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
}