Skip to content

Commit

Permalink
refactor(pgs): decouple from rest of codebase
Browse files Browse the repository at this point in the history
  • Loading branch information
neurosnap committed Jan 26, 2025
1 parent 954b9b8 commit 654e15b
Show file tree
Hide file tree
Showing 30 changed files with 1,073 additions and 604 deletions.
2 changes: 1 addition & 1 deletion auth/__snapshots__/api_test.snap
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ successfully added pico+ user
---

[TestUser - 1]
[{"id":"1","user_id":"user-1","name":"my-key","key":"nice-pubkey","created_at":"0001-01-01T00:00:00Z"}]
[{"id":"1","user_id":"user-1","name":"my-key","public_key":"nice-pubkey","created_at":"0001-01-01T00:00:00Z"}]
---

[TestAuthApi/rss - 1]
Expand Down
25 changes: 23 additions & 2 deletions cmd/pgs/ssh/main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,28 @@
package main

import "github.com/picosh/pico/pgs"
import (
"github.com/picosh/pico/pgs"
pgsdb "github.com/picosh/pico/pgs/db"
"github.com/picosh/pico/shared"
"github.com/picosh/pico/shared/storage"
"github.com/picosh/utils"
)

func main() {
pgs.StartSshServer()
minioURL := utils.GetEnv("MINIO_URL", "")
minioUser := utils.GetEnv("MINIO_ROOT_USER", "")
minioPass := utils.GetEnv("MINIO_ROOT_PASSWORD", "")
dbURL := utils.GetEnv("DATABASE_URL", "")
logger := shared.CreateLogger("pgs")
dbpool, err := pgsdb.NewDB(dbURL, logger)
if err != nil {
panic(err)
}
st, err := storage.NewStorageMinio(logger, minioURL, minioUser, minioPass)
if err != nil {
panic(err)
}
cfg := pgs.NewPgsConfig(logger, dbpool, st)
killCh := make(chan error)
pgs.StartSshServer(cfg, killCh)
}
24 changes: 22 additions & 2 deletions cmd/pgs/web/main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,27 @@
package main

import "github.com/picosh/pico/pgs"
import (
"github.com/picosh/pico/pgs"
pgsdb "github.com/picosh/pico/pgs/db"
"github.com/picosh/pico/shared"
"github.com/picosh/pico/shared/storage"
"github.com/picosh/utils"
)

func main() {
pgs.StartApiServer()
minioURL := utils.GetEnv("MINIO_URL", "")
minioUser := utils.GetEnv("MINIO_ROOT_USER", "")
minioPass := utils.GetEnv("MINIO_ROOT_PASSWORD", "")
dbURL := utils.GetEnv("DATABASE_URL", "")
logger := shared.CreateLogger("pgs")
dbpool, err := pgsdb.NewDB(dbURL, logger)
if err != nil {
panic(err)
}
st, err := storage.NewStorageMinio(logger, minioURL, minioUser, minioPass)
if err != nil {
panic(err)
}
cfg := pgs.NewPgsConfig(logger, dbpool, st)
pgs.StartApiServer(cfg)
}
6 changes: 3 additions & 3 deletions cmd/scripts/clean-object-store/clean.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"strings"

"github.com/picosh/pico/db"
"github.com/picosh/pico/db/postgres"
"github.com/picosh/pico/pgs"
pgsdb "github.com/picosh/pico/pgs/db"
"github.com/picosh/pico/shared"
"github.com/picosh/pico/shared/storage"
"github.com/picosh/utils"
Expand Down Expand Up @@ -41,10 +41,10 @@ func main() {
picoCfg.MinioURL = os.Getenv("MINIO_URL")
picoCfg.MinioUser = os.Getenv("MINIO_ROOT_USER")
picoCfg.MinioPass = os.Getenv("MINIO_ROOT_PASSWORD")
picoDb := postgres.NewDB(picoCfg.DbURL, picoCfg.Logger)
picoDb, err := pgsdb.NewDB(picoCfg.DbURL, picoCfg.Logger)
bail(err)

var st storage.StorageServe
var err error
st, err = storage.NewStorageMinio(logger, picoCfg.MinioURL, picoCfg.MinioUser, picoCfg.MinioPass)
bail(err)

Expand Down
70 changes: 31 additions & 39 deletions db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,18 @@ var ErrNameInvalid = errors.New("username has invalid characters in it")
var ErrPublicKeyTaken = errors.New("public key is already associated with another user")

type PublicKey struct {
ID string `json:"id"`
UserID string `json:"user_id"`
Name string `json:"name"`
Key string `json:"key"`
CreatedAt *time.Time `json:"created_at"`
ID string `json:"id" db:"id"`
UserID string `json:"user_id" db:"user_id"`
Name string `json:"name" db:"name"`
Key string `json:"public_key" db:"public_key"`
CreatedAt *time.Time `json:"created_at" db:"created_at"`
}

type User struct {
ID string `json:"id"`
Name string `json:"name"`
PublicKey *PublicKey `json:"public_key,omitempty"`
CreatedAt *time.Time `json:"created_at"`
ID string `json:"id" db:"id"`
Name string `json:"name" db:"name"`
PublicKey *PublicKey `json:"public_key,omitempty" db:"public_key,omitempty"`
CreatedAt *time.Time `json:"created_at" db:"created_at"`
}

type PostData struct {
Expand All @@ -53,20 +53,20 @@ func (p *PostData) Scan(value interface{}) error {
}

type Project struct {
ID string `json:"id"`
UserID string `json:"user_id"`
Name string `json:"name"`
ProjectDir string `json:"project_dir"`
Username string `json:"username"`
Acl ProjectAcl `json:"acl"`
Blocked string `json:"blocked"`
CreatedAt *time.Time `json:"created_at"`
UpdatedAt *time.Time `json:"updated_at"`
ID string `json:"id" db:"id"`
UserID string `json:"user_id" db:"user_id"`
Name string `json:"name" db:"name"`
ProjectDir string `json:"project_dir" db:"project_dir"`
Username string `json:"username" db:"username"`
Acl ProjectAcl `json:"acl" db:"acl"`
Blocked string `json:"blocked" db:"blocked"`
CreatedAt *time.Time `json:"created_at" db:"created_at"`
UpdatedAt *time.Time `json:"updated_at" db:"updated_at"`
}

type ProjectAcl struct {
Type string `json:"type"`
Data []string `json:"data"`
Type string `json:"type" db:"type"`
Data []string `json:"data" db:"data"`
}

// Make the Attrs struct implement the driver.Valuer interface. This method
Expand Down Expand Up @@ -218,13 +218,13 @@ type Token struct {
}

type FeatureFlag struct {
ID string `json:"id"`
UserID string `json:"user_id"`
PaymentHistoryID string `json:"payment_history_id"`
Name string `json:"name"`
CreatedAt *time.Time `json:"created_at"`
ExpiresAt *time.Time `json:"expires_at"`
Data FeatureFlagData `json:"data"`
ID string `json:"id" db:"id"`
UserID string `json:"user_id" db:"user_id"`
PaymentHistoryID string `json:"payment_history_id" db:"payment_history_id"`
Name string `json:"name" db:"name"`
CreatedAt *time.Time `json:"created_at" db:"created_at"`
ExpiresAt *time.Time `json:"expires_at" db:"expires_at"`
Data FeatureFlagData `json:"data" db:"data"`
}

func NewFeatureFlag(userID, name string, storageMax uint64, fileMax int64, specialFileMax int64) *FeatureFlag {
Expand Down Expand Up @@ -268,9 +268,9 @@ func (ff *FeatureFlag) IsValid() bool {
}

type FeatureFlagData struct {
StorageMax uint64 `json:"storage_max"`
FileMax int64 `json:"file_max"`
SpecialFileMax int64 `json:"special_file_max"`
StorageMax uint64 `json:"storage_max" db:"storage_max"`
FileMax int64 `json:"file_max" db:"file_max"`
SpecialFileMax int64 `json:"special_file_max" db:"special_file_max"`
}

// Make the Attrs struct implement the driver.Valuer interface. This method
Expand Down Expand Up @@ -354,6 +354,7 @@ type DB interface {
FindUserForName(name string) (*User, error)
FindUserForNameAndKey(name string, pubkey string) (*User, error)
FindUserForKey(name string, pubkey string) (*User, error)
FindUserByPubkey(pubkey string) (*User, error)
FindUser(userID string) (*User, error)
ValidateName(name string) (bool, error)
SetUserName(userID string, name string) error
Expand Down Expand Up @@ -405,16 +406,7 @@ type DB interface {
FindFeedItemsByPostID(postID string) ([]*FeedItem, error)

UpsertProject(userID, name, projectDir string) (*Project, error)
InsertProject(userID, name, projectDir string) (string, error)
UpdateProject(userID, name string) error
UpdateProjectAcl(userID, name string, acl ProjectAcl) error
LinkToProject(userID, projectID, projectDir string, commit bool) error
RemoveProject(projectID string) error
FindProjectByName(userID, name string) (*Project, error)
FindProjectLinks(userID, name string) ([]*Project, error)
FindProjectsByUser(userID string) ([]*Project, error)
FindProjectsByPrefix(userID, name string) ([]*Project, error)
FindAllProjects(page *Pager, by string) (*Paginate[*Project], error)

Close() error
}
Loading

0 comments on commit 654e15b

Please sign in to comment.