mirror of
https://github.com/lus/pasty.git
synced 2023-08-10 21:13:09 +03:00
refactor package structure & remove v1 API
This commit is contained in:
@ -2,9 +2,10 @@ package storage
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/lus/pasty/internal/config"
|
||||
"github.com/lus/pasty/internal/shared"
|
||||
"github.com/lus/pasty/internal/paste"
|
||||
"github.com/lus/pasty/internal/storage/file"
|
||||
"github.com/lus/pasty/internal/storage/mongodb"
|
||||
"github.com/lus/pasty/internal/storage/postgres"
|
||||
@ -19,8 +20,8 @@ type Driver interface {
|
||||
Initialize() error
|
||||
Terminate() error
|
||||
ListIDs() ([]string, error)
|
||||
Get(id string) (*shared.Paste, error)
|
||||
Save(paste *shared.Paste) error
|
||||
Get(id string) (*paste.Paste, error)
|
||||
Save(paste *paste.Paste) error
|
||||
Delete(id string) error
|
||||
Cleanup() (int, error)
|
||||
}
|
||||
@ -43,15 +44,15 @@ func Load() error {
|
||||
}
|
||||
|
||||
// GetDriver returns the driver with the given type if it exists
|
||||
func GetDriver(storageType shared.StorageType) (Driver, error) {
|
||||
switch storageType {
|
||||
case shared.StorageTypeFile:
|
||||
func GetDriver(storageType string) (Driver, error) {
|
||||
switch strings.TrimSpace(strings.ToLower(storageType)) {
|
||||
case "file":
|
||||
return new(file.FileDriver), nil
|
||||
case shared.StorageTypePostgres:
|
||||
case "postgres":
|
||||
return new(postgres.PostgresDriver), nil
|
||||
case shared.StorageTypeMongoDB:
|
||||
case "mongodb":
|
||||
return new(mongodb.MongoDBDriver), nil
|
||||
case shared.StorageTypeS3:
|
||||
case "s3":
|
||||
return new(s3.S3Driver), nil
|
||||
default:
|
||||
return nil, fmt.Errorf("invalid storage type '%s'", storageType)
|
||||
|
@ -10,7 +10,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/lus/pasty/internal/config"
|
||||
"github.com/lus/pasty/internal/shared"
|
||||
"github.com/lus/pasty/internal/paste"
|
||||
)
|
||||
|
||||
// FileDriver represents the file storage driver
|
||||
@ -35,7 +35,7 @@ func (driver *FileDriver) ListIDs() ([]string, error) {
|
||||
var ids []string
|
||||
|
||||
// Fill the IDs slice
|
||||
err := filepath.Walk(driver.filePath, func(path string, info os.FileInfo, err error) error {
|
||||
err := filepath.Walk(driver.filePath, func(_ string, info os.FileInfo, err error) error {
|
||||
// Check if a walking error occurred
|
||||
if err != nil {
|
||||
return err
|
||||
@ -65,7 +65,7 @@ func (driver *FileDriver) ListIDs() ([]string, error) {
|
||||
}
|
||||
|
||||
// Get loads a paste
|
||||
func (driver *FileDriver) Get(id string) (*shared.Paste, error) {
|
||||
func (driver *FileDriver) Get(id string) (*paste.Paste, error) {
|
||||
// Read the file
|
||||
id = base64.StdEncoding.EncodeToString([]byte(id))
|
||||
data, err := ioutil.ReadFile(filepath.Join(driver.filePath, id+".json"))
|
||||
@ -77,7 +77,7 @@ func (driver *FileDriver) Get(id string) (*shared.Paste, error) {
|
||||
}
|
||||
|
||||
// Unmarshal the file into a paste
|
||||
paste := new(shared.Paste)
|
||||
paste := new(paste.Paste)
|
||||
err = json.Unmarshal(data, &paste)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@ -86,7 +86,7 @@ func (driver *FileDriver) Get(id string) (*shared.Paste, error) {
|
||||
}
|
||||
|
||||
// Save saves a paste
|
||||
func (driver *FileDriver) Save(paste *shared.Paste) error {
|
||||
func (driver *FileDriver) Save(paste *paste.Paste) error {
|
||||
// Marshal the paste
|
||||
jsonBytes, err := json.Marshal(paste)
|
||||
if err != nil {
|
||||
|
@ -5,7 +5,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/lus/pasty/internal/config"
|
||||
"github.com/lus/pasty/internal/shared"
|
||||
"github.com/lus/pasty/internal/paste"
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
@ -65,7 +65,7 @@ func (driver *MongoDBDriver) ListIDs() ([]string, error) {
|
||||
}
|
||||
|
||||
// Decode all paste documents
|
||||
var pasteSlice []shared.Paste
|
||||
var pasteSlice []paste.Paste
|
||||
err = result.All(ctx, &pasteSlice)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@ -80,7 +80,7 @@ func (driver *MongoDBDriver) ListIDs() ([]string, error) {
|
||||
}
|
||||
|
||||
// Get loads a paste
|
||||
func (driver *MongoDBDriver) Get(id string) (*shared.Paste, error) {
|
||||
func (driver *MongoDBDriver) Get(id string) (*paste.Paste, error) {
|
||||
// Define the collection to use for this database operation
|
||||
collection := driver.client.Database(driver.database).Collection(driver.collection)
|
||||
|
||||
@ -100,7 +100,7 @@ func (driver *MongoDBDriver) Get(id string) (*shared.Paste, error) {
|
||||
}
|
||||
|
||||
// Return the retrieved paste object
|
||||
paste := new(shared.Paste)
|
||||
paste := new(paste.Paste)
|
||||
err = result.Decode(paste)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@ -109,7 +109,7 @@ func (driver *MongoDBDriver) Get(id string) (*shared.Paste, error) {
|
||||
}
|
||||
|
||||
// Save saves a paste
|
||||
func (driver *MongoDBDriver) Save(paste *shared.Paste) error {
|
||||
func (driver *MongoDBDriver) Save(paste *paste.Paste) error {
|
||||
// Define the collection to use for this database operation
|
||||
collection := driver.client.Database(driver.database).Collection(driver.collection)
|
||||
|
||||
|
@ -12,7 +12,7 @@ import (
|
||||
"github.com/jackc/pgx/v4/pgxpool"
|
||||
"github.com/johejo/golang-migrate-extra/source/iofs"
|
||||
"github.com/lus/pasty/internal/config"
|
||||
"github.com/lus/pasty/internal/shared"
|
||||
"github.com/lus/pasty/internal/paste"
|
||||
)
|
||||
|
||||
//go:embed migrations/*.sql
|
||||
@ -76,12 +76,12 @@ func (driver *PostgresDriver) ListIDs() ([]string, error) {
|
||||
}
|
||||
|
||||
// Get loads a paste
|
||||
func (driver *PostgresDriver) Get(id string) (*shared.Paste, error) {
|
||||
func (driver *PostgresDriver) Get(id string) (*paste.Paste, error) {
|
||||
query := "SELECT * FROM pastes WHERE id = $1"
|
||||
|
||||
row := driver.pool.QueryRow(context.Background(), query, id)
|
||||
|
||||
paste := new(shared.Paste)
|
||||
paste := new(paste.Paste)
|
||||
if err := row.Scan(&paste.ID, &paste.Content, &paste.ModificationToken, &paste.Created, &paste.Metadata); err != nil {
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return nil, nil
|
||||
@ -92,7 +92,7 @@ func (driver *PostgresDriver) Get(id string) (*shared.Paste, error) {
|
||||
}
|
||||
|
||||
// Save saves a paste
|
||||
func (driver *PostgresDriver) Save(paste *shared.Paste) error {
|
||||
func (driver *PostgresDriver) Save(paste *paste.Paste) error {
|
||||
query := `
|
||||
INSERT INTO pastes (id, content, "modificationToken", created, metadata)
|
||||
VALUES ($1, $2, $3, $4, $5)
|
||||
|
@ -9,7 +9,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/lus/pasty/internal/config"
|
||||
"github.com/lus/pasty/internal/shared"
|
||||
"github.com/lus/pasty/internal/paste"
|
||||
"github.com/minio/minio-go/v7"
|
||||
"github.com/minio/minio-go/v7/pkg/credentials"
|
||||
)
|
||||
@ -59,7 +59,7 @@ func (driver *S3Driver) ListIDs() ([]string, error) {
|
||||
}
|
||||
|
||||
// Get loads a paste
|
||||
func (driver *S3Driver) Get(id string) (*shared.Paste, error) {
|
||||
func (driver *S3Driver) Get(id string) (*paste.Paste, error) {
|
||||
// Read the object
|
||||
object, err := driver.client.GetObject(context.Background(), driver.bucket, id+".json", minio.GetObjectOptions{})
|
||||
if err != nil {
|
||||
@ -74,7 +74,7 @@ func (driver *S3Driver) Get(id string) (*shared.Paste, error) {
|
||||
}
|
||||
|
||||
// Unmarshal the object into a paste
|
||||
paste := new(shared.Paste)
|
||||
paste := new(paste.Paste)
|
||||
err = json.Unmarshal(data, &paste)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@ -83,7 +83,7 @@ func (driver *S3Driver) Get(id string) (*shared.Paste, error) {
|
||||
}
|
||||
|
||||
// Save saves a paste
|
||||
func (driver *S3Driver) Save(paste *shared.Paste) error {
|
||||
func (driver *S3Driver) Save(paste *paste.Paste) error {
|
||||
// Marshal the paste
|
||||
jsonBytes, err := json.Marshal(paste)
|
||||
if err != nil {
|
||||
|
Reference in New Issue
Block a user