Skip to content

Commit 69cf6c8

Browse files
committed
refactor package structure
1 parent 1a574ad commit 69cf6c8

File tree

15 files changed

+71
-385
lines changed

15 files changed

+71
-385
lines changed

cmd/transfer/main.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"os"
66

77
"github.com/lus/pasty/internal/config"
8-
"github.com/lus/pasty/internal/shared"
98
"github.com/lus/pasty/internal/storage"
109
)
1110

@@ -20,7 +19,7 @@ func main() {
2019
config.Load()
2120

2221
// Create and initialize the first (from) driver
23-
from, err := storage.GetDriver(shared.StorageType(os.Args[1]))
22+
from, err := storage.GetDriver(storage.Type(os.Args[1]))
2423
if err != nil {
2524
panic(err)
2625
}
@@ -30,7 +29,7 @@ func main() {
3029
}
3130

3231
// Create and initialize the second (to) driver
33-
to, err := storage.GetDriver(shared.StorageType(os.Args[2]))
32+
to, err := storage.GetDriver(storage.Type(os.Args[2]))
3433
if err != nil {
3534
panic(err)
3635
}

internal/config/config.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,12 @@ import (
55
"time"
66

77
"github.com/lus/pasty/internal/env"
8-
"github.com/lus/pasty/internal/shared"
98
)
109

1110
// Config represents the general application configuration structure
1211
type Config struct {
1312
WebAddress string
14-
StorageType shared.StorageType
13+
StorageType string
1514
HastebinSupport bool
1615
IDLength int
1716
IDCharacters string
@@ -80,7 +79,7 @@ func Load() {
8079

8180
Current = &Config{
8281
WebAddress: env.MustString("WEB_ADDRESS", ":8080"),
83-
StorageType: shared.StorageType(strings.ToLower(env.MustString("STORAGE_TYPE", "file"))),
82+
StorageType: strings.ToLower(env.MustString("STORAGE_TYPE", "file")),
8483
HastebinSupport: env.MustBool("HASTEBIN_SUPPORT", false),
8584
IDLength: env.MustInt("ID_LENGTH", 6),
8685
IDCharacters: env.MustString("ID_CHARACTERS", "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"),

internal/paste/paste.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package paste
2+
3+
import (
4+
"github.com/alexedwards/argon2id"
5+
)
6+
7+
// Paste represents a paste
8+
type Paste struct {
9+
ID string `json:"id"`
10+
Content string `json:"content"`
11+
ModificationToken string `json:"modificationToken,omitempty"`
12+
Created int64 `json:"created"`
13+
Metadata map[string]interface{} `json:"metadata"`
14+
}
15+
16+
// HashModificationToken hashes the current modification token of a paste
17+
func (paste *Paste) HashModificationToken() error {
18+
hash, err := argon2id.CreateHash(paste.ModificationToken, argon2id.DefaultParams)
19+
if err != nil {
20+
return err
21+
}
22+
paste.ModificationToken = hash
23+
return nil
24+
}
25+
26+
// CheckModificationToken checks whether or not the given modification token is correct
27+
func (paste *Paste) CheckModificationToken(modificationToken string) bool {
28+
match, err := argon2id.ComparePasswordAndHash(modificationToken, paste.ModificationToken)
29+
return err == nil && match
30+
}

internal/shared/paste.go

Lines changed: 0 additions & 42 deletions
This file was deleted.

internal/shared/storage_type.go

Lines changed: 0 additions & 11 deletions
This file was deleted.

internal/storage/driver.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ package storage
22

33
import (
44
"fmt"
5+
"strings"
56

67
"github.com/lus/pasty/internal/config"
7-
"github.com/lus/pasty/internal/shared"
8+
"github.com/lus/pasty/internal/paste"
89
"github.com/lus/pasty/internal/storage/file"
910
"github.com/lus/pasty/internal/storage/mongodb"
1011
"github.com/lus/pasty/internal/storage/postgres"
@@ -19,8 +20,8 @@ type Driver interface {
1920
Initialize() error
2021
Terminate() error
2122
ListIDs() ([]string, error)
22-
Get(id string) (*shared.Paste, error)
23-
Save(paste *shared.Paste) error
23+
Get(id string) (*paste.Paste, error)
24+
Save(paste *paste.Paste) error
2425
Delete(id string) error
2526
Cleanup() (int, error)
2627
}
@@ -43,15 +44,15 @@ func Load() error {
4344
}
4445

4546
// GetDriver returns the driver with the given type if it exists
46-
func GetDriver(storageType shared.StorageType) (Driver, error) {
47-
switch storageType {
48-
case shared.StorageTypeFile:
47+
func GetDriver(storageType string) (Driver, error) {
48+
switch strings.TrimSpace(strings.ToLower(storageType)) {
49+
case "file":
4950
return new(file.FileDriver), nil
50-
case shared.StorageTypePostgres:
51+
case "postgres":
5152
return new(postgres.PostgresDriver), nil
52-
case shared.StorageTypeMongoDB:
53+
case "mongodb":
5354
return new(mongodb.MongoDBDriver), nil
54-
case shared.StorageTypeS3:
55+
case "s3":
5556
return new(s3.S3Driver), nil
5657
default:
5758
return nil, fmt.Errorf("invalid storage type '%s'", storageType)

internal/storage/file/file_driver.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
"time"
1111

1212
"github.com/lus/pasty/internal/config"
13-
"github.com/lus/pasty/internal/shared"
13+
"github.com/lus/pasty/internal/paste"
1414
)
1515

1616
// FileDriver represents the file storage driver
@@ -35,7 +35,7 @@ func (driver *FileDriver) ListIDs() ([]string, error) {
3535
var ids []string
3636

3737
// Fill the IDs slice
38-
err := filepath.Walk(driver.filePath, func(path string, info os.FileInfo, err error) error {
38+
err := filepath.Walk(driver.filePath, func(_ string, info os.FileInfo, err error) error {
3939
// Check if a walking error occurred
4040
if err != nil {
4141
return err
@@ -65,7 +65,7 @@ func (driver *FileDriver) ListIDs() ([]string, error) {
6565
}
6666

6767
// Get loads a paste
68-
func (driver *FileDriver) Get(id string) (*shared.Paste, error) {
68+
func (driver *FileDriver) Get(id string) (*paste.Paste, error) {
6969
// Read the file
7070
id = base64.StdEncoding.EncodeToString([]byte(id))
7171
data, err := ioutil.ReadFile(filepath.Join(driver.filePath, id+".json"))
@@ -77,7 +77,7 @@ func (driver *FileDriver) Get(id string) (*shared.Paste, error) {
7777
}
7878

7979
// Unmarshal the file into a paste
80-
paste := new(shared.Paste)
80+
paste := new(paste.Paste)
8181
err = json.Unmarshal(data, &paste)
8282
if err != nil {
8383
return nil, err
@@ -86,7 +86,7 @@ func (driver *FileDriver) Get(id string) (*shared.Paste, error) {
8686
}
8787

8888
// Save saves a paste
89-
func (driver *FileDriver) Save(paste *shared.Paste) error {
89+
func (driver *FileDriver) Save(paste *paste.Paste) error {
9090
// Marshal the paste
9191
jsonBytes, err := json.Marshal(paste)
9292
if err != nil {

internal/storage/mongodb/mongodb_driver.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"time"
66

77
"github.com/lus/pasty/internal/config"
8-
"github.com/lus/pasty/internal/shared"
8+
"github.com/lus/pasty/internal/paste"
99
"go.mongodb.org/mongo-driver/bson"
1010
"go.mongodb.org/mongo-driver/mongo"
1111
"go.mongodb.org/mongo-driver/mongo/options"
@@ -65,7 +65,7 @@ func (driver *MongoDBDriver) ListIDs() ([]string, error) {
6565
}
6666

6767
// Decode all paste documents
68-
var pasteSlice []shared.Paste
68+
var pasteSlice []paste.Paste
6969
err = result.All(ctx, &pasteSlice)
7070
if err != nil {
7171
return nil, err
@@ -80,7 +80,7 @@ func (driver *MongoDBDriver) ListIDs() ([]string, error) {
8080
}
8181

8282
// Get loads a paste
83-
func (driver *MongoDBDriver) Get(id string) (*shared.Paste, error) {
83+
func (driver *MongoDBDriver) Get(id string) (*paste.Paste, error) {
8484
// Define the collection to use for this database operation
8585
collection := driver.client.Database(driver.database).Collection(driver.collection)
8686

@@ -100,7 +100,7 @@ func (driver *MongoDBDriver) Get(id string) (*shared.Paste, error) {
100100
}
101101

102102
// Return the retrieved paste object
103-
paste := new(shared.Paste)
103+
paste := new(paste.Paste)
104104
err = result.Decode(paste)
105105
if err != nil {
106106
return nil, err
@@ -109,7 +109,7 @@ func (driver *MongoDBDriver) Get(id string) (*shared.Paste, error) {
109109
}
110110

111111
// Save saves a paste
112-
func (driver *MongoDBDriver) Save(paste *shared.Paste) error {
112+
func (driver *MongoDBDriver) Save(paste *paste.Paste) error {
113113
// Define the collection to use for this database operation
114114
collection := driver.client.Database(driver.database).Collection(driver.collection)
115115

internal/storage/postgres/postgres_driver.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
"github.com/jackc/pgx/v4/pgxpool"
1313
"github.com/johejo/golang-migrate-extra/source/iofs"
1414
"github.com/lus/pasty/internal/config"
15-
"github.com/lus/pasty/internal/shared"
15+
"github.com/lus/pasty/internal/paste"
1616
)
1717

1818
//go:embed migrations/*.sql
@@ -76,12 +76,12 @@ func (driver *PostgresDriver) ListIDs() ([]string, error) {
7676
}
7777

7878
// Get loads a paste
79-
func (driver *PostgresDriver) Get(id string) (*shared.Paste, error) {
79+
func (driver *PostgresDriver) Get(id string) (*paste.Paste, error) {
8080
query := "SELECT * FROM pastes WHERE id = $1"
8181

8282
row := driver.pool.QueryRow(context.Background(), query, id)
8383

84-
paste := new(shared.Paste)
84+
paste := new(paste.Paste)
8585
if err := row.Scan(&paste.ID, &paste.Content, &paste.ModificationToken, &paste.Created, &paste.Metadata); err != nil {
8686
if errors.Is(err, pgx.ErrNoRows) {
8787
return nil, nil
@@ -92,7 +92,7 @@ func (driver *PostgresDriver) Get(id string) (*shared.Paste, error) {
9292
}
9393

9494
// Save saves a paste
95-
func (driver *PostgresDriver) Save(paste *shared.Paste) error {
95+
func (driver *PostgresDriver) Save(paste *paste.Paste) error {
9696
query := `
9797
INSERT INTO pastes (id, content, "modificationToken", created, metadata)
9898
VALUES ($1, $2, $3, $4, $5)

internal/storage/s3/s3_driver.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
"time"
1010

1111
"github.com/lus/pasty/internal/config"
12-
"github.com/lus/pasty/internal/shared"
12+
"github.com/lus/pasty/internal/paste"
1313
"github.com/minio/minio-go/v7"
1414
"github.com/minio/minio-go/v7/pkg/credentials"
1515
)
@@ -59,7 +59,7 @@ func (driver *S3Driver) ListIDs() ([]string, error) {
5959
}
6060

6161
// Get loads a paste
62-
func (driver *S3Driver) Get(id string) (*shared.Paste, error) {
62+
func (driver *S3Driver) Get(id string) (*paste.Paste, error) {
6363
// Read the object
6464
object, err := driver.client.GetObject(context.Background(), driver.bucket, id+".json", minio.GetObjectOptions{})
6565
if err != nil {
@@ -74,7 +74,7 @@ func (driver *S3Driver) Get(id string) (*shared.Paste, error) {
7474
}
7575

7676
// Unmarshal the object into a paste
77-
paste := new(shared.Paste)
77+
paste := new(paste.Paste)
7878
err = json.Unmarshal(data, &paste)
7979
if err != nil {
8080
return nil, err
@@ -83,7 +83,7 @@ func (driver *S3Driver) Get(id string) (*shared.Paste, error) {
8383
}
8484

8585
// Save saves a paste
86-
func (driver *S3Driver) Save(paste *shared.Paste) error {
86+
func (driver *S3Driver) Save(paste *paste.Paste) error {
8787
// Marshal the paste
8888
jsonBytes, err := json.Marshal(paste)
8989
if err != nil {

0 commit comments

Comments
 (0)