From 1aa2a226c9aa551edcdf5d2d4912852e406f40ee Mon Sep 17 00:00:00 2001 From: Ali Shokoohi Date: Wed, 18 Sep 2024 20:29:29 +0330 Subject: [PATCH 1/8] Added .env to .gitignore file --- .gitignore | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.gitignore b/.gitignore index 5371c0a..74ec4a5 100644 --- a/.gitignore +++ b/.gitignore @@ -14,3 +14,6 @@ casbin-server .idea/ *.iml + +# Environment Variables +.env From 8a2807a8f068faccd3aaeb7b090925d5dc7183b4 Mon Sep 17 00:00:00 2001 From: Ali Shokoohi Date: Wed, 18 Sep 2024 20:40:20 +0330 Subject: [PATCH 2/8] Added env example file --- env.example | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 env.example diff --git a/env.example b/env.example new file mode 100644 index 0000000..ef4291a --- /dev/null +++ b/env.example @@ -0,0 +1,7 @@ +# Database Configuration +DB_HOST="127.0.0.1" +DB_PORT="5432" +DB_USERNAME="casbin" +DB_NAME="casbin" +DB_PASSWORD="casbin123" +CONNECTION_CONFIG_PATH="config/connection_config.json" From 10d7c6b3ef5b0b73f2b2cbf456bca7f37f95395f Mon Sep 17 00:00:00 2001 From: Ali Shokoohi Date: Wed, 18 Sep 2024 21:45:37 +0330 Subject: [PATCH 3/8] Added gin handler --- dto/enforce.go | 5 ++++ go.mod | 23 ++++++++++++++---- go.sum | 45 ++++++++++++++++++++++++++++++++++++ handler/gin/handler.go | 43 ++++++++++++++++++++++++++++++++++ handler/handler_interface.go | 12 ++++++++++ 5 files changed, 123 insertions(+), 5 deletions(-) create mode 100644 dto/enforce.go create mode 100644 handler/gin/handler.go create mode 100644 handler/handler_interface.go diff --git a/dto/enforce.go b/dto/enforce.go new file mode 100644 index 0000000..57269fb --- /dev/null +++ b/dto/enforce.go @@ -0,0 +1,5 @@ +package dto + +type EnforceResponse struct { + Allowed bool `json:"allowed"` +} diff --git a/go.mod b/go.mod index 226bac0..0c4771e 100644 --- a/go.mod +++ b/go.mod @@ -5,17 +5,23 @@ go 1.19 require ( github.com/casbin/casbin/v2 v2.60.0 github.com/casbin/gorm-adapter/v3 v3.14.0 - github.com/stretchr/testify v1.8.0 + github.com/stretchr/testify v1.8.1 google.golang.org/grpc v1.42.0 - google.golang.org/protobuf v1.27.1 + google.golang.org/protobuf v1.28.1 ) require ( github.com/Knetic/govaluate v3.0.1-0.20171022003610-9aa49832a739+incompatible // indirect github.com/davecgh/go-spew v1.1.1 // indirect + github.com/gin-contrib/sse v0.1.0 // indirect + github.com/gin-gonic/gin v1.8.2 // indirect github.com/glebarez/go-sqlite v1.19.1 // indirect github.com/glebarez/sqlite v1.5.0 // indirect + github.com/go-playground/locales v0.14.0 // indirect + github.com/go-playground/universal-translator v0.18.0 // indirect + github.com/go-playground/validator/v10 v10.11.1 // indirect github.com/go-sql-driver/mysql v1.6.0 // indirect + github.com/goccy/go-json v0.9.11 // indirect github.com/golang-sql/civil v0.0.0-20220223132316-b832511892a9 // indirect github.com/golang-sql/sqlexp v0.1.0 // indirect github.com/golang/protobuf v1.5.0 // indirect @@ -30,16 +36,23 @@ require ( github.com/jackc/pgx/v4 v4.17.2 // indirect github.com/jinzhu/inflection v1.0.0 // indirect github.com/jinzhu/now v1.1.5 // indirect + github.com/json-iterator/go v1.1.12 // indirect github.com/kr/pretty v0.3.0 // indirect + github.com/leodido/go-urn v1.2.1 // indirect github.com/mattn/go-isatty v0.0.16 // indirect github.com/microsoft/go-mssqldb v0.17.0 // indirect + github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 // indirect + github.com/modern-go/reflect2 v1.0.2 // indirect + github.com/pelletier/go-toml/v2 v2.0.6 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0 // indirect + github.com/ugorji/go/codec v1.2.7 // indirect golang.org/x/crypto v0.0.0-20221005025214-4161e89ecf1b // indirect - golang.org/x/net v0.0.0-20220425223048-2871e0cb64e4 // indirect - golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab // indirect - golang.org/x/text v0.3.7 // indirect + golang.org/x/net v0.4.0 // indirect + golang.org/x/sys v0.3.0 // indirect + golang.org/x/text v0.5.0 // indirect google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013 // indirect + gopkg.in/yaml.v2 v2.4.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect gorm.io/driver/mysql v1.4.1 // indirect gorm.io/driver/postgres v1.4.4 // indirect diff --git a/go.sum b/go.sum index df31edd..d974e34 100644 --- a/go.sum +++ b/go.sum @@ -43,15 +43,28 @@ github.com/envoyproxy/go-control-plane v0.9.9-0.20201210154907-fd9021fe5dad/go.m github.com/envoyproxy/go-control-plane v0.9.10-0.20210907150352-cf90f659a021/go.mod h1:AFq3mo9L8Lqqiid3OhADV3RfLJnjiw63cSpi+fDTRC0= github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c= github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04= +github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE= +github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI= +github.com/gin-gonic/gin v1.8.2 h1:UzKToD9/PoFj/V4rvlKqTRKnQYyz8Sc1MJlv4JHPtvY= +github.com/gin-gonic/gin v1.8.2/go.mod h1:qw5AYuDrzRTnhvusDsrov+fDIxp9Dleuu12h8nfB398= github.com/glebarez/go-sqlite v1.19.1 h1:o2XhjyR8CQ2m84+bVz10G0cabmG0tY4sIMiCbrcUTrY= github.com/glebarez/go-sqlite v1.19.1/go.mod h1:9AykawGIyIcxoSfpYWiX1SgTNHTNsa/FVc75cDkbp4M= github.com/glebarez/sqlite v1.5.0 h1:+8LAEpmywqresSoGlqjjT+I9m4PseIM3NcerIJ/V7mk= github.com/glebarez/sqlite v1.5.0/go.mod h1:0wzXzTvfVJIN2GqRhCdMbnYd+m+aH5/QV7B30rM6NgY= github.com/go-kit/log v0.1.0/go.mod h1:zbhenjAZHb184qTLMA9ZjW7ThYL0H2mk7Q6pNt4vbaY= github.com/go-logfmt/logfmt v0.5.0/go.mod h1:wCYkCAKZfumFQihp8CzCvQ3paCTfi41vtzG1KdI/P7A= +github.com/go-playground/assert/v2 v2.0.1/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4= +github.com/go-playground/locales v0.14.0 h1:u50s323jtVGugKlcYeyzC0etD1HifMjqmJqb8WugfUU= +github.com/go-playground/locales v0.14.0/go.mod h1:sawfccIbzZTqEDETgFXqTho0QybSa7l++s0DH+LDiLs= +github.com/go-playground/universal-translator v0.18.0 h1:82dyy6p4OuJq4/CByFNOn/jYrnRPArHwAcmLoJZxyho= +github.com/go-playground/universal-translator v0.18.0/go.mod h1:UvRDBj+xPUEGrFYl+lu/H90nyDXpg0fqeB/AQUGNTVA= +github.com/go-playground/validator/v10 v10.11.1 h1:prmOlTVv+YjZjmRmNSF3VmspqJIxJWXmqUsHwfTRRkQ= +github.com/go-playground/validator/v10 v10.11.1/go.mod h1:i+3WkQ1FvaUjjxh1kSvIA4dMGDBiPU55YFDl0WbKdWU= github.com/go-sql-driver/mysql v1.6.0 h1:BCTh4TKNUYmOmMUcQ3IipzF5prigylS7XXjEkfCHuOE= github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg= github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY= +github.com/goccy/go-json v0.9.11 h1:/pAaQDLHEoCq/5FFmSKBswWmK6H0e8g4159Kc/X/nqk= +github.com/goccy/go-json v0.9.11/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I= github.com/gofrs/uuid v4.0.0+incompatible h1:1SD/1F5pU8p29ybwgQSwpQk+mwdRrXCYuPhW6m+TnJw= github.com/gofrs/uuid v4.0.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM= github.com/golang-jwt/jwt v3.2.1+incompatible/go.mod h1:8pz2t5EyA70fFQQSrl6XZXzqecmYZeUEB8OUGHkxJ+I= @@ -87,6 +100,7 @@ github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/ github.com/google/go-cmp v0.5.3/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU= github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI= github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= @@ -145,11 +159,14 @@ github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkr github.com/jinzhu/now v1.1.4/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ= github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= +github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= +github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51/go.mod h1:CzGEWj7cYgsdH8dAjBGEr58BoE7ScuLd+fwFZ44+/x8= github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= github.com/konsorten/go-windows-terminal-sequences v1.0.2/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= +github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0= github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= @@ -158,6 +175,8 @@ github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw= +github.com/leodido/go-urn v1.2.1 h1:BqpAaACuzVSgi/VLzGZIobT2z4v53pjosyNd9Yv6n/w= +github.com/leodido/go-urn v1.2.1/go.mod h1:zt4jvISO2HfUBqxjfIshjdMTYS56ZS/qv49ictyFfxY= github.com/lib/pq v1.0.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= github.com/lib/pq v1.1.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= github.com/lib/pq v1.2.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= @@ -173,10 +192,17 @@ github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/ github.com/mattn/go-sqlite3 v1.14.15/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg= github.com/microsoft/go-mssqldb v0.17.0 h1:Fto83dMZPnYv1Zwx5vHHxpNraeEaUlQ/hhHLgZiaenE= github.com/microsoft/go-mssqldb v0.17.0/go.mod h1:OkoNGhGEs8EZqchVTtochlXruEhEOaO4S0d2sB5aeGQ= +github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc= +github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= +github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M= +github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk= github.com/modocache/gover v0.0.0-20171022184752-b58185e213c5/go.mod h1:caMODM3PzxT8aQXRPkAt8xlV/e7d7w8GM5g0fa5F0D8= github.com/montanaflynn/stats v0.6.6/go.mod h1:etXPPgVO6n31NxCd9KQUMvCM+ve0ruNzt6R8Bnaayow= +github.com/pelletier/go-toml/v2 v2.0.6 h1:nrzqCb7j9cDFj2coyLNLaZuJTLjWjlaz6nvTvIwycIU= +github.com/pelletier/go-toml/v2 v2.0.6/go.mod h1:eumQOmlWiOPt5WriQQqoM5y18pDHwha2N+QD+EUNTek= github.com/pkg/browser v0.0.0-20210115035449-ce105d075bb4/go.mod h1:N6UoU20jOqggOuDwUaBQpluzLNDqif3kq9z2wpdYEfQ= github.com/pkg/browser v0.0.0-20210911075715-681adbf594b8/go.mod h1:HKlIX3XHQyzLZPlr7++PzdhaXEj94dEiJgZDTsxEqUI= +github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA= github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I= github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= @@ -188,6 +214,7 @@ github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6L github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4= github.com/rogpeppe/go-internal v1.6.1 h1:/FiVV8dS/e+YqF2JvO3yXRFbBLTIuSDkuC7aBOAvL+k= github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc= +github.com/rogpeppe/go-internal v1.8.0/go.mod h1:WmiCO8CzOY8rg0OYDC4/i/2WRWAB6poM+XZ2dLUbcbE= github.com/rs/xid v1.2.1/go.mod h1:+uKXf+4Djp6Md1KODXJxgGQPKngRmWyn10oCKFzNHOQ= github.com/rs/zerolog v1.13.0/go.mod h1:YbFCdg8HfsridGWAh22vktObvhZbQsZXe4/zB0OKkWU= github.com/rs/zerolog v1.15.0/go.mod h1:xYTKnLHcpfU2225ny5qZjxnj9NvkumZYjJHlAThCjNc= @@ -201,14 +228,20 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+ github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.2.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE= github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= +github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= +github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk= github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= +github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= +github.com/ugorji/go v1.2.7/go.mod h1:nF9osbDWLy6bDVv/Rtoh6QgnvNDpmCalQV5urGCCS6M= +github.com/ugorji/go/codec v1.2.7 h1:YPXUKf7fYbp/y8xloBqZOw2qaVggbfwMlI8WM3wZUJ0= +github.com/ugorji/go/codec v1.2.7/go.mod h1:WGN1fab3R1fzQlVQTkfxVtIBhWDRqOviHU95kRgeqEY= github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/zenazn/goji v0.9.0/go.mod h1:7S9M489iMyHBNxwZnk9/EHS098H4/F6TATF2mIxtB1Q= go.opentelemetry.io/proto/otlp v0.7.0/go.mod h1:PqfVotwruBrMGOCsRd/89rSnXhoiJIqeYNgFYFoEGnI= @@ -232,6 +265,7 @@ golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPh golang.org/x/crypto v0.0.0-20201203163018-be400aefbc4c/go.mod h1:jdWPYTVW3xRLrWPugEBEK3UY2ZEsg3UU495nc5E+M+I= golang.org/x/crypto v0.0.0-20210616213533-5ff15b29337e/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= +golang.org/x/crypto v0.0.0-20211215153901-e495a2d5b3d3/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= golang.org/x/crypto v0.0.0-20220511200225-c6db032c6c88/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= golang.org/x/crypto v0.0.0-20220722155217-630584e8d5aa/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= golang.org/x/crypto v0.0.0-20221005025214-4161e89ecf1b h1:huxqepDufQpLLIRXiVkTvnxrzJlpwmIWAObmcCcUFr0= @@ -259,6 +293,8 @@ golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= golang.org/x/net v0.0.0-20220425223048-2871e0cb64e4 h1:HVyaeDAYux4pnY+D/SiwmLOR36ewZ4iGQIIrtnuCjFA= golang.org/x/net v0.0.0-20220425223048-2871e0cb64e4/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk= +golang.org/x/net v0.4.0 h1:Q5QPcMlvfxFTAPV0+07Xz/MpK9NTXu2VDUuy0FeMfaU= +golang.org/x/net v0.4.0/go.mod h1:MBQ8lrhLObU/6UmLb4fmbmk5OcyYmqtbGd/9yIeKjEE= golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= @@ -284,11 +320,14 @@ golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210616045830-e2b7044e8c71/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20210806184541-e5e7981a1069/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20211007075335-d3039528d8ac/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220224120231-95c6836cb0e7/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab h1:2QkjZIsXupsJbJIdSjjUOgWK3aEtzyuh2mPt3l/CkeU= golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.3.0 h1:w8ZOecv6NaNa/zC8944JTU3vz4u6Lagfk4RPQxv92NQ= +golang.org/x/sys v0.3.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= @@ -299,6 +338,8 @@ golang.org/x/text v0.3.4/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk= golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= +golang.org/x/text v0.5.0 h1:OLmvp0KP+FVG99Ct/qFiL/Fhk4zp4QQnZ7b2U+5piUM= +golang.org/x/text v0.5.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY= @@ -347,15 +388,19 @@ google.golang.org/protobuf v1.25.0/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlba google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= google.golang.org/protobuf v1.27.1 h1:SnqbnDw1V7RiZcXPx5MEeqPv2s79L9i7BJUlG/+RurQ= google.golang.org/protobuf v1.27.1/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= +google.golang.org/protobuf v1.28.1 h1:d0NfwRgPtno5B1Wa6L2DAG+KivqkdutMf1UhdNx175w= +google.golang.org/protobuf v1.28.1/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI= gopkg.in/inconshreveable/log15.v2 v2.0.0-20180818164646-67afb5ed74ec/go.mod h1:aPpfJ7XW+gOuirDoZ8gHhLh3kZ1B08FtV2bbmy7Jv3s= gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.3/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/handler/gin/handler.go b/handler/gin/handler.go new file mode 100644 index 0000000..15be710 --- /dev/null +++ b/handler/gin/handler.go @@ -0,0 +1,43 @@ +package gin + +import ( + "net/http" + + "github.com/casbin/casbin-server/dto" + "github.com/casbin/casbin-server/handler" + "github.com/gin-gonic/gin" +) + +type GinContext struct { + ctx *gin.Context +} + +func (g *GinContext) Bind(v interface{}) error { + return g.ctx.Bind(v) +} + +func (g *GinContext) JSON(statusCode int, v interface{}) error { + g.ctx.JSON(statusCode, v) + return nil +} + +func (g *GinContext) Param(key string) string { + return g.ctx.Param(key) +} + +func (g *GinContext) QueryParam(key string) string { + return g.ctx.Query(key) +} + +type httpHandler struct{} + +func NewHttpHandler() handler.HttpHandler { + return &httpHandler{} +} + +func (h *httpHandler) Enforce(c handler.Context) { + response := dto.EnforceResponse{ + Allowed: true, + } + c.JSON(http.StatusOK, response) +} diff --git a/handler/handler_interface.go b/handler/handler_interface.go new file mode 100644 index 0000000..b7b496c --- /dev/null +++ b/handler/handler_interface.go @@ -0,0 +1,12 @@ +package handler + +type HttpHandler interface { + Enforce(c Context) +} + +type Context interface { + Bind(interface{}) error + JSON(int, interface{}) error + Param(string) string + QueryParam(string) string +} From 8dbdc1f810a46706282f96ac4901face5d83ae04 Mon Sep 17 00:00:00 2001 From: Ali Shokoohi Date: Wed, 18 Sep 2024 22:15:58 +0330 Subject: [PATCH 4/8] Added router --- handler/gin/handler.go | 10 +++--- main.go | 73 +++++++++++++++++++++++++++----------- router/gin/gin.go | 26 ++++++++++++++ router/router_interface.go | 10 ++++++ 4 files changed, 94 insertions(+), 25 deletions(-) create mode 100644 router/gin/gin.go create mode 100644 router/router_interface.go diff --git a/handler/gin/handler.go b/handler/gin/handler.go index 15be710..7520ce4 100644 --- a/handler/gin/handler.go +++ b/handler/gin/handler.go @@ -9,24 +9,24 @@ import ( ) type GinContext struct { - ctx *gin.Context + Ctx *gin.Context } func (g *GinContext) Bind(v interface{}) error { - return g.ctx.Bind(v) + return g.Ctx.Bind(v) } func (g *GinContext) JSON(statusCode int, v interface{}) error { - g.ctx.JSON(statusCode, v) + g.Ctx.JSON(statusCode, v) return nil } func (g *GinContext) Param(key string) string { - return g.ctx.Param(key) + return g.Ctx.Param(key) } func (g *GinContext) QueryParam(key string) string { - return g.ctx.Query(key) + return g.Ctx.Query(key) } type httpHandler struct{} diff --git a/main.go b/main.go index e13a669..f0dd43a 100644 --- a/main.go +++ b/main.go @@ -21,32 +21,65 @@ import ( "fmt" "log" "net" + "sync" + ginHandler "github.com/casbin/casbin-server/handler/gin" pb "github.com/casbin/casbin-server/proto" + "github.com/casbin/casbin-server/router" + ginRouter "github.com/casbin/casbin-server/router/gin" "github.com/casbin/casbin-server/server" "google.golang.org/grpc" "google.golang.org/grpc/reflection" ) func main() { - var port int - flag.IntVar(&port, "port", 50051, "listening port") - flag.Parse() - - if port < 1 || port > 65535 { - panic(fmt.Sprintf("invalid port number: %d", port)) - } - - 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()) - // Register reflection service on gRPC server. - reflection.Register(s) - log.Println("Listening on", port) - if err := s.Serve(lis); err != nil { - log.Fatalf("failed to serve: %v", err) - } + wg := sync.WaitGroup{} + wg.Add(1) + go func() { + defer wg.Done() + var port int + flag.IntVar(&port, "port", 50051, "gRPC listening port") + flag.Parse() + + if port < 1 || port > 65535 { + panic(fmt.Sprintf("invalid gRPC port number: %d", port)) + } + + 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()) + // Register reflection service on gRPC server. + reflection.Register(s) + log.Println("gRPC listening on", port) + if err := s.Serve(lis); err != nil { + log.Fatalf("failed to gRPC serve: %v", err) + } + }() + wg.Add(1) + go func() { + defer wg.Done() + var port int + flag.IntVar(&port, "http-port", 8585, "http listening port") + flag.Parse() + if port < 1 || port > 65535 { + panic(fmt.Sprintf("invalid http port number: %d", port)) + } + var r router.Router + r = ginRouter.New() // or echoRouter.New() + // Define handlers + h := ginHandler.NewHttpHandler() + r.POST("/authorize", h.Enforce) + + // Start the server + httpAddr := fmt.Sprintf(":%d", port) + log.Println("http listening on", httpAddr) + if err := r.Serve(httpAddr); err != nil { + log.Fatalf("failed to http serve: %v", err) + } + + }() + wg.Wait() } diff --git a/router/gin/gin.go b/router/gin/gin.go new file mode 100644 index 0000000..19f90a7 --- /dev/null +++ b/router/gin/gin.go @@ -0,0 +1,26 @@ +package gin + +import ( // Import the handler package that contains Context and HandlerFunc + + ginHandler "github.com/casbin/casbin-server/handler/gin" + "github.com/casbin/casbin-server/router" + "github.com/gin-gonic/gin" +) + +type GinRouter struct { + engine *gin.Engine +} + +func New() *GinRouter { + return &GinRouter{engine: gin.Default()} +} + +func (r *GinRouter) POST(path string, handler router.HandlerFunc) { + r.engine.POST(path, func(c *gin.Context) { + handler(&ginHandler.GinContext{Ctx: c}) // Convert gin.Context to your custom Context + }) +} + +func (r *GinRouter) Serve(addr string) error { + return r.engine.Run(addr) +} diff --git a/router/router_interface.go b/router/router_interface.go new file mode 100644 index 0000000..8187aba --- /dev/null +++ b/router/router_interface.go @@ -0,0 +1,10 @@ +package router + +import "github.com/casbin/casbin-server/handler" + +type Router interface { + POST(path string, handler HandlerFunc) + Serve(addr string) error +} + +type HandlerFunc func(handler.Context) From 37705e9f890e95e6d1baa751337e893207e7237a Mon Sep 17 00:00:00 2001 From: Ali Shokoohi Date: Fri, 20 Sep 2024 18:32:22 +0330 Subject: [PATCH 5/8] Completed request methods --- router/gin/gin.go | 22 +++++++++++++++++++++- router/router_interface.go | 4 ++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/router/gin/gin.go b/router/gin/gin.go index 19f90a7..5ceb3cc 100644 --- a/router/gin/gin.go +++ b/router/gin/gin.go @@ -15,12 +15,32 @@ func New() *GinRouter { return &GinRouter{engine: gin.Default()} } +func (r *GinRouter) GET(path string, handler router.HandlerFunc) { + r.engine.GET(path, func(c *gin.Context) { + handler(&ginHandler.GinContext{Ctx: c}) // Convert gin.Context to your custom Context + }) +} + func (r *GinRouter) POST(path string, handler router.HandlerFunc) { r.engine.POST(path, func(c *gin.Context) { handler(&ginHandler.GinContext{Ctx: c}) // Convert gin.Context to your custom Context }) } - +func (r *GinRouter) PUT(path string, handler router.HandlerFunc) { + r.engine.PUT(path, func(c *gin.Context) { + handler(&ginHandler.GinContext{Ctx: c}) // Convert gin.Context to your custom Context + }) +} +func (r *GinRouter) DELETE(path string, handler router.HandlerFunc) { + r.engine.DELETE(path, func(c *gin.Context) { + handler(&ginHandler.GinContext{Ctx: c}) // Convert gin.Context to your custom Context + }) +} +func (r *GinRouter) OPTIONS(path string, handler router.HandlerFunc) { + r.engine.OPTIONS(path, func(c *gin.Context) { + handler(&ginHandler.GinContext{Ctx: c}) // Convert gin.Context to your custom Context + }) +} func (r *GinRouter) Serve(addr string) error { return r.engine.Run(addr) } diff --git a/router/router_interface.go b/router/router_interface.go index 8187aba..88340f3 100644 --- a/router/router_interface.go +++ b/router/router_interface.go @@ -3,7 +3,11 @@ package router import "github.com/casbin/casbin-server/handler" type Router interface { + GET(path string, handler HandlerFunc) POST(path string, handler HandlerFunc) + PUT(path string, handler HandlerFunc) + DELETE(path string, handler HandlerFunc) + OPTIONS(path string, handler HandlerFunc) Serve(addr string) error } From b29c204620d1c602aa258ae5d40518f79f1e726e Mon Sep 17 00:00:00 2001 From: Ali Shokoohi Date: Fri, 20 Sep 2024 20:58:46 +0330 Subject: [PATCH 6/8] Connect handler layer into server layer --- dto/enforce.go | 10 +++- handler/gin/handler.go | 61 +++++++++++++++++++++--- handler/handler_interface.go | 1 + main.go | 14 +++++- router/gin/gin.go | 46 ------------------- router/gin/router.go | 89 ++++++++++++++++++++++++++++++++++++ router/router_interface.go | 7 ++- 7 files changed, 172 insertions(+), 56 deletions(-) delete mode 100644 router/gin/gin.go create mode 100644 router/gin/router.go diff --git a/dto/enforce.go b/dto/enforce.go index 57269fb..641f944 100644 --- a/dto/enforce.go +++ b/dto/enforce.go @@ -1,5 +1,13 @@ package dto +import ( + pb "github.com/casbin/casbin-server/proto" +) + +type EnforceRequest struct { + pb.EnforceRequest +} + type EnforceResponse struct { - Allowed bool `json:"allowed"` + pb.BoolReply } diff --git a/handler/gin/handler.go b/handler/gin/handler.go index 7520ce4..ad1c3ca 100644 --- a/handler/gin/handler.go +++ b/handler/gin/handler.go @@ -1,10 +1,14 @@ package gin import ( + "context" + "log" "net/http" "github.com/casbin/casbin-server/dto" "github.com/casbin/casbin-server/handler" + "github.com/casbin/casbin-server/proto" + server "github.com/casbin/casbin-server/server" "github.com/gin-gonic/gin" ) @@ -16,6 +20,10 @@ func (g *GinContext) Bind(v interface{}) error { return g.Ctx.Bind(v) } +func (g *GinContext) ShouldBind(v interface{}) error { + return g.Ctx.ShouldBind(v) +} + func (g *GinContext) JSON(statusCode int, v interface{}) error { g.Ctx.JSON(statusCode, v) return nil @@ -29,15 +37,56 @@ func (g *GinContext) QueryParam(key string) string { return g.Ctx.Query(key) } -type httpHandler struct{} +type httpHandler struct { + server *server.Server +} -func NewHttpHandler() handler.HttpHandler { - return &httpHandler{} +func NewHttpHandler( + server *server.Server, +) handler.HttpHandler { + return &httpHandler{ + server: server, + } } func (h *httpHandler) Enforce(c handler.Context) { - response := dto.EnforceResponse{ - Allowed: true, + var response dto.EnforceResponse + var request dto.EnforceRequest + ctx := context.Background() + err := c.ShouldBind(&request) + if err != nil { + c.JSON(http.StatusBadRequest, response) + return + } + // Create New Enforcer if input handler == -1 + if request.GetEnforcerHandler() == -1 { + e, err := h.server.NewEnforcer(ctx, &proto.NewEnforcerRequest{AdapterHandle: -1}) + if err != nil { + log.Println("Error at calling NewEnforcer in Enforce handler:", err.Error()) + c.JSON(http.StatusInternalServerError, response) + return + } + request.EnforcerHandler = e.GetHandler() + } + allowed, err := h.server.Enforce( + ctx, + &proto.EnforceRequest{ + EnforcerHandler: request.EnforcerHandler, + Params: request.Params, + }, + ) + if err != nil { + log.Println("Error at calling Enforce in Enforce handler:", err.Error()) + c.JSON(http.StatusInternalServerError, response) + return + } + response.BoolReply = *allowed + + if response.GetRes() { + c.JSON(http.StatusOK, response) + return + } else { + c.JSON(http.StatusForbidden, response) + return } - c.JSON(http.StatusOK, response) } diff --git a/handler/handler_interface.go b/handler/handler_interface.go index b7b496c..ff1a71e 100644 --- a/handler/handler_interface.go +++ b/handler/handler_interface.go @@ -6,6 +6,7 @@ type HttpHandler interface { type Context interface { Bind(interface{}) error + ShouldBind(interface{}) error JSON(int, interface{}) error Param(string) string QueryParam(string) string diff --git a/main.go b/main.go index f0dd43a..080b441 100644 --- a/main.go +++ b/main.go @@ -69,9 +69,19 @@ func main() { } var r router.Router r = ginRouter.New() // or echoRouter.New() + server := server.NewServer() // Define handlers - h := ginHandler.NewHttpHandler() - r.POST("/authorize", h.Enforce) + h := ginHandler.NewHttpHandler(server) + api := r.Group("/api") + { + v1 := api.Group("/v1") + { + enforce := v1.Group("/enforce") + { + enforce.POST("", h.Enforce) + } + } + } // Start the server httpAddr := fmt.Sprintf(":%d", port) diff --git a/router/gin/gin.go b/router/gin/gin.go deleted file mode 100644 index 5ceb3cc..0000000 --- a/router/gin/gin.go +++ /dev/null @@ -1,46 +0,0 @@ -package gin - -import ( // Import the handler package that contains Context and HandlerFunc - - ginHandler "github.com/casbin/casbin-server/handler/gin" - "github.com/casbin/casbin-server/router" - "github.com/gin-gonic/gin" -) - -type GinRouter struct { - engine *gin.Engine -} - -func New() *GinRouter { - return &GinRouter{engine: gin.Default()} -} - -func (r *GinRouter) GET(path string, handler router.HandlerFunc) { - r.engine.GET(path, func(c *gin.Context) { - handler(&ginHandler.GinContext{Ctx: c}) // Convert gin.Context to your custom Context - }) -} - -func (r *GinRouter) POST(path string, handler router.HandlerFunc) { - r.engine.POST(path, func(c *gin.Context) { - handler(&ginHandler.GinContext{Ctx: c}) // Convert gin.Context to your custom Context - }) -} -func (r *GinRouter) PUT(path string, handler router.HandlerFunc) { - r.engine.PUT(path, func(c *gin.Context) { - handler(&ginHandler.GinContext{Ctx: c}) // Convert gin.Context to your custom Context - }) -} -func (r *GinRouter) DELETE(path string, handler router.HandlerFunc) { - r.engine.DELETE(path, func(c *gin.Context) { - handler(&ginHandler.GinContext{Ctx: c}) // Convert gin.Context to your custom Context - }) -} -func (r *GinRouter) OPTIONS(path string, handler router.HandlerFunc) { - r.engine.OPTIONS(path, func(c *gin.Context) { - handler(&ginHandler.GinContext{Ctx: c}) // Convert gin.Context to your custom Context - }) -} -func (r *GinRouter) Serve(addr string) error { - return r.engine.Run(addr) -} diff --git a/router/gin/router.go b/router/gin/router.go new file mode 100644 index 0000000..a60ecc6 --- /dev/null +++ b/router/gin/router.go @@ -0,0 +1,89 @@ +package gin + +import ( // Import the handler package that contains Context and HandlerFunc + + ginHandler "github.com/casbin/casbin-server/handler/gin" + "github.com/casbin/casbin-server/router" + "github.com/gin-gonic/gin" +) + +type GinRouter struct { + engine *gin.Engine +} + +func New() *GinRouter { + return &GinRouter{engine: gin.Default()} +} + +type GinRouterGroup struct { + routerGroup *gin.RouterGroup +} + +func NewGroup(routerGroup *gin.RouterGroup) *GinRouterGroup { + return &GinRouterGroup{routerGroup: routerGroup} +} + +func (r *GinRouter) GET(path string, handler router.HandlerFunc) { + r.engine.GET(path, func(c *gin.Context) { + handler(&ginHandler.GinContext{Ctx: c}) // Convert gin.Context to your custom Context + }) +} + +func (r *GinRouter) POST(path string, handler router.HandlerFunc) { + r.engine.POST(path, func(c *gin.Context) { + handler(&ginHandler.GinContext{Ctx: c}) // Convert gin.Context to your custom Context + }) +} +func (r *GinRouter) PUT(path string, handler router.HandlerFunc) { + r.engine.PUT(path, func(c *gin.Context) { + handler(&ginHandler.GinContext{Ctx: c}) // Convert gin.Context to your custom Context + }) +} +func (r *GinRouter) DELETE(path string, handler router.HandlerFunc) { + r.engine.DELETE(path, func(c *gin.Context) { + handler(&ginHandler.GinContext{Ctx: c}) // Convert gin.Context to your custom Context + }) +} +func (r *GinRouter) OPTIONS(path string, handler router.HandlerFunc) { + r.engine.OPTIONS(path, func(c *gin.Context) { + handler(&ginHandler.GinContext{Ctx: c}) // Convert gin.Context to your custom Context + }) +} +func (r *GinRouter) Serve(addr string) error { + return r.engine.Run(addr) +} + +func (r *GinRouter) Group(relativePath string, handlers ...router.HandlerFunc) router.RouterGroup { + return NewGroup(r.engine.Group(relativePath)) +} + +func (r *GinRouterGroup) GET(path string, handler router.HandlerFunc) { + r.routerGroup.GET(path, func(c *gin.Context) { + handler(&ginHandler.GinContext{Ctx: c}) // Convert gin.Context to your custom Context + }) +} + +func (r *GinRouterGroup) POST(path string, handler router.HandlerFunc) { + r.routerGroup.POST(path, func(c *gin.Context) { + handler(&ginHandler.GinContext{Ctx: c}) // Convert gin.Context to your custom Context + }) +} +func (r *GinRouterGroup) PUT(path string, handler router.HandlerFunc) { + r.routerGroup.PUT(path, func(c *gin.Context) { + handler(&ginHandler.GinContext{Ctx: c}) // Convert gin.Context to your custom Context + }) +} +func (r *GinRouterGroup) DELETE(path string, handler router.HandlerFunc) { + r.routerGroup.DELETE(path, func(c *gin.Context) { + handler(&ginHandler.GinContext{Ctx: c}) // Convert gin.Context to your custom Context + }) +} +func (r *GinRouterGroup) OPTIONS(path string, handler router.HandlerFunc) { + r.routerGroup.OPTIONS(path, func(c *gin.Context) { + handler(&ginHandler.GinContext{Ctx: c}) // Convert gin.Context to your custom Context + }) +} + +func (r *GinRouterGroup) Group(relativePath string, handlers ...router.HandlerFunc) router.RouterGroup { + return NewGroup(r.routerGroup.Group(relativePath)) +} diff --git a/router/router_interface.go b/router/router_interface.go index 88340f3..f0139f9 100644 --- a/router/router_interface.go +++ b/router/router_interface.go @@ -2,12 +2,17 @@ package router import "github.com/casbin/casbin-server/handler" -type Router interface { +type RouterGroup interface { GET(path string, handler HandlerFunc) POST(path string, handler HandlerFunc) PUT(path string, handler HandlerFunc) DELETE(path string, handler HandlerFunc) OPTIONS(path string, handler HandlerFunc) + Group(relativePath string, handlers ...HandlerFunc) RouterGroup +} + +type Router interface { + RouterGroup Serve(addr string) error } From 896ab6541fa37a6e503414351b76061280901028 Mon Sep 17 00:00:00 2001 From: Ali Shokoohi Date: Fri, 20 Sep 2024 22:49:57 +0330 Subject: [PATCH 7/8] Reduce comments --- router/gin/router.go | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/router/gin/router.go b/router/gin/router.go index a60ecc6..2df481f 100644 --- a/router/gin/router.go +++ b/router/gin/router.go @@ -1,7 +1,6 @@ package gin -import ( // Import the handler package that contains Context and HandlerFunc - +import ( ginHandler "github.com/casbin/casbin-server/handler/gin" "github.com/casbin/casbin-server/router" "github.com/gin-gonic/gin" @@ -31,22 +30,22 @@ func (r *GinRouter) GET(path string, handler router.HandlerFunc) { func (r *GinRouter) POST(path string, handler router.HandlerFunc) { r.engine.POST(path, func(c *gin.Context) { - handler(&ginHandler.GinContext{Ctx: c}) // Convert gin.Context to your custom Context + handler(&ginHandler.GinContext{Ctx: c}) }) } func (r *GinRouter) PUT(path string, handler router.HandlerFunc) { r.engine.PUT(path, func(c *gin.Context) { - handler(&ginHandler.GinContext{Ctx: c}) // Convert gin.Context to your custom Context + handler(&ginHandler.GinContext{Ctx: c}) }) } func (r *GinRouter) DELETE(path string, handler router.HandlerFunc) { r.engine.DELETE(path, func(c *gin.Context) { - handler(&ginHandler.GinContext{Ctx: c}) // Convert gin.Context to your custom Context + handler(&ginHandler.GinContext{Ctx: c}) }) } func (r *GinRouter) OPTIONS(path string, handler router.HandlerFunc) { r.engine.OPTIONS(path, func(c *gin.Context) { - handler(&ginHandler.GinContext{Ctx: c}) // Convert gin.Context to your custom Context + handler(&ginHandler.GinContext{Ctx: c}) }) } func (r *GinRouter) Serve(addr string) error { @@ -59,28 +58,28 @@ func (r *GinRouter) Group(relativePath string, handlers ...router.HandlerFunc) r func (r *GinRouterGroup) GET(path string, handler router.HandlerFunc) { r.routerGroup.GET(path, func(c *gin.Context) { - handler(&ginHandler.GinContext{Ctx: c}) // Convert gin.Context to your custom Context + handler(&ginHandler.GinContext{Ctx: c}) }) } func (r *GinRouterGroup) POST(path string, handler router.HandlerFunc) { r.routerGroup.POST(path, func(c *gin.Context) { - handler(&ginHandler.GinContext{Ctx: c}) // Convert gin.Context to your custom Context + handler(&ginHandler.GinContext{Ctx: c}) }) } func (r *GinRouterGroup) PUT(path string, handler router.HandlerFunc) { r.routerGroup.PUT(path, func(c *gin.Context) { - handler(&ginHandler.GinContext{Ctx: c}) // Convert gin.Context to your custom Context + handler(&ginHandler.GinContext{Ctx: c}) }) } func (r *GinRouterGroup) DELETE(path string, handler router.HandlerFunc) { r.routerGroup.DELETE(path, func(c *gin.Context) { - handler(&ginHandler.GinContext{Ctx: c}) // Convert gin.Context to your custom Context + handler(&ginHandler.GinContext{Ctx: c}) }) } func (r *GinRouterGroup) OPTIONS(path string, handler router.HandlerFunc) { r.routerGroup.OPTIONS(path, func(c *gin.Context) { - handler(&ginHandler.GinContext{Ctx: c}) // Convert gin.Context to your custom Context + handler(&ginHandler.GinContext{Ctx: c}) }) } From b3ef472e880224c7eebfb086a8537570d725a008 Mon Sep 17 00:00:00 2001 From: Ali Shokoohi Date: Fri, 20 Sep 2024 23:01:26 +0330 Subject: [PATCH 8/8] Fixed versions --- env.example | 7 ------- go.mod | 3 +-- go.sum | 12 ++++-------- 3 files changed, 5 insertions(+), 17 deletions(-) delete mode 100644 env.example diff --git a/env.example b/env.example deleted file mode 100644 index ef4291a..0000000 --- a/env.example +++ /dev/null @@ -1,7 +0,0 @@ -# Database Configuration -DB_HOST="127.0.0.1" -DB_PORT="5432" -DB_USERNAME="casbin" -DB_NAME="casbin" -DB_PASSWORD="casbin123" -CONNECTION_CONFIG_PATH="config/connection_config.json" diff --git a/go.mod b/go.mod index 0c4771e..1fd56c6 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ go 1.19 require ( github.com/casbin/casbin/v2 v2.60.0 github.com/casbin/gorm-adapter/v3 v3.14.0 + github.com/gin-gonic/gin v1.8.2 github.com/stretchr/testify v1.8.1 google.golang.org/grpc v1.42.0 google.golang.org/protobuf v1.28.1 @@ -14,7 +15,6 @@ require ( github.com/Knetic/govaluate v3.0.1-0.20171022003610-9aa49832a739+incompatible // indirect github.com/davecgh/go-spew v1.1.1 // indirect github.com/gin-contrib/sse v0.1.0 // indirect - github.com/gin-gonic/gin v1.8.2 // indirect github.com/glebarez/go-sqlite v1.19.1 // indirect github.com/glebarez/sqlite v1.5.0 // indirect github.com/go-playground/locales v0.14.0 // indirect @@ -37,7 +37,6 @@ require ( github.com/jinzhu/inflection v1.0.0 // indirect github.com/jinzhu/now v1.1.5 // indirect github.com/json-iterator/go v1.1.12 // indirect - github.com/kr/pretty v0.3.0 // indirect github.com/leodido/go-urn v1.2.1 // indirect github.com/mattn/go-isatty v0.0.16 // indirect github.com/microsoft/go-mssqldb v0.17.0 // indirect diff --git a/go.sum b/go.sum index d974e34..93256f1 100644 --- a/go.sum +++ b/go.sum @@ -53,6 +53,7 @@ github.com/glebarez/sqlite v1.5.0 h1:+8LAEpmywqresSoGlqjjT+I9m4PseIM3NcerIJ/V7mk github.com/glebarez/sqlite v1.5.0/go.mod h1:0wzXzTvfVJIN2GqRhCdMbnYd+m+aH5/QV7B30rM6NgY= github.com/go-kit/log v0.1.0/go.mod h1:zbhenjAZHb184qTLMA9ZjW7ThYL0H2mk7Q6pNt4vbaY= github.com/go-logfmt/logfmt v0.5.0/go.mod h1:wCYkCAKZfumFQihp8CzCvQ3paCTfi41vtzG1KdI/P7A= +github.com/go-playground/assert/v2 v2.0.1 h1:MsBgLAaY856+nPRTKrp3/OZK38U/wa0CcBYNjji3q3A= github.com/go-playground/assert/v2 v2.0.1/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4= github.com/go-playground/locales v0.14.0 h1:u50s323jtVGugKlcYeyzC0etD1HifMjqmJqb8WugfUU= github.com/go-playground/locales v0.14.0/go.mod h1:sawfccIbzZTqEDETgFXqTho0QybSa7l++s0DH+LDiLs= @@ -212,8 +213,8 @@ github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0 h1:OdAsTTz6O github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo= github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ= github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4= -github.com/rogpeppe/go-internal v1.6.1 h1:/FiVV8dS/e+YqF2JvO3yXRFbBLTIuSDkuC7aBOAvL+k= github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc= +github.com/rogpeppe/go-internal v1.8.0 h1:FCbCCtXNOY3UtUuHUYaghJg4y7Fd14rXifAYUAtL9R8= github.com/rogpeppe/go-internal v1.8.0/go.mod h1:WmiCO8CzOY8rg0OYDC4/i/2WRWAB6poM+XZ2dLUbcbE= github.com/rs/xid v1.2.1/go.mod h1:+uKXf+4Djp6Md1KODXJxgGQPKngRmWyn10oCKFzNHOQ= github.com/rs/zerolog v1.13.0/go.mod h1:YbFCdg8HfsridGWAh22vktObvhZbQsZXe4/zB0OKkWU= @@ -236,8 +237,8 @@ github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5 github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk= github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= +github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk= github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= github.com/ugorji/go v1.2.7/go.mod h1:nF9osbDWLy6bDVv/Rtoh6QgnvNDpmCalQV5urGCCS6M= github.com/ugorji/go/codec v1.2.7 h1:YPXUKf7fYbp/y8xloBqZOw2qaVggbfwMlI8WM3wZUJ0= @@ -291,7 +292,6 @@ golang.org/x/net v0.0.0-20201010224723-4f7140c49acb/go.mod h1:sp8m0HH+o8qH0wwXwY golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= -golang.org/x/net v0.0.0-20220425223048-2871e0cb64e4 h1:HVyaeDAYux4pnY+D/SiwmLOR36ewZ4iGQIIrtnuCjFA= golang.org/x/net v0.0.0-20220425223048-2871e0cb64e4/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk= golang.org/x/net v0.4.0 h1:Q5QPcMlvfxFTAPV0+07Xz/MpK9NTXu2VDUuy0FeMfaU= golang.org/x/net v0.4.0/go.mod h1:MBQ8lrhLObU/6UmLb4fmbmk5OcyYmqtbGd/9yIeKjEE= @@ -324,7 +324,6 @@ golang.org/x/sys v0.0.0-20210806184541-e5e7981a1069/go.mod h1:oPkhp1MJrh7nUepCBc golang.org/x/sys v0.0.0-20211007075335-d3039528d8ac/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220224120231-95c6836cb0e7/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab h1:2QkjZIsXupsJbJIdSjjUOgWK3aEtzyuh2mPt3l/CkeU= golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.3.0 h1:w8ZOecv6NaNa/zC8944JTU3vz4u6Lagfk4RPQxv92NQ= golang.org/x/sys v0.3.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= @@ -336,7 +335,6 @@ golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.4/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= -golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk= golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= golang.org/x/text v0.5.0 h1:OLmvp0KP+FVG99Ct/qFiL/Fhk4zp4QQnZ7b2U+5piUM= golang.org/x/text v0.5.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= @@ -386,13 +384,11 @@ google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2 google.golang.org/protobuf v1.23.1-0.20200526195155-81db48ad09cc/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= google.golang.org/protobuf v1.25.0/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlbajtzgsN7c= google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= -google.golang.org/protobuf v1.27.1 h1:SnqbnDw1V7RiZcXPx5MEeqPv2s79L9i7BJUlG/+RurQ= -google.golang.org/protobuf v1.27.1/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= google.golang.org/protobuf v1.28.1 h1:d0NfwRgPtno5B1Wa6L2DAG+KivqkdutMf1UhdNx175w= google.golang.org/protobuf v1.28.1/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI= gopkg.in/inconshreveable/log15.v2 v2.0.0-20180818164646-67afb5ed74ec/go.mod h1:aPpfJ7XW+gOuirDoZ8gHhLh3kZ1B08FtV2bbmy7Jv3s=