Skip to content

Commit 0643b6e

Browse files
committed
Merge branch 'main' of github.com:casbin/casbin-mesh
2 parents 883fed3 + 4277623 commit 0643b6e

12 files changed

+974
-517
lines changed

Dockerfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ COPY ./go.mod ./
44
COPY ./go.sum ./
55
RUN go mod download
66
COPY . .
7-
RUN export GO111MODULE=on && CGO_ENABLED=0 GOOS=linux go build -ldflags "-s -w" -o build/casmesh cmd/app/main.go
7+
RUN export GO111MODULE=on && CGO_ENABLED=0 GOOS=linux go build -ldflags "-s -w" -o build/casmesh cmd/app/*.go
88

99

1010
FROM alpine:latest

cmd/cli/auth.go client/v2/auth.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
// specific language governing permissions and limitations
1616
// under the License.
1717

18-
package main
18+
package client
1919

2020
type AuthType = string
2121

cmd/cli/client.go client/v2/client.go

+31-34
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,18 @@
1-
// Licensed to the Apache Software Foundation (ASF) under one
2-
// or more contributor license agreements. See the NOTICE file
3-
// distributed with this work for additional information
4-
// regarding copyright ownership. The ASF licenses this file
5-
// to you under the Apache License, Version 2.0 (the
6-
// "License"); you may not use this file except in compliance
7-
// with the License. You may obtain a copy of the License at
1+
// Copyright 2022 The casbin-neo Authors. All Rights Reserved.
82
//
9-
// http://www.apache.org/licenses/LICENSE-2.0
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
106
//
11-
// Unless required by applicable law or agreed to in writing,
12-
// software distributed under the License is distributed on an
13-
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14-
// KIND, either express or implied. See the License for the
15-
// specific language governing permissions and limitations
16-
// under the License.
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
1714

18-
package main
15+
package client
1916

2017
import (
2118
"context"
@@ -29,23 +26,23 @@ import (
2926
"time"
3027
)
3128

32-
type client struct {
29+
type Client struct {
3330
grpcClient command.CasbinMeshClient
3431
}
3532

3633
var (
3734
MarshalFailed = errors.New("marshal failed")
3835
)
3936

40-
func (c client) ShowStats(ctx context.Context) ([]byte, error) {
37+
func (c Client) ShowStats(ctx context.Context) ([]byte, error) {
4138
resp, err := c.grpcClient.ShowStats(ctx, &command.StatsRequest{})
4239
if err != nil {
4340
return nil, err
4441
}
4542
return resp.Payload, nil
4643
}
4744

48-
func (c client) AddPolicies(ctx context.Context, namespace, sec, ptype string, rules [][]string) ([][]string, error) {
45+
func (c Client) AddPolicies(ctx context.Context, namespace, sec, ptype string, rules [][]string) ([][]string, error) {
4946
payload := command.AddPoliciesPayload{
5047
Sec: sec,
5148
PType: ptype,
@@ -70,7 +67,7 @@ func (c client) AddPolicies(ctx context.Context, namespace, sec, ptype string, r
7067
return command.ToStringArray(resp.EffectedRules), nil
7168
}
7269

73-
func (c client) RemovePolicies(ctx context.Context, namespace, sec, ptype string, rules [][]string) ([][]string, error) {
70+
func (c Client) RemovePolicies(ctx context.Context, namespace, sec, ptype string, rules [][]string) ([][]string, error) {
7471
payload := command.RemovePoliciesPayload{
7572
Sec: sec,
7673
PType: ptype,
@@ -95,7 +92,7 @@ func (c client) RemovePolicies(ctx context.Context, namespace, sec, ptype string
9592
return command.ToStringArray(resp.EffectedRules), nil
9693
}
9794

98-
func (c client) UpdatePolicies(ctx context.Context, namespace, sec, ptype string, old, new [][]string) (bool, error) {
95+
func (c Client) UpdatePolicies(ctx context.Context, namespace, sec, ptype string, old, new [][]string) (bool, error) {
9996
log.Printf("sec:%s,ptype:%s,or%v,nr:%v", sec, ptype, old, new)
10097
payload := command.UpdatePoliciesPayload{
10198
Sec: sec,
@@ -122,7 +119,7 @@ func (c client) UpdatePolicies(ctx context.Context, namespace, sec, ptype string
122119
return resp.Effected, nil
123120
}
124121

125-
func (c client) ListNamespaces(ctx context.Context) ([]string, error) {
122+
func (c Client) ListNamespaces(ctx context.Context) ([]string, error) {
126123
resp, err := c.grpcClient.ListNamespaces(ctx, &command.ListNamespacesRequest{})
127124
if err != nil {
128125
return nil, err
@@ -133,15 +130,15 @@ func (c client) ListNamespaces(ctx context.Context) ([]string, error) {
133130
return resp.Namespace, nil
134131
}
135132

136-
func (c client) ListPolicies(ctx context.Context, namespace string) ([][]string, error) {
133+
func (c Client) ListPolicies(ctx context.Context, namespace string) ([][]string, error) {
137134
resp, err := c.grpcClient.ListPolicies(ctx, &command.ListPoliciesRequest{Namespace: namespace})
138135
if err != nil {
139136
return nil, err
140137
}
141138
return command.ToStringArray(resp.Policies), nil
142139
}
143140

144-
func (c client) Enforce(ctx context.Context, namespace string, level command.EnforcePayload_Level, freshness int64, params ...interface{}) (bool, error) {
141+
func (c Client) Enforce(ctx context.Context, namespace string, level command.EnforcePayload_Level, freshness int64, params ...interface{}) (bool, error) {
145142
var B [][]byte
146143
for _, p := range params {
147144
b, err := json.Marshal(p)
@@ -170,7 +167,7 @@ func (c client) Enforce(ctx context.Context, namespace string, level command.Enf
170167
return result.Ok, nil
171168
}
172169

173-
func (c client) PrintModel(ctx context.Context, namespace string) (string, error) {
170+
func (c Client) PrintModel(ctx context.Context, namespace string) (string, error) {
174171
resp, err := c.grpcClient.PrintModel(ctx, &command.PrintModelRequest{Namespace: namespace})
175172
if err != nil {
176173
return "", err
@@ -181,31 +178,31 @@ func (c client) PrintModel(ctx context.Context, namespace string) (string, error
181178
return resp.Model, nil
182179
}
183180

184-
type options struct {
185-
target string
186-
authType AuthType
187-
username string
188-
password string
181+
type Options struct {
182+
Target string
183+
AuthType AuthType
184+
Username string
185+
Password string
189186
}
190187

191-
func NewClient(op options) *client {
188+
func NewClient(op Options) *Client {
192189
var opts []grpc.DialOption
193190
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
194191
defer cancel()
195192
opts = append(opts, grpc.WithInsecure())
196193
opts = append(opts, grpc.WithBlock())
197194

198-
switch op.authType {
195+
switch op.AuthType {
199196
case Basic:
200-
opts = append(opts, grpc.WithUnaryInterceptor(grpc_middleware.ChainUnaryClient(BasicAuthor(op.username, op.password))))
197+
opts = append(opts, grpc.WithUnaryInterceptor(grpc_middleware.ChainUnaryClient(BasicAuthor(op.Username, op.Password))))
201198
}
202199

203-
conn, err := grpc.DialContext(ctx, op.target, opts...)
200+
conn, err := grpc.DialContext(ctx, op.Target, opts...)
204201
if err != nil {
205202
log.Fatalf("fail to dial: %v", err)
206203
}
207204
log.Println("login success!")
208205
//defer conn.Close()
209206
c := command.NewCasbinMeshClient(conn)
210-
return &client{grpcClient: c}
207+
return &Client{grpcClient: c}
211208
}

cmd/cli/client_test.go client/v2/client_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
// specific language governing permissions and limitations
1616
// under the License.
1717

18-
package main
18+
package client
1919

2020
import (
2121
"context"
@@ -26,7 +26,7 @@ import (
2626
)
2727

2828
var (
29-
c *client
29+
c *Client
3030
)
3131

3232
func TestMain(m *testing.M) {

client/v2/go.mod

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
module github.com/casbin/casbin-mesh/client/v2
2+
3+
go 1.18
4+
5+
require (
6+
github.com/casbin/casbin-mesh v0.0.0-20220510133536-c1b32d87368a
7+
github.com/grpc-ecosystem/go-grpc-middleware v1.3.0
8+
google.golang.org/grpc v1.47.0
9+
)
10+
11+
require (
12+
github.com/golang/protobuf v1.5.2 // indirect
13+
golang.org/x/net v0.0.0-20201202161906-c7110b5ffcbb // indirect
14+
golang.org/x/sys v0.0.0-20210816183151-1e6c022a8912 // indirect
15+
golang.org/x/text v0.3.3 // indirect
16+
google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013 // indirect
17+
google.golang.org/protobuf v1.27.1 // indirect
18+
)

0 commit comments

Comments
 (0)