a simple url shortening service, in the same vein as bit.ly and tinyurl.com, written in Go and using BoltDB as a backend
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.

44 lines
662B

  1. package bolt
  2. import (
  3. bolt "go.etcd.io/bbolt"
  4. "net/url"
  5. "os"
  6. "time"
  7. )
  8. type BoltStorage struct {
  9. DB *bolt.DB
  10. }
  11. func (stor *BoltStorage) Shutdown() {
  12. stor.DB.Close()
  13. }
  14. func New(u *url.URL) (*BoltStorage, error) {
  15. path := u.Opaque
  16. if u.Path != "" {
  17. path = u.Path
  18. }
  19. db, err := bolt.Open(path, 0600, &bolt.Options{Timeout: 3 * time.Second})
  20. if err != nil {
  21. return nil, err
  22. }
  23. return &BoltStorage{DB: db}, nil
  24. }
  25. func (stor *BoltStorage) Backup(bpath string) error {
  26. tx, err := stor.DB.Begin(false)
  27. if err != nil {
  28. return err
  29. }
  30. defer tx.Rollback()
  31. err = tx.CopyFile(bpath, os.FileMode(0600))
  32. if err != nil {
  33. return err
  34. }
  35. return nil
  36. }