inject.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. */