2020-08-23 01:06:29 +03:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2021-04-15 20:26:17 +03:00
|
|
|
|
2021-04-15 21:15:42 +03:00
|
|
|
"github.com/lus/pasty/internal/config"
|
|
|
|
"github.com/lus/pasty/internal/shared"
|
2021-04-15 22:03:26 +03:00
|
|
|
"github.com/lus/pasty/internal/storage/file"
|
|
|
|
"github.com/lus/pasty/internal/storage/mongodb"
|
2021-04-15 22:30:37 +03:00
|
|
|
"github.com/lus/pasty/internal/storage/postgres"
|
2021-04-15 22:03:26 +03:00
|
|
|
"github.com/lus/pasty/internal/storage/s3"
|
2020-08-23 01:06:29 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// Current holds the current storage driver
|
|
|
|
var Current Driver
|
|
|
|
|
|
|
|
// Driver represents a storage driver
|
|
|
|
type Driver interface {
|
|
|
|
Initialize() error
|
|
|
|
Terminate() error
|
2020-08-26 22:59:22 +03:00
|
|
|
ListIDs() ([]string, error)
|
2021-04-15 21:15:42 +03:00
|
|
|
Get(id string) (*shared.Paste, error)
|
|
|
|
Save(paste *shared.Paste) error
|
2020-08-24 21:22:53 +03:00
|
|
|
Delete(id string) error
|
2020-09-19 02:56:50 +03:00
|
|
|
Cleanup() (int, error)
|
2020-08-23 01:06:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Load loads the current storage driver
|
|
|
|
func Load() error {
|
2020-08-24 00:44:47 +03:00
|
|
|
// Define the driver to use
|
2021-04-15 21:15:42 +03:00
|
|
|
driver, err := GetDriver(config.Current.StorageType)
|
2020-08-26 23:12:36 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2020-08-23 01:06:29 +03:00
|
|
|
}
|
2020-08-24 00:44:47 +03:00
|
|
|
|
|
|
|
// Initialize the driver
|
2020-08-26 23:12:36 +03:00
|
|
|
err = driver.Initialize()
|
2020-08-24 00:44:47 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
Current = driver
|
|
|
|
return nil
|
2020-08-23 01:06:29 +03:00
|
|
|
}
|
2020-08-26 23:12:36 +03:00
|
|
|
|
2021-04-15 21:15:42 +03:00
|
|
|
// GetDriver returns the driver with the given type if it exists
|
|
|
|
func GetDriver(storageType shared.StorageType) (Driver, error) {
|
|
|
|
switch storageType {
|
|
|
|
case shared.StorageTypeFile:
|
2021-04-15 22:03:26 +03:00
|
|
|
return new(file.FileDriver), nil
|
2021-04-15 21:15:42 +03:00
|
|
|
case shared.StorageTypePostgres:
|
2021-04-15 22:30:37 +03:00
|
|
|
return new(postgres.PostgresDriver), nil
|
2021-04-15 21:15:42 +03:00
|
|
|
case shared.StorageTypeMongoDB:
|
2021-04-15 22:03:26 +03:00
|
|
|
return new(mongodb.MongoDBDriver), nil
|
2021-04-15 21:15:42 +03:00
|
|
|
case shared.StorageTypeS3:
|
2021-04-15 22:03:26 +03:00
|
|
|
return new(s3.S3Driver), nil
|
2020-08-26 23:12:36 +03:00
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("invalid storage type '%s'", storageType)
|
|
|
|
}
|
|
|
|
}
|