2018-11-11 16:56:30 -09:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2019-12-26 07:21:48 -09:00
|
|
|
"git.binarythought.com/cdramey/qurl/obj"
|
|
|
|
"git.binarythought.com/cdramey/qurl/storage/bolt"
|
2019-12-26 16:08:43 -09:00
|
|
|
"net/url"
|
2018-11-11 16:56:30 -09:00
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Storage interface {
|
2019-12-26 07:21:48 -09:00
|
|
|
AddQURL(*obj.QURL) error
|
|
|
|
GetQURLByURL(string) (*obj.QURL, error)
|
|
|
|
GetQURLByID(uint64) (*obj.QURL, error)
|
2018-11-11 16:56:30 -09:00
|
|
|
SetQURLSequence(uint64) error
|
2018-11-18 18:28:44 -09:00
|
|
|
Backup(string) error
|
2018-11-11 16:56:30 -09:00
|
|
|
Shutdown()
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewStorage(su string) (Storage, error) {
|
|
|
|
u, err := url.Parse(su)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if u.Scheme == "" {
|
|
|
|
return nil, fmt.Errorf("URL must include a scheme")
|
|
|
|
}
|
|
|
|
|
|
|
|
var stor Storage
|
|
|
|
|
|
|
|
switch strings.ToLower(u.Scheme) {
|
|
|
|
case "bolt", "boltdb", "bbolt":
|
|
|
|
stor, err = bolt.New(u)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("Unsupported URL scheme")
|
|
|
|
}
|
|
|
|
|
|
|
|
return stor, nil
|
|
|
|
}
|