config_cache.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. package config
  2. import (
  3. "errors"
  4. )
  5. type ConfigObjectCreator func(data []byte) (interface{}, error)
  6. var (
  7. configCache map[string]ConfigObjectCreator
  8. )
  9. func getConfigKey(protocol string, proxyType string) string {
  10. return protocol + "_" + proxyType
  11. }
  12. func registerConfigType(protocol string, proxyType string, creator ConfigObjectCreator) error {
  13. // TODO: check name
  14. configCache[getConfigKey(protocol, proxyType)] = creator
  15. return nil
  16. }
  17. func RegisterInboundConnectionConfig(protocol string, creator ConfigObjectCreator) error {
  18. return registerConfigType(protocol, "inbound", creator)
  19. }
  20. func RegisterOutboundConnectionConfig(protocol string, creator ConfigObjectCreator) error {
  21. return registerConfigType(protocol, "outbound", creator)
  22. }
  23. func CreateInboundConnectionConfig(protocol string, data []byte) (interface{}, error) {
  24. creator, found := configCache[getConfigKey(protocol, "inbound")]
  25. if !found {
  26. return nil, errors.New(protocol + " not found.")
  27. }
  28. return creator(data)
  29. }
  30. func CreateOutboundConnectionConfig(protocol string, data []byte) (interface{}, error) {
  31. creator, found := configCache[getConfigKey(protocol, "outbound")]
  32. if !found {
  33. return nil, errors.New(protocol + " not found.")
  34. }
  35. return creator(data)
  36. }
  37. func initializeConfigCache() {
  38. configCache = make(map[string]ConfigObjectCreator)
  39. }
  40. func init() {
  41. initializeConfigCache()
  42. }