a simple url shortener in Go (check it out at qurl.org)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

43 lines
684 B

6 years ago
6 years ago
  1. package storage
  2. import (
  3. "fmt"
  4. "net/url"
  5. "qurl/qurl"
  6. "qurl/storage/bolt"
  7. "strings"
  8. )
  9. type Storage interface {
  10. AddQURL(*qurl.QURL) error
  11. GetQURLByURL(string) (*qurl.QURL, error)
  12. SetQURLSequence(uint64) error
  13. Backup(string) error
  14. Shutdown()
  15. }
  16. func NewStorage(su string) (Storage, error) {
  17. u, err := url.Parse(su)
  18. if err != nil {
  19. return nil, err
  20. }
  21. if u.Scheme == "" {
  22. return nil, fmt.Errorf("URL must include a scheme")
  23. }
  24. var stor Storage
  25. switch strings.ToLower(u.Scheme) {
  26. case "bolt", "boltdb", "bbolt":
  27. stor, err = bolt.New(u)
  28. if err != nil {
  29. return nil, err
  30. }
  31. default:
  32. return nil, fmt.Errorf("Unsupported URL scheme")
  33. }
  34. return stor, nil
  35. }