role.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // Copyright © 2021-2022 Ettore Di Giacinto <[email protected]>
  2. //
  3. // This program is free software; you can redistribute it and/or modify
  4. // it under the terms of the GNU General Public License as published by
  5. // the Free Software Foundation; either version 2 of the License, or
  6. // (at your option) any later version.
  7. //
  8. // This program is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. // GNU General Public License for more details.
  12. //
  13. // You should have received a copy of the GNU General Public License along
  14. // with this program; if not, see <http://www.gnu.org/licenses/>.
  15. package service
  16. import (
  17. "strings"
  18. "github.com/ipfs/go-log"
  19. )
  20. // Role is a service role.
  21. // It is identified by a unique string which is sent over the wire
  22. // and streamed to/from the clients.
  23. // Roles can be applied either directly, or assigned within roles in the API
  24. type Role string
  25. // RoleConfig is the role config structure, which holds all the objects that can be used by a Role
  26. type RoleConfig struct {
  27. Client *Client
  28. UUID, ServiceID, StateDir, APIAddress, NetworkToken string
  29. Logger log.StandardLogger
  30. roles map[Role]func(c *RoleConfig) error
  31. }
  32. // RoleOption is a role option
  33. type RoleOption func(c *RoleConfig)
  34. // RoleKey is an association between a Role(string) and a Handler which actually
  35. // fullfills the role
  36. type RoleKey struct {
  37. RoleHandler func(c *RoleConfig) error
  38. Role Role
  39. }
  40. // WithRole sets the available roles
  41. func WithRole(f map[Role]func(c *RoleConfig) error) RoleOption {
  42. return func(c *RoleConfig) {
  43. c.roles = f
  44. }
  45. }
  46. // WithRoleLogger sets a logger for the role action
  47. func WithRoleLogger(l log.StandardLogger) RoleOption {
  48. return func(c *RoleConfig) {
  49. c.Logger = l
  50. }
  51. }
  52. // WithRoleUUID sets the UUID which performs the role
  53. func WithRoleUUID(u string) RoleOption {
  54. return func(c *RoleConfig) {
  55. c.UUID = u
  56. }
  57. }
  58. // WithRoleStateDir sets the statedir for the role
  59. func WithRoleStateDir(s string) RoleOption {
  60. return func(c *RoleConfig) {
  61. c.StateDir = s
  62. }
  63. }
  64. // WithRoleToken sets the network token which can be used by the role
  65. func WithRoleToken(s string) RoleOption {
  66. return func(c *RoleConfig) {
  67. c.NetworkToken = s
  68. }
  69. }
  70. // WithRoleAPIAddress sets the API Address used during the execution
  71. func WithRoleAPIAddress(s string) RoleOption {
  72. return func(c *RoleConfig) {
  73. c.APIAddress = s
  74. }
  75. }
  76. // WithRoleServiceID sets a role service ID
  77. func WithRoleServiceID(s string) RoleOption {
  78. return func(c *RoleConfig) {
  79. c.ServiceID = s
  80. }
  81. }
  82. // WithRoleClient sets a client for a role
  83. func WithRoleClient(e *Client) RoleOption {
  84. return func(c *RoleConfig) {
  85. c.Client = e
  86. }
  87. }
  88. // Apply applies a role and takes a list of options
  89. func (rr Role) Apply(opts ...RoleOption) {
  90. c := &RoleConfig{}
  91. for _, o := range opts {
  92. o(c)
  93. }
  94. for _, role := range strings.Split(string(rr), ",") {
  95. r := Role(role)
  96. if f, exists := c.roles[r]; exists {
  97. c.Logger.Info("Role loaded. Applying ", r)
  98. if err := f(c); err != nil {
  99. c.Logger.Warning("Failed applying role", role, err)
  100. }
  101. } else {
  102. c.Logger.Warn("Unknown role: ", r)
  103. }
  104. }
  105. }