mirror of
https://github.com/lus/pasty.git
synced 2023-08-10 21:13:09 +03:00
Implement MongoDB storage driver
This commit is contained in:
@@ -32,6 +32,9 @@ func Load() error {
|
||||
case "s3":
|
||||
driver = new(S3Driver)
|
||||
break
|
||||
case "mongodb":
|
||||
driver = new(MongoDBDriver)
|
||||
break
|
||||
default:
|
||||
return fmt.Errorf("invalid storage type '%s'", storageType)
|
||||
}
|
||||
|
@@ -12,13 +12,13 @@ import (
|
||||
|
||||
// FileDriver represents the file storage driver
|
||||
type FileDriver struct {
|
||||
FilePath string
|
||||
filePath string
|
||||
}
|
||||
|
||||
// Initialize initializes the file storage driver
|
||||
func (driver *FileDriver) Initialize() error {
|
||||
driver.FilePath = env.Get("STORAGE_FILE_PATH", "./data")
|
||||
return os.MkdirAll(driver.FilePath, os.ModePerm)
|
||||
driver.filePath = env.Get("STORAGE_FILE_PATH", "./data")
|
||||
return os.MkdirAll(driver.filePath, os.ModePerm)
|
||||
}
|
||||
|
||||
// Terminate terminates the file storage driver (does nothing, because the file storage driver does not need any termination)
|
||||
@@ -29,7 +29,7 @@ func (driver *FileDriver) Terminate() error {
|
||||
// Get loads a paste
|
||||
func (driver *FileDriver) Get(id snowflake.ID) (*pastes.Paste, error) {
|
||||
// Read the file
|
||||
data, err := ioutil.ReadFile(filepath.Join(driver.FilePath, id.String()+".json"))
|
||||
data, err := ioutil.ReadFile(filepath.Join(driver.filePath, id.String()+".json"))
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
return nil, nil
|
||||
@@ -55,7 +55,7 @@ func (driver *FileDriver) Save(paste *pastes.Paste) error {
|
||||
}
|
||||
|
||||
// Create the file to save the paste to
|
||||
file, err := os.Create(filepath.Join(driver.FilePath, paste.ID.String()+".json"))
|
||||
file, err := os.Create(filepath.Join(driver.filePath, paste.ID.String()+".json"))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -68,5 +68,5 @@ func (driver *FileDriver) Save(paste *pastes.Paste) error {
|
||||
|
||||
// Delete deletes a paste
|
||||
func (driver *FileDriver) Delete(id snowflake.ID) error {
|
||||
return os.Remove(filepath.Join(driver.FilePath, id.String()+".json"))
|
||||
return os.Remove(filepath.Join(driver.filePath, id.String()+".json"))
|
||||
}
|
||||
|
105
internal/storage/mongodb_driver.go
Normal file
105
internal/storage/mongodb_driver.go
Normal file
@@ -0,0 +1,105 @@
|
||||
package storage
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/Lukaesebrot/pasty/internal/env"
|
||||
"github.com/Lukaesebrot/pasty/internal/pastes"
|
||||
"github.com/bwmarrin/snowflake"
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
"go.mongodb.org/mongo-driver/mongo/readpref"
|
||||
"time"
|
||||
)
|
||||
|
||||
// MongoDBDriver represents the MongoDB storage driver
|
||||
type MongoDBDriver struct {
|
||||
client *mongo.Client
|
||||
database string
|
||||
collection string
|
||||
}
|
||||
|
||||
// Initialize initializes the MongoDB storage driver
|
||||
func (driver *MongoDBDriver) Initialize() error {
|
||||
// Define the context for the following database operation
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
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")))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Ping the MongoDB host
|
||||
err = client.Ping(ctx, readpref.Primary())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Set the driver attributes
|
||||
driver.client = client
|
||||
driver.database = env.Get("STORAGE_MONGODB_DATABASE", "pasty")
|
||||
driver.collection = env.Get("STORAGE_MONGODB_COLLECTION", "pastes")
|
||||
return nil
|
||||
}
|
||||
|
||||
// Terminate terminates the MongoDB storage driver
|
||||
func (driver *MongoDBDriver) Terminate() error {
|
||||
return driver.client.Disconnect(context.TODO())
|
||||
}
|
||||
|
||||
// Get loads a paste
|
||||
func (driver *MongoDBDriver) Get(id snowflake.ID) (*pastes.Paste, error) {
|
||||
// Define the collection to use for this database operation
|
||||
collection := driver.client.Database(driver.database).Collection(driver.collection)
|
||||
|
||||
// Define the context for the following database operation
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
|
||||
// Try to retrieve the corresponding paste document
|
||||
filter := bson.M{"_id": id.String()}
|
||||
result := collection.FindOne(ctx, filter)
|
||||
err := result.Err()
|
||||
if err != nil {
|
||||
if err == mongo.ErrNoDocuments {
|
||||
return nil, nil
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Return the retrieved paste object
|
||||
paste := new(pastes.Paste)
|
||||
err = result.Decode(paste)
|
||||
return paste, err
|
||||
}
|
||||
|
||||
// Save saves a paste
|
||||
func (driver *MongoDBDriver) Save(paste *pastes.Paste) error {
|
||||
// Define the collection to use for this database operation
|
||||
collection := driver.client.Database(driver.database).Collection(driver.collection)
|
||||
|
||||
// Define the context for the following database operation
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
|
||||
// Insert the paste object
|
||||
_, err := collection.InsertOne(ctx, paste)
|
||||
return err
|
||||
}
|
||||
|
||||
// Delete deletes a paste
|
||||
func (driver *MongoDBDriver) Delete(id snowflake.ID) error {
|
||||
// Define the collection to use for this database operation
|
||||
collection := driver.client.Database(driver.database).Collection(driver.collection)
|
||||
|
||||
// Define the context for the following database operation
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
|
||||
// Delete the document
|
||||
filter := bson.M{"_id": id.String()}
|
||||
_, err := collection.DeleteOne(ctx, filter)
|
||||
return err
|
||||
}
|
@@ -14,8 +14,8 @@ import (
|
||||
|
||||
// S3Driver represents the AWS S3 storage driver
|
||||
type S3Driver struct {
|
||||
Client *minio.Client
|
||||
Bucket string
|
||||
client *minio.Client
|
||||
bucket string
|
||||
}
|
||||
|
||||
// Initialize initializes the AWS S3 storage driver
|
||||
@@ -28,8 +28,8 @@ func (driver *S3Driver) Initialize() error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
driver.Client = client
|
||||
driver.Bucket = env.Get("STORAGE_S3_BUCKET", "pasty")
|
||||
driver.client = client
|
||||
driver.bucket = env.Get("STORAGE_S3_BUCKET", "pasty")
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -41,7 +41,7 @@ func (driver *S3Driver) Terminate() error {
|
||||
// Get loads a paste
|
||||
func (driver *S3Driver) Get(id snowflake.ID) (*pastes.Paste, error) {
|
||||
// Read the object
|
||||
object, err := driver.Client.GetObject(context.Background(), driver.Bucket, id.String()+".json", minio.GetObjectOptions{})
|
||||
object, err := driver.client.GetObject(context.Background(), driver.bucket, id.String()+".json", minio.GetObjectOptions{})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -69,7 +69,7 @@ func (driver *S3Driver) Save(paste *pastes.Paste) error {
|
||||
|
||||
// Put the object
|
||||
reader := bytes.NewReader(jsonBytes)
|
||||
_, err = driver.Client.PutObject(context.Background(), driver.Bucket, paste.ID.String()+".json", reader, reader.Size(), minio.PutObjectOptions{
|
||||
_, err = driver.client.PutObject(context.Background(), driver.bucket, paste.ID.String()+".json", reader, reader.Size(), minio.PutObjectOptions{
|
||||
ContentType: "application/json",
|
||||
})
|
||||
return err
|
||||
@@ -77,5 +77,5 @@ func (driver *S3Driver) Save(paste *pastes.Paste) error {
|
||||
|
||||
// Delete deletes a paste
|
||||
func (driver *S3Driver) Delete(id snowflake.ID) error {
|
||||
return driver.Client.RemoveObject(context.Background(), driver.Bucket, id.String()+".json", minio.RemoveObjectOptions{})
|
||||
return driver.client.RemoveObject(context.Background(), driver.bucket, id.String()+".json", minio.RemoveObjectOptions{})
|
||||
}
|
||||
|
Reference in New Issue
Block a user