Skip to content

Commit

Permalink
Moved routeid to eskip package
Browse files Browse the repository at this point in the history
  • Loading branch information
danpersa committed Dec 29, 2015
1 parent 3f5adc1 commit 35ba6cc
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 33 deletions.
25 changes: 25 additions & 0 deletions eskip/eskip.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import (
"errors"
"fmt"
"strings"
"regexp"
"github.com/zalando/skipper/filters/flowid"
)

// Represents a matcher condition for incoming requests.
Expand Down Expand Up @@ -256,3 +258,26 @@ func ParseFilters(f string) ([]*Filter, error) {

return rs[0].filters, nil
}

const randomIdLength = 16

var routeIdRx = regexp.MustCompile("\\W")

// generate weak random id for a route if
// it doesn't have one.
func GenerateIfNeeded(existingId string) string {
if existingId != "" {
return existingId
}

// using this to avoid adding a new dependency.
id, err := flowid.NewFlowId(randomIdLength)
if err != nil {
return existingId
}

// replace characters that are not allowed
// for eskip route ids.
id = routeIdRx.ReplaceAllString(id, "x")
return "route" + id
}
3 changes: 1 addition & 2 deletions etcd/etcd.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ import (
log "github.com/Sirupsen/logrus"
"github.com/coreos/go-etcd/etcd"
"github.com/zalando/skipper/eskip"
"github.com/zalando/skipper/routeid"
"net/http"
"path"
)
Expand Down Expand Up @@ -239,7 +238,7 @@ func (c *Client) Delete(id string) error {

func (c *Client) UpsertAll(routes []*eskip.Route) error {
for _, r := range routes {
r.Id = routeid.GenerateIfNeeded(r.Id)
r.Id = eskip.GenerateIfNeeded(r.Id)
err := c.Upsert(r)
if err != nil {
return err
Expand Down
3 changes: 1 addition & 2 deletions innkeeper/eskiptojson.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package innkeeper

import (
"github.com/zalando/skipper/eskip"
"github.com/zalando/skipper/routeid"
)

func convertPathMatcher(r *eskip.Route) *pathMatcher {
Expand Down Expand Up @@ -95,7 +94,7 @@ func convertEskipToInnkeeper(routes []*eskip.Route) (data []*routeData) {

for _, r := range routes {

id := routeid.GenerateIfNeeded(r.Id)
id := eskip.GenerateIfNeeded(r.Id)
host := convertHost(r)
method := convertMethod(r)
pathMatch := convertPathMatcher(r)
Expand Down
29 changes: 0 additions & 29 deletions routeid/routeid.go

This file was deleted.

0 comments on commit 35ba6cc

Please sign in to comment.