Skip to content

Commit

Permalink
Working server with only one adapter/model
Browse files Browse the repository at this point in the history
You can now specify on the command line the model to use, so that is not
under the client's responsability. The client should only know the
server's url. It's on the server's responsability to create the adapter,
read the model and load policies.

Change-Id: Ib7c5ef4454e58c09e56a9bea0407b4f61d301814
Signed-off-by: Loïc Collignon <loic.collignon@iot.bzh>
  • Loading branch information
Loïc Collignon committed Aug 21, 2019
1 parent 5b17273 commit fd5f4d1
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 72 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ casbin-server

.idea/
*.iml
.vscode/
8 changes: 7 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,12 @@ func main() {
var driverName string
var connectionString string
var dbSpecified bool
var modelFile string
flag.IntVar(&port, "port", 50051, "listening port")
flag.StringVar(&driverName, "driver", "postgres", "adapter's driver (can be 'file', 'mysql', 'postgres' or 'mssql')")
flag.StringVar(&connectionString, "connstr", "", "adapter's connection string or file path if driver is 'file'")
flag.BoolVar(&dbSpecified, "dbspecified", false, "indicates whether the db is specified or not in the connstr")
flag.StringVar(&modelFile, "model", "", "path of the model file")
flag.Parse()

if port < 1 || port > 65535 {
Expand All @@ -47,12 +49,16 @@ func main() {
panic("a connection string should be provided")
}

if modelFile == "" {
panic("a model file has to be specified")
}

lis, err := net.Listen("tcp", fmt.Sprintf(":%d", port))
if err != nil {
log.Fatalf("failed to listen: %v", err)
}
s := grpc.NewServer()
pb.RegisterCasbinServer(s, server.NewServer(driverName, connectionString, dbSpecified))
pb.RegisterCasbinServer(s, server.NewServer(driverName, connectionString, dbSpecified, modelFile))
// Register reflection service on gRPC server.
reflection.Register(s)
log.Println("Listening on", port)
Expand Down
109 changes: 50 additions & 59 deletions proto/casbin.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion proto/casbin.proto
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ service Casbin {
}

message NewEnforcerRequest {
string modelText = 1;
}

message NewEnforcerReply {
Expand Down
8 changes: 5 additions & 3 deletions server/enforcer.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,19 @@ import (
type Server struct {
enforcerMap map[int]*casbin.Enforcer
adapter persist.Adapter
modelFile string
}

// NewServer creates a new instance the server.
func NewServer(driverName string, connectionString string, dbSpecified bool) *Server {
func NewServer(driverName string, connectionString string, dbSpecified bool, modelFile string) *Server {
s := Server{}
var err error

s.adapter, err = newAdapter(driverName, connectionString, dbSpecified)
if err != nil {
panic(err)
}
s.modelFile = modelFile
s.enforcerMap = map[int]*casbin.Enforcer{}

return &s
Expand All @@ -62,7 +64,7 @@ func (s *Server) NewEnforcer(ctx context.Context, in *pb.NewEnforcerRequest) (*p
var e *casbin.Enforcer

if s.adapter == nil {
m, err := model.NewModelFromString(in.ModelText)
m, err := model.NewModelFromFile(s.modelFile)
if err != nil {
return &pb.NewEnforcerReply{Handler: 0}, err
}
Expand All @@ -72,7 +74,7 @@ func (s *Server) NewEnforcer(ctx context.Context, in *pb.NewEnforcerRequest) (*p
return &pb.NewEnforcerReply{Handler: 0}, err
}
} else {
m, err := model.NewModelFromString(in.ModelText)
m, err := model.NewModelFromFile(s.modelFile)
if err != nil {
return &pb.NewEnforcerReply{Handler: 0}, err
}
Expand Down
10 changes: 2 additions & 8 deletions server/test_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package server

import (
"context"
"io/ioutil"
"testing"

pb "github.com/casbin/casbin-server/proto"
Expand All @@ -29,15 +28,10 @@ type testEngine struct {
}

func newTestEngine(t *testing.T, from, connectStr string, modelLoc string) *testEngine {
s := NewServer(from, connectStr, false)
s := NewServer(from, connectStr, false, modelLoc)
ctx := context.Background()

modelText, err := ioutil.ReadFile(modelLoc)
if err != nil {
t.Fatal(err)
}

resp, err := s.NewEnforcer(ctx, &pb.NewEnforcerRequest{ModelText: string(modelText)})
resp, err := s.NewEnforcer(ctx, &pb.NewEnforcerRequest{})
if err != nil {
t.Fatal(err)
}
Expand Down

0 comments on commit fd5f4d1

Please sign in to comment.