http urls monitor.

wrapper.go 2.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. // Copyright (c) 2012-present The upper.io/db authors. All rights reserved.
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining
  4. // a copy of this software and associated documentation files (the
  5. // "Software"), to deal in the Software without restriction, including
  6. // without limitation the rights to use, copy, modify, merge, publish,
  7. // distribute, sublicense, and/or sell copies of the Software, and to
  8. // permit persons to whom the Software is furnished to do so, subject to
  9. // the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be
  12. // included in all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  16. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  18. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  19. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  20. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  21. package db
  22. import (
  23. "fmt"
  24. "sync"
  25. )
  26. var (
  27. adapters map[string]*AdapterFuncMap
  28. adaptersMu sync.RWMutex
  29. )
  30. func init() {
  31. adapters = make(map[string]*AdapterFuncMap)
  32. }
  33. // AdapterFuncMap defines functions that need to be implemented by adapters.
  34. type AdapterFuncMap struct {
  35. Open func(settings ConnectionURL) (Database, error)
  36. }
  37. // RegisterAdapter registers a generic Database adapter. This function must be
  38. // called from adapter packages upon initialization.
  39. func RegisterAdapter(name string, adapter *AdapterFuncMap) {
  40. adaptersMu.Lock()
  41. defer adaptersMu.Unlock()
  42. if name == "" {
  43. panic(`Missing adapter name`)
  44. }
  45. if _, ok := adapters[name]; ok {
  46. panic(`db.RegisterAdapter() called twice for adapter: ` + name)
  47. }
  48. adapters[name] = adapter
  49. }
  50. func adapter(name string) AdapterFuncMap {
  51. adaptersMu.RLock()
  52. defer adaptersMu.RUnlock()
  53. if fn, ok := adapters[name]; ok {
  54. return *fn
  55. }
  56. return missingAdapter(name)
  57. }
  58. func missingAdapter(name string) AdapterFuncMap {
  59. err := fmt.Errorf("upper: Missing adapter %q, forgot to import?", name)
  60. return AdapterFuncMap{
  61. Open: func(ConnectionURL) (Database, error) {
  62. return nil, err
  63. },
  64. }
  65. }
  66. // Open attempts to open a database. Returns a generic Database instance on
  67. // success.
  68. func Open(adapterName string, settings ConnectionURL) (Database, error) {
  69. return adapter(adapterName).Open(settings)
  70. }