Skip to content

Commit

Permalink
refactor package structure
Browse files Browse the repository at this point in the history
  • Loading branch information
lus committed Aug 29, 2022
1 parent 1a574ad commit 69cf6c8
Show file tree
Hide file tree
Showing 15 changed files with 71 additions and 385 deletions.
5 changes: 2 additions & 3 deletions cmd/transfer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"os"

"github.com/lus/pasty/internal/config"
"github.com/lus/pasty/internal/shared"
"github.com/lus/pasty/internal/storage"
)

Expand All @@ -20,7 +19,7 @@ func main() {
config.Load()

// Create and initialize the first (from) driver
from, err := storage.GetDriver(shared.StorageType(os.Args[1]))
from, err := storage.GetDriver(storage.Type(os.Args[1]))
if err != nil {
panic(err)
}
Expand All @@ -30,7 +29,7 @@ func main() {
}

// Create and initialize the second (to) driver
to, err := storage.GetDriver(shared.StorageType(os.Args[2]))
to, err := storage.GetDriver(storage.Type(os.Args[2]))
if err != nil {
panic(err)
}
Expand Down
5 changes: 2 additions & 3 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@ import (
"time"

"github.com/lus/pasty/internal/env"
"github.com/lus/pasty/internal/shared"
)

// Config represents the general application configuration structure
type Config struct {
WebAddress string
StorageType shared.StorageType
StorageType string
HastebinSupport bool
IDLength int
IDCharacters string
Expand Down Expand Up @@ -80,7 +79,7 @@ func Load() {

Current = &Config{
WebAddress: env.MustString("WEB_ADDRESS", ":8080"),
StorageType: shared.StorageType(strings.ToLower(env.MustString("STORAGE_TYPE", "file"))),
StorageType: strings.ToLower(env.MustString("STORAGE_TYPE", "file")),
HastebinSupport: env.MustBool("HASTEBIN_SUPPORT", false),
IDLength: env.MustInt("ID_LENGTH", 6),
IDCharacters: env.MustString("ID_CHARACTERS", "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"),
Expand Down
30 changes: 30 additions & 0 deletions internal/paste/paste.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package paste

import (
"github.com/alexedwards/argon2id"
)

// Paste represents a paste
type Paste struct {
ID string `json:"id"`
Content string `json:"content"`
ModificationToken string `json:"modificationToken,omitempty"`
Created int64 `json:"created"`
Metadata map[string]interface{} `json:"metadata"`
}

// HashModificationToken hashes the current modification token of a paste
func (paste *Paste) HashModificationToken() error {
hash, err := argon2id.CreateHash(paste.ModificationToken, argon2id.DefaultParams)
if err != nil {
return err
}
paste.ModificationToken = hash
return nil
}

// CheckModificationToken checks whether or not the given modification token is correct
func (paste *Paste) CheckModificationToken(modificationToken string) bool {
match, err := argon2id.ComparePasswordAndHash(modificationToken, paste.ModificationToken)
return err == nil && match
}
42 changes: 0 additions & 42 deletions internal/shared/paste.go

This file was deleted.

11 changes: 0 additions & 11 deletions internal/shared/storage_type.go

This file was deleted.

19 changes: 10 additions & 9 deletions internal/storage/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)
}
Expand All @@ -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)
Expand Down
10 changes: 5 additions & 5 deletions internal/storage/file/file_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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"))
Expand All @@ -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
Expand All @@ -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 {
Expand Down
10 changes: 5 additions & 5 deletions internal/storage/mongodb/mongodb_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand All @@ -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)

Expand All @@ -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
Expand All @@ -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)

Expand Down
8 changes: 4 additions & 4 deletions internal/storage/postgres/postgres_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions internal/storage/s3/s3_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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 {
Expand All @@ -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
Expand All @@ -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 {
Expand Down
Loading

0 comments on commit 69cf6c8

Please sign in to comment.