inject.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. package inject
  2. import (
  3. "runtime"
  4. "github.com/casbin/casbin"
  5. "github.com/facebookgo/inject"
  6. "kpt.xdmy/pkg/setting"
  7. // "kpt.xdmy/service/bll"
  8. )
  9. // Object 注入对象
  10. type Object struct {
  11. // Common *bll.Common
  12. Enforcer *casbin.Enforcer
  13. }
  14. var Obj *Object
  15. // Init 初始化依赖注入
  16. func Init() {
  17. g := new(inject.Graph)
  18. // 注入casbin
  19. osType := runtime.GOOS
  20. var path string
  21. if osType == "windows" {
  22. path = setting.CurrentPath + "conf\\rbac_model.conf"
  23. } else if osType == "linux" {
  24. path = setting.CurrentPath + "conf/rbac_model.conf"
  25. }
  26. enforcer := casbin.NewEnforcer(path, false)
  27. _ = g.Provide(&inject.Object{Value: enforcer})
  28. // Common := new(bll.Common)
  29. // _ = g.Provide(&inject.Object{Value: Common})
  30. if err := g.Populate(); err != nil {
  31. panic("初始化依赖注入发生错误:" + err.Error())
  32. }
  33. Obj = &Object{
  34. Enforcer: enforcer,
  35. // Common: Common,
  36. }
  37. return
  38. }
  39. //var instance *Object
  40. //var once sync.Once
  41. //
  42. //func GetInstance() *Object {
  43. // once.Do(func() {
  44. // instance = Init()
  45. // })
  46. // return instance
  47. //}
  48. // // 加载casbin策略数据,包括角色权限数据、用户角色数据
  49. // func LoadCasbinPolicyData() error {
  50. // // c := Obj.Common
  51. // err := c.RoleAPI.LoadAllPolicy()
  52. // if err != nil {
  53. // return err
  54. // }
  55. // err = c.UserAPI.LoadAllPolicy()
  56. // if err != nil {
  57. // return err
  58. // }
  59. // return nil
  60. // }