Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangxu19830126 committed Jul 12, 2018
1 parent 373c056 commit d8e9bcc
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 10 deletions.
22 changes: 12 additions & 10 deletions pkg/pb/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,18 @@ import (

// ValidateRouting validate routing
func ValidateRouting(value *metapb.Routing) error {
if value.API == 0 {
return fmt.Errorf("missing api")
}

if value.ClusterID == 0 {
return fmt.Errorf("missing cluster")
}

if value.Name == "" {
return fmt.Errorf("missing name")
}

if value.TrafficRate <= 0 || value.TrafficRate > 100 {
return fmt.Errorf("error traffic rate: %d", value.TrafficRate)
}
Expand Down Expand Up @@ -48,16 +56,10 @@ func ValidateAPI(value *metapb.API) error {
return fmt.Errorf("missing api name")
}

if value.URLPattern == "" {
return fmt.Errorf("missing url pattern")
}

if value.Method == "" {
return fmt.Errorf("missing http method")
}

if _, err := regexp.Compile(value.URLPattern); err != nil {
return err
if value.URLPattern != "" {
if _, err := regexp.Compile(value.URLPattern); err != nil {
return err
}
}

return nil
Expand Down
21 changes: 21 additions & 0 deletions pkg/store/store_etcd.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/coreos/etcd/clientv3"
"github.com/coreos/etcd/mvcc/mvccpb"
pbutil "github.com/fagongzi/gateway/pkg/pb"
"github.com/fagongzi/gateway/pkg/pb/metapb"
"github.com/fagongzi/gateway/pkg/util"
"github.com/fagongzi/util/format"
Expand Down Expand Up @@ -138,6 +139,11 @@ func (e *EtcdStore) GetBindServers(id uint64) ([]uint64, error) {

// PutCluster add or update the cluster
func (e *EtcdStore) PutCluster(value *metapb.Cluster) (uint64, error) {
err := pbutil.ValidateCluster(value)
if err != nil {
return 0, err
}

return e.putPB(e.clustersDir, value, func(id uint64) {
value.ID = id
})
Expand All @@ -164,6 +170,11 @@ func (e *EtcdStore) GetCluster(id uint64) (*metapb.Cluster, error) {

// PutServer add or update the server
func (e *EtcdStore) PutServer(value *metapb.Server) (uint64, error) {
err := pbutil.ValidateServer(value)
if err != nil {
return 0, err
}

return e.putPB(e.serversDir, value, func(id uint64) {
value.ID = id
})
Expand All @@ -187,6 +198,11 @@ func (e *EtcdStore) GetServer(id uint64) (*metapb.Server, error) {

// PutAPI add or update a API
func (e *EtcdStore) PutAPI(value *metapb.API) (uint64, error) {
err := pbutil.ValidateAPI(value)
if err != nil {
return 0, err
}

return e.putPB(e.apisDir, value, func(id uint64) {
value.ID = id
})
Expand All @@ -210,6 +226,11 @@ func (e *EtcdStore) GetAPI(id uint64) (*metapb.API, error) {

// PutRouting add or update routing
func (e *EtcdStore) PutRouting(value *metapb.Routing) (uint64, error) {
err := pbutil.ValidateRouting(value)
if err != nil {
return 0, err
}

return e.putPB(e.routingsDir, value, func(id uint64) {
value.ID = id
})
Expand Down

0 comments on commit d8e9bcc

Please sign in to comment.