inject.go 1.3 KB

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