1
0
mirror of https://github.com/lus/pasty.git synced 2023-08-10 21:13:09 +03:00

Rework project structure

This commit is contained in:
Lukas Schulte Pelkum
2021-04-15 20:15:42 +02:00
parent de2add44ec
commit 1792ef1c38
20 changed files with 238 additions and 328 deletions

View File

@@ -1,11 +1,11 @@
package storage
import (
"errors"
"fmt"
"strings"
"github.com/lus/pasty/internal/env"
"github.com/lus/pasty/internal/pastes"
"github.com/lus/pasty/internal/config"
"github.com/lus/pasty/internal/shared"
)
// Current holds the current storage driver
@@ -16,8 +16,8 @@ type Driver interface {
Initialize() error
Terminate() error
ListIDs() ([]string, error)
Get(id string) (*pastes.Paste, error)
Save(paste *pastes.Paste) error
Get(id string) (*shared.Paste, error)
Save(paste *shared.Paste) error
Delete(id string) error
Cleanup() (int, error)
}
@@ -25,8 +25,7 @@ type Driver interface {
// Load loads the current storage driver
func Load() error {
// Define the driver to use
storageType := env.Get("STORAGE_TYPE", "file")
driver, err := GetDriver(storageType)
driver, err := GetDriver(config.Current.StorageType)
if err != nil {
return err
}
@@ -40,17 +39,18 @@ func Load() error {
return nil
}
// GetDriver returns the driver with the given type string if it exists
func GetDriver(storageType string) (Driver, error) {
switch strings.ToLower(storageType) {
case "file":
// GetDriver returns the driver with the given type if it exists
func GetDriver(storageType shared.StorageType) (Driver, error) {
switch storageType {
case shared.StorageTypeFile:
return new(FileDriver), nil
case "s3":
return new(S3Driver), nil
case "mongodb":
case shared.StorageTypePostgres:
// TODO: Implement Postgres driver
return nil, errors.New("TODO")
case shared.StorageTypeMongoDB:
return new(MongoDBDriver), nil
case "sql":
return new(SQLDriver), nil
case shared.StorageTypeS3:
return new(S3Driver), nil
default:
return nil, fmt.Errorf("invalid storage type '%s'", storageType)
}

View File

@@ -9,8 +9,8 @@ import (
"strings"
"time"
"github.com/lus/pasty/internal/env"
"github.com/lus/pasty/internal/pastes"
"github.com/lus/pasty/internal/config"
"github.com/lus/pasty/internal/shared"
)
// FileDriver represents the file storage driver
@@ -20,7 +20,7 @@ type FileDriver struct {
// Initialize initializes the file storage driver
func (driver *FileDriver) Initialize() error {
driver.filePath = env.Get("STORAGE_FILE_PATH", "./data")
driver.filePath = config.Current.File.Path
return os.MkdirAll(driver.filePath, os.ModePerm)
}
@@ -60,7 +60,7 @@ func (driver *FileDriver) ListIDs() ([]string, error) {
}
// Get loads a paste
func (driver *FileDriver) Get(id string) (*pastes.Paste, error) {
func (driver *FileDriver) Get(id string) (*shared.Paste, error) {
// Read the file
id = base64.StdEncoding.EncodeToString([]byte(id))
data, err := ioutil.ReadFile(filepath.Join(driver.filePath, id+".json"))
@@ -72,7 +72,7 @@ func (driver *FileDriver) Get(id string) (*pastes.Paste, error) {
}
// Unmarshal the file into a paste
paste := new(pastes.Paste)
paste := new(shared.Paste)
err = json.Unmarshal(data, &paste)
if err != nil {
return nil, err
@@ -81,7 +81,7 @@ func (driver *FileDriver) Get(id string) (*pastes.Paste, error) {
}
// Save saves a paste
func (driver *FileDriver) Save(paste *pastes.Paste) error {
func (driver *FileDriver) Save(paste *shared.Paste) error {
// Marshal the paste
jsonBytes, err := json.Marshal(paste)
if err != nil {
@@ -123,15 +123,15 @@ func (driver *FileDriver) Cleanup() (int, error) {
// Retrieve the paste object
paste, err := driver.Get(id)
if err != nil {
return 0, err
return deleted, err
}
// Delete the paste if it is expired
lifetime := env.Duration("AUTODELETE_LIFETIME", 30*24*time.Hour)
lifetime := config.Current.AutoDelete.Lifetime
if paste.AutoDelete && paste.Created+int64(lifetime.Seconds()) < time.Now().Unix() {
err = driver.Delete(id)
if err != nil {
return 0, err
return deleted, err
}
deleted++
}

View File

@@ -1,24 +1,14 @@
package storage
import (
"strconv"
"github.com/lus/pasty/internal/env"
"github.com/lus/pasty/internal/config"
"github.com/lus/pasty/internal/utils"
)
// AcquireID generates a new unique ID
func AcquireID() (string, error) {
// Read the ID length
rawLength := env.Get("ID_LENGTH", "6")
length, err := strconv.Atoi(rawLength)
if err != nil {
return "", err
}
// Generate the unique ID
for {
id := utils.RandomString(length)
id := utils.RandomString(config.Current.IDLength)
paste, err := Current.Get(id)
if err != nil {
return "", err

View File

@@ -4,8 +4,8 @@ import (
"context"
"time"
"github.com/lus/pasty/internal/env"
"github.com/lus/pasty/internal/pastes"
"github.com/lus/pasty/internal/config"
"github.com/lus/pasty/internal/shared"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
@@ -26,7 +26,7 @@ func (driver *MongoDBDriver) Initialize() error {
defer cancel()
// Connect to the MongoDB host
client, err := mongo.Connect(ctx, options.Client().ApplyURI(env.Get("STORAGE_MONGODB_CONNECTION_STRING", "mongodb://pasty:pasty@example.host/pasty")))
client, err := mongo.Connect(ctx, options.Client().ApplyURI(config.Current.MongoDB.DSN))
if err != nil {
return err
}
@@ -39,8 +39,8 @@ func (driver *MongoDBDriver) Initialize() error {
// Set the driver attributes
driver.client = client
driver.database = env.Get("STORAGE_MONGODB_DATABASE", "pasty")
driver.collection = env.Get("STORAGE_MONGODB_COLLECTION", "pastes")
driver.database = config.Current.MongoDB.Database
driver.collection = config.Current.MongoDB.Collection
return nil
}
@@ -65,7 +65,7 @@ func (driver *MongoDBDriver) ListIDs() ([]string, error) {
}
// Decode all paste documents
var pasteSlice []pastes.Paste
var pasteSlice []shared.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) (*pastes.Paste, error) {
func (driver *MongoDBDriver) Get(id string) (*shared.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) (*pastes.Paste, error) {
}
// Return the retrieved paste object
paste := new(pastes.Paste)
paste := new(shared.Paste)
err = result.Decode(paste)
if err != nil {
return nil, err
@@ -109,7 +109,7 @@ func (driver *MongoDBDriver) Get(id string) (*pastes.Paste, error) {
}
// Save saves a paste
func (driver *MongoDBDriver) Save(paste *pastes.Paste) error {
func (driver *MongoDBDriver) Save(paste *shared.Paste) error {
// Define the collection to use for this database operation
collection := driver.client.Database(driver.database).Collection(driver.collection)
@@ -157,7 +157,7 @@ func (driver *MongoDBDriver) Cleanup() (int, error) {
}
// Delete the paste if it is expired
lifetime := env.Duration("AUTODELETE_LIFETIME", 30*24*time.Hour)
lifetime := config.Current.AutoDelete.Lifetime
if paste.AutoDelete && paste.Created+int64(lifetime.Seconds()) < time.Now().Unix() {
err = driver.Delete(id)
if err != nil {

View File

@@ -8,8 +8,8 @@ import (
"strings"
"time"
"github.com/lus/pasty/internal/env"
"github.com/lus/pasty/internal/pastes"
"github.com/lus/pasty/internal/config"
"github.com/lus/pasty/internal/shared"
"github.com/minio/minio-go/v7"
"github.com/minio/minio-go/v7/pkg/credentials"
)
@@ -22,16 +22,16 @@ type S3Driver struct {
// Initialize initializes the AWS S3 storage driver
func (driver *S3Driver) Initialize() error {
client, err := minio.New(env.Get("STORAGE_S3_ENDPOINT", ""), &minio.Options{
Creds: credentials.NewStaticV4(env.Get("STORAGE_S3_ACCESS_KEY_ID", ""), env.Get("STORAGE_S3_SECRET_ACCESS_KEY", ""), env.Get("STORAGE_S3_SECRET_TOKEN", "")),
Secure: env.Bool("STORAGE_S3_SECURE", true),
Region: env.Get("STORAGE_S3_REGION", ""),
client, err := minio.New(config.Current.S3.Endpoint, &minio.Options{
Creds: credentials.NewStaticV4(config.Current.S3.AccessKeyID, config.Current.S3.SecretAccessKey, config.Current.S3.SecretToken),
Secure: config.Current.S3.Secure,
Region: config.Current.S3.Region,
})
if err != nil {
return err
}
driver.client = client
driver.bucket = env.Get("STORAGE_S3_BUCKET", "pasty")
driver.bucket = config.Current.S3.Bucket
return nil
}
@@ -59,7 +59,7 @@ func (driver *S3Driver) ListIDs() ([]string, error) {
}
// Get loads a paste
func (driver *S3Driver) Get(id string) (*pastes.Paste, error) {
func (driver *S3Driver) Get(id string) (*shared.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) (*pastes.Paste, error) {
}
// Unmarshal the object into a paste
paste := new(pastes.Paste)
paste := new(shared.Paste)
err = json.Unmarshal(data, &paste)
if err != nil {
return nil, err
@@ -83,7 +83,7 @@ func (driver *S3Driver) Get(id string) (*pastes.Paste, error) {
}
// Save saves a paste
func (driver *S3Driver) Save(paste *pastes.Paste) error {
func (driver *S3Driver) Save(paste *shared.Paste) error {
// Marshal the paste
jsonBytes, err := json.Marshal(paste)
if err != nil {
@@ -123,7 +123,7 @@ func (driver *S3Driver) Cleanup() (int, error) {
}
// Delete the paste if it is expired
lifetime := env.Duration("AUTODELETE_LIFETIME", 30*24*time.Hour)
lifetime := config.Current.AutoDelete.Lifetime
if paste.AutoDelete && paste.Created+int64(lifetime.Seconds()) < time.Now().Unix() {
err = driver.Delete(id)
if err != nil {

View File

@@ -1,144 +0,0 @@
package storage
import (
"database/sql"
"time"
_ "github.com/go-sql-driver/mysql"
_ "github.com/lib/pq"
"github.com/lus/pasty/internal/env"
"github.com/lus/pasty/internal/pastes"
_ "github.com/mattn/go-sqlite3"
)
// SQLDriver represents the SQL storage driver
type SQLDriver struct {
database *sql.DB
table string
}
// Initialize initializes the SQL storage driver
func (driver *SQLDriver) Initialize() error {
// Parse the DSN and create a database object
db, err := sql.Open(env.Get("STORAGE_SQL_DRIVER", "sqlite3"), env.Get("STORAGE_SQL_DSN", "./db"))
if err != nil {
return err
}
// Ping the database
err = db.Ping()
if err != nil {
return err
}
// Migrate the database
table := env.Get("STORAGE_SQL_TABLE", "pasty")
_, err = db.Exec(`
CREATE TABLE IF NOT EXISTS ? (
id varchar NOT NULL PRIMARY KEY,
content varchar NOT NULL,
suggestedSyntaxType varchar NOT NULL,
deletionToken varchar NOT NULL,
created bigint NOT NULL,
autoDelete bool NOT NULL
);
`, table)
if err != nil {
return err
}
// Set the database object and table name of the SQL driver
driver.database = db
driver.table = table
return nil
}
// Terminate terminates the SQL storage driver
func (driver *SQLDriver) Terminate() error {
return driver.database.Close()
}
// ListIDs returns a list of all existing paste IDs
func (driver *SQLDriver) ListIDs() ([]string, error) {
// Execute a SELECT query to retrieve all the paste IDs
rows, err := driver.database.Query("SELECT id FROM ?", driver.table)
if err != nil {
return nil, err
}
defer rows.Close()
// Scan the rows into a slice of IDs and return it
var ids []string
err = rows.Scan(&ids)
if err != nil {
return nil, err
}
return ids, nil
}
// Get loads a paste
func (driver *SQLDriver) Get(id string) (*pastes.Paste, error) {
// Execute a SELECT query to retrieve the paste
row := driver.database.QueryRow("SELECT * FROM ? WHERE id = ?", driver.table, id)
err := row.Err()
if err != nil {
if err == sql.ErrNoRows {
return nil, nil
}
return nil, err
}
// Scan the row into a paste and return it
paste := new(pastes.Paste)
err = row.Scan(&paste)
if err != nil {
return nil, err
}
return paste, nil
}
// Save saves a paste
func (driver *SQLDriver) Save(paste *pastes.Paste) error {
// Execute an INSERT statement to create the paste
_, err := driver.database.Exec("INSERT INTO ? (?, ?, ?, ?, ?, ?)", driver.table, paste.ID, paste.Content, paste.SuggestedSyntaxType, paste.DeletionToken, paste.Created, paste.AutoDelete)
return err
}
// Delete deletes a paste
func (driver *SQLDriver) Delete(id string) error {
// Execute a DELETE statement to delete the paste
_, err := driver.database.Exec("DELETE FROM ? WHERE id = ?", driver.table, id)
return err
}
// Cleanup cleans up the expired pastes
func (driver *SQLDriver) Cleanup() (int, error) {
// Retrieve all paste IDs
ids, err := driver.ListIDs()
if err != nil {
return 0, err
}
// Define the amount of deleted items
deleted := 0
// Loop through all pastes
for _, id := range ids {
// Retrieve the paste object
paste, err := driver.Get(id)
if err != nil {
return 0, err
}
// Delete the paste if it is expired
lifetime := env.Duration("AUTODELETE_LIFETIME", 30*24*time.Hour)
if paste.AutoDelete && paste.Created+int64(lifetime.Seconds()) < time.Now().Unix() {
err = driver.Delete(id)
if err != nil {
return 0, err
}
deleted++
}
}
return deleted, nil
}