1
0
mirror of https://github.com/lus/pasty.git synced 2023-08-10 21:13:09 +03:00
pasty/internal/storage/driver.go
2020-08-23 00:06:29 +02:00

39 lines
814 B
Go

package storage
import (
"fmt"
"github.com/Lukaesebrot/pasty/internal/env"
"github.com/Lukaesebrot/pasty/internal/pastes"
"github.com/bwmarrin/snowflake"
"strings"
)
// Current holds the current storage driver
var Current Driver
// Driver represents a storage driver
type Driver interface {
Initialize() error
Terminate() error
Get(id snowflake.ID) (*pastes.Paste, error)
Save(paste *pastes.Paste) error
Delete(id snowflake.ID) error
}
// Load loads the current storage driver
func Load() error {
storageType := strings.ToLower(env.Get("STORAGE_TYPE", "file"))
switch storageType {
case "file":
driver := new(FileDriver)
err := driver.Initialize()
if err != nil {
return err
}
Current = driver
return nil
default:
return fmt.Errorf("invalid storage type '%s'", storageType)
}
}