From 3f842bf653a7128eddfc5b2cc8d10d1eb4f36bf8 Mon Sep 17 00:00:00 2001 From: Abhishek Shroff Date: Tue, 22 Oct 2024 02:09:34 +0530 Subject: [PATCH] [server][core] Nearly remove all functionality from app.App --- server/internal/api/v1/api.go | 9 ++- server/internal/api/v1/auth_api.go | 10 ++-- server/internal/api/v1/login/login.go | 7 +-- server/internal/api/v1/my/my.go | 19 +++---- server/internal/api/webdav/handler.go | 11 ++-- server/internal/command/command.go | 3 +- server/internal/command/common/common.go | 29 +--------- server/internal/command/schema/cmd.go | 9 ++- server/internal/command/serve/cmd.go | 5 +- server/internal/command/storage/cmd.go | 5 +- server/internal/command/user/add.go | 2 +- server/internal/core/app/app.go | 15 +---- server/internal/core/fs/filesystem.go | 6 +- server/internal/core/fs/fs.go | 11 ++-- server/internal/core/user/user.go | 4 -- server/internal/core/user/user_lists.go | 71 +++++++++++------------- 16 files changed, 79 insertions(+), 137 deletions(-) diff --git a/server/internal/api/v1/api.go b/server/internal/api/v1/api.go index 7328c4f9..fbe984be 100644 --- a/server/internal/api/v1/api.go +++ b/server/internal/api/v1/api.go @@ -6,16 +6,15 @@ import ( "github.com/shroff/phylum/server/internal/api/v1/login" "github.com/shroff/phylum/server/internal/api/v1/my" "github.com/shroff/phylum/server/internal/api/v1/users" - "github.com/shroff/phylum/server/internal/core/app" ) -func Setup(r *gin.RouterGroup, a app.App) { +func Setup(r *gin.RouterGroup) { // Unauthenticated routes - login.SetupRoutes(r, a) + login.SetupRoutes(r) // Authenticated routes - r.Use(CreateApiAuthHandler(a)) + r.Use(CreateApiAuthHandler()) fs.SetupRoutes(r) - my.SetupRoutes(r, a) + my.SetupRoutes(r) users.SetupUserRoutes(r) } diff --git a/server/internal/api/v1/auth_api.go b/server/internal/api/v1/auth_api.go index 616046fb..f801f122 100644 --- a/server/internal/api/v1/auth_api.go +++ b/server/internal/api/v1/auth_api.go @@ -6,7 +6,7 @@ import ( "github.com/gin-gonic/gin" "github.com/shroff/phylum/server/internal/api/auth" - "github.com/shroff/phylum/server/internal/core/app" + "github.com/shroff/phylum/server/internal/core/fs" "github.com/shroff/phylum/server/internal/core/user" ) @@ -19,18 +19,18 @@ const ( authHeaderBearer = 2 ) -func CreateApiAuthHandler(a app.App) func(c *gin.Context) { +func CreateApiAuthHandler() func(c *gin.Context) { return func(c *gin.Context) { ctx := c.Request.Context() - if user, err := extractUserDetails(a, c); err != nil { + if user, err := extractUserDetails(c); err != nil { panic(err) } else { - auth.Set(c, user, a.OpenFileSystem(ctx, user)) + auth.Set(c, user, fs.Open(ctx, user)) } } } -func extractUserDetails(a app.App, c *gin.Context) (user.User, error) { +func extractUserDetails(c *gin.Context) (user.User, error) { userManager := user.CreateManager(c.Request.Context()) if header := c.Request.Header.Get("Authorization"); header == "" { return user.User{}, auth.ErrRequired diff --git a/server/internal/api/v1/login/login.go b/server/internal/api/v1/login/login.go index 7005939f..1c8fff57 100644 --- a/server/internal/api/v1/login/login.go +++ b/server/internal/api/v1/login/login.go @@ -4,7 +4,6 @@ import ( "net/http" "github.com/gin-gonic/gin" - "github.com/shroff/phylum/server/internal/core/app" "github.com/shroff/phylum/server/internal/core/errors" "github.com/shroff/phylum/server/internal/core/user" ) @@ -14,12 +13,12 @@ type loginResponse struct { User user.User `json:"user"` } -func SetupRoutes(r *gin.RouterGroup, a app.App) { +func SetupRoutes(r *gin.RouterGroup) { group := r.Group("/login") - group.POST("/password", createLoginRouteHandler(a)) + group.POST("/password", createLoginRouteHandler()) } -func createLoginRouteHandler(a app.App) func(c *gin.Context) { +func createLoginRouteHandler() func(c *gin.Context) { return func(c *gin.Context) { email, ok := c.GetQuery("email") if !ok { diff --git a/server/internal/api/v1/my/my.go b/server/internal/api/v1/my/my.go index 69dc7001..9ff1b66d 100644 --- a/server/internal/api/v1/my/my.go +++ b/server/internal/api/v1/my/my.go @@ -4,7 +4,6 @@ import ( "github.com/gin-gonic/gin" "github.com/shroff/phylum/server/internal/api/auth" fsapi "github.com/shroff/phylum/server/internal/api/v1/fs" - "github.com/shroff/phylum/server/internal/core/app" "github.com/shroff/phylum/server/internal/core/fs" "github.com/shroff/phylum/server/internal/core/user" ) @@ -18,10 +17,10 @@ type mySharedResponse struct { Shared []fs.Resource `json:"shared"` } -func SetupRoutes(r *gin.RouterGroup, a app.App) { +func SetupRoutes(r *gin.RouterGroup) { group := r.Group("/my") group.GET("/home", handleMyHomeRoute) - group.GET("/shared", createMySharedRouteHandler(a)) + group.GET("/shared", createMySharedRouteHandler()) } func handleMyHomeRoute(c *gin.Context) { @@ -38,13 +37,13 @@ func handleMyHomeRoute(c *gin.Context) { }) } -func createMySharedRouteHandler(a app.App) func(*gin.Context) { +func createMySharedRouteHandler() func(*gin.Context) { return func(c *gin.Context) { - u := auth.GetUser(c) - shared, err := user.CreateManager(c.Request.Context()).GetSharedResources(u) - if err != nil { - panic(err) - } - c.JSON(200, mySharedResponse{Shared: shared}) + // u := auth.GetUser(c) + // // shared, err := user.CreateManager(c.Request.Context()).GetSharedResources(u) + // if err != nil { + // panic(err) + // } + // c.JSON(200, mySharedResponse{Shared: shared}) } } diff --git a/server/internal/api/webdav/handler.go b/server/internal/api/webdav/handler.go index f51497b0..847ff1f1 100644 --- a/server/internal/api/webdav/handler.go +++ b/server/internal/api/webdav/handler.go @@ -8,25 +8,23 @@ import ( "github.com/gin-gonic/gin" "github.com/shroff/phylum/server/internal/api/auth" webdav "github.com/shroff/phylum/server/internal/api/webdav/impl" - "github.com/shroff/phylum/server/internal/core/app" "github.com/shroff/phylum/server/internal/core/fs" "github.com/shroff/phylum/server/internal/core/user" "github.com/sirupsen/logrus" ) type handler struct { - app app.App prefix string lockSystem webdav.LockSystem } -func createBasicAuthHandler(a app.App) func(c *gin.Context) { +func createBasicAuthHandler() func(c *gin.Context) { return func(c *gin.Context) { authSuccess := false if username, pass, ok := c.Request.BasicAuth(); ok { ctx := c.Request.Context() if u, err := user.CreateManager(ctx).VerifyUserPassword(username, pass); err == nil { - auth.Set(c, u, a.OpenFileSystem(ctx, u)) + auth.Set(c, u, fs.Open(ctx, u)) authSuccess = true } else if !errors.Is(err, user.ErrCredentialsInvalid) { panic(err) @@ -39,14 +37,13 @@ func createBasicAuthHandler(a app.App) func(c *gin.Context) { } } -func SetupHandler(r *gin.RouterGroup, app app.App) { +func SetupHandler(r *gin.RouterGroup) { logrus.Debug("Setting up WebDAV access at " + r.BasePath()) handler := &handler{ - app: app, prefix: r.BasePath(), lockSystem: webdav.NewMemLS(), } - r.Use(createBasicAuthHandler(app)) + r.Use(createBasicAuthHandler()) r.Handle("OPTIONS", "*path", handler.HandleRequest) r.Handle("GET", "*path", handler.HandleRequest) r.Handle("PUT", "*path", handler.HandleRequest) diff --git a/server/internal/command/command.go b/server/internal/command/command.go index 413e868d..7d792b14 100644 --- a/server/internal/command/command.go +++ b/server/internal/command/command.go @@ -4,7 +4,6 @@ import ( "os" "path" - "github.com/shroff/phylum/server/internal/command/common" "github.com/shroff/phylum/server/internal/command/fs" "github.com/shroff/phylum/server/internal/command/schema" "github.com/shroff/phylum/server/internal/command/serve" @@ -57,7 +56,7 @@ func SetupCommand() { defer func() { logrus.Debug("Shutting Down App") - common.DB().Close() + db.Get().Close() }() rootCmd.AddCommand([]*cobra.Command{ diff --git a/server/internal/command/common/common.go b/server/internal/command/common/common.go index f0e48084..e8130aaa 100644 --- a/server/internal/command/common/common.go +++ b/server/internal/command/common/common.go @@ -5,38 +5,13 @@ import ( "fmt" "os" - "github.com/shroff/phylum/server/internal/core/app" - "github.com/shroff/phylum/server/internal/core/db" "github.com/shroff/phylum/server/internal/core/fs" - "github.com/shroff/phylum/server/internal/core/storage" "github.com/shroff/phylum/server/internal/core/user" "github.com/spf13/cobra" ) -var s storage.Storage -var a app.App var f fs.FileSystem -func DB() *db.DbHandler { - return db.Get() -} - -func Storage() storage.Storage { - return storage.Get() -} - -func App() app.App { - if a == nil { - if app, err := app.Create(context.Background(), DB(), Storage()); err != nil { - fmt.Println("could not initialize app: " + err.Error()) - os.Exit(3) - } else { - a = app - } - } - return a -} - func RootFileSystem() fs.FileSystem { var rootUser user.User if user, err := user.CreateManager(context.Background()).UserByEmail(user.DefaultUserUsername); err != nil { @@ -46,7 +21,7 @@ func RootFileSystem() fs.FileSystem { rootUser = user } - return App().OpenFileSystem(context.Background(), rootUser) + return fs.Open(context.Background(), rootUser) } func UserFileSystem(cmd *cobra.Command) fs.FileSystem { @@ -59,7 +34,7 @@ func UserFileSystem(cmd *cobra.Command) fs.FileSystem { fmt.Println("could not find user '" + value + "': " + err.Error()) os.Exit(5) } else { - f = App().OpenFileSystem(context.Background(), u) + f = fs.Open(context.Background(), u) } } } diff --git a/server/internal/command/schema/cmd.go b/server/internal/command/schema/cmd.go index e19b5957..dd14e960 100644 --- a/server/internal/command/schema/cmd.go +++ b/server/internal/command/schema/cmd.go @@ -6,7 +6,6 @@ import ( "os" "strconv" - "github.com/shroff/phylum/server/internal/command/common" "github.com/shroff/phylum/server/internal/core/db" "github.com/spf13/cobra" "github.com/spf13/viper" @@ -30,11 +29,11 @@ func setupSchemaResetCommand() *cobra.Command { Short: "Reset Database Schema", Run: func(cmd *cobra.Command, args []string) { viper.Set("auto_migrate", false) - if err := common.DB().DeleteSchema(context.Background()); err != nil { + if err := db.Get().DeleteSchema(context.Background()); err != nil { fmt.Println("unable to delete database schema: " + err.Error()) os.Exit(1) } - if err := common.DB().Migrate(context.Background(), -1); err != nil { + if err := db.Get().Migrate(context.Background(), -1); err != nil { fmt.Println("unable to migrate database schema: " + err.Error()) os.Exit(1) } @@ -51,13 +50,13 @@ func setupSchemaMigrateCommand() *cobra.Command { ctx := context.Background() if args[0] == "auto" || args[0] == "latest" { db.AutoMigrate = true - common.DB() + db.Get() } else { db.AutoMigrate = false if v, err := strconv.Atoi(args[0]); err != nil { fmt.Println(err.Error()) os.Exit(3) - } else if err := common.DB().Migrate(ctx, v); err != nil { + } else if err := db.Get().Migrate(ctx, v); err != nil { fmt.Println(err.Error()) os.Exit(4) } diff --git a/server/internal/command/serve/cmd.go b/server/internal/command/serve/cmd.go index ab065756..76299982 100644 --- a/server/internal/command/serve/cmd.go +++ b/server/internal/command/serve/cmd.go @@ -4,7 +4,6 @@ import ( "github.com/fvbock/endless" apiv1 "github.com/shroff/phylum/server/internal/api/v1" "github.com/shroff/phylum/server/internal/api/webdav" - "github.com/shroff/phylum/server/internal/command/common" "github.com/sirupsen/logrus" "github.com/spf13/cobra" "github.com/spf13/viper" @@ -18,8 +17,8 @@ func SetupCommand() *cobra.Command { config := viper.GetViper() engine := createEngine(config.GetBool("debug"), config.GetBool("log_body"), config.GetBool("cors_enabled"), config.GetStringSlice("cors_origins")) - webdav.SetupHandler(engine.Group(config.GetString("webdav_prefix")), common.App()) - apiv1.Setup(engine.Group("/api/v1"), common.App()) + webdav.SetupHandler(engine.Group(config.GetString("webdav_prefix"))) + apiv1.Setup(engine.Group("/api/v1")) server := endless.NewServer(config.GetString("listen"), engine) server.BeforeBegin = func(addr string) { diff --git a/server/internal/command/storage/cmd.go b/server/internal/command/storage/cmd.go index 435a3dbc..d30be3d2 100644 --- a/server/internal/command/storage/cmd.go +++ b/server/internal/command/storage/cmd.go @@ -7,7 +7,6 @@ import ( "os" "strings" - "github.com/shroff/phylum/server/internal/command/common" "github.com/shroff/phylum/server/internal/core/storage" "github.com/sirupsen/logrus" "github.com/spf13/cobra" @@ -59,7 +58,7 @@ func setupStorageCreateCommand() *cobra.Command { params[paramName] = val } - if err := common.Storage().CreateBackend(context.Background(), name, driver, params); err != nil { + if err := storage.Get().CreateBackend(context.Background(), name, driver, params); err != nil { logrus.Fatal(err) } @@ -74,7 +73,7 @@ func setupStorageListCommand() *cobra.Command { Short: "List all storage backends", Run: func(cmd *cobra.Command, args []string) { logrus.Info("Available storage backends:") - for k, v := range common.Storage().ListBackends() { + for k, v := range storage.Get().ListBackends() { logrus.Info(fmt.Sprintf("%s: %s", k, v)) } }, diff --git a/server/internal/command/user/add.go b/server/internal/command/user/add.go index ea7993a4..f1f540a6 100644 --- a/server/internal/command/user/add.go +++ b/server/internal/command/user/add.go @@ -95,7 +95,7 @@ func setupUserAddCommand() *cobra.Command { fmt.Println(homePath) ctx := context.Background() - err = common.App().RunInTx(ctx, func(db *db.DbHandler) error { + err = db.Get().WithTx(ctx, func(db *db.DbHandler) error { userManager := user.CreateManager(ctx).WithDb(db) var u user.User if user, err := userManager.CreateUser(username, displayName, password, root); err != nil { diff --git a/server/internal/core/app/app.go b/server/internal/core/app/app.go index fde115fc..32229620 100644 --- a/server/internal/core/app/app.go +++ b/server/internal/core/app/app.go @@ -7,24 +7,19 @@ import ( "github.com/google/uuid" "github.com/shroff/phylum/server/internal/core/db" "github.com/shroff/phylum/server/internal/core/fs" - "github.com/shroff/phylum/server/internal/core/storage" "github.com/shroff/phylum/server/internal/core/user" ) type App interface { - RunInTx(ctx context.Context, fn func(*db.DbHandler) error) error - OpenFileSystem(ctx context.Context, u user.User) fs.FileSystem } type app struct { db *db.DbHandler - cs storage.Storage } -func Create(ctx context.Context, db *db.DbHandler, cs storage.Storage) (App, error) { +func Create(ctx context.Context, db *db.DbHandler) (App, error) { app := app{ db: db, - cs: cs, } if _, err := user.CreateManager(ctx).UserByEmail(user.DefaultUserUsername); err != nil { if errors.Is(err, user.ErrNotFound) { @@ -38,14 +33,6 @@ func Create(ctx context.Context, db *db.DbHandler, cs storage.Storage) (App, err return app, nil } -func (a app) RunInTx(ctx context.Context, fn func(*db.DbHandler) error) error { - return a.db.WithTx(ctx, fn) -} - -func (a app) OpenFileSystem(ctx context.Context, u user.User) fs.FileSystem { - return fs.OpenFileSystem(a.db, ctx, a.cs, u.Username, u.Root) -} - func (a app) populateData(ctx context.Context) (u user.User, e error) { e = a.db.WithTx(ctx, func(dbh *db.DbHandler) error { var err error diff --git a/server/internal/core/fs/filesystem.go b/server/internal/core/fs/filesystem.go index 12ba8dab..9c388765 100644 --- a/server/internal/core/fs/filesystem.go +++ b/server/internal/core/fs/filesystem.go @@ -9,8 +9,8 @@ import ( ) type filesystem struct { - db *db.DbHandler ctx context.Context + db *db.DbHandler cs storage.Storage rootID uuid.UUID username string @@ -26,8 +26,8 @@ func (f filesystem) WithRoot(id uuid.UUID) FileSystem { func (f filesystem) withRoot(id uuid.UUID) filesystem { return filesystem{ - db: f.db, ctx: f.ctx, + db: f.db, cs: f.cs, rootID: id, username: f.username, @@ -40,8 +40,8 @@ func (f filesystem) WithDb(db *db.DbHandler) FileSystem { func (f filesystem) withDb(db *db.DbHandler) filesystem { return filesystem{ - db: db, ctx: f.ctx, + db: db, cs: f.cs, rootID: f.rootID, username: f.username, diff --git a/server/internal/core/fs/fs.go b/server/internal/core/fs/fs.go index f98e275f..d8f0406c 100644 --- a/server/internal/core/fs/fs.go +++ b/server/internal/core/fs/fs.go @@ -8,6 +8,7 @@ import ( "github.com/google/uuid" "github.com/shroff/phylum/server/internal/core/db" "github.com/shroff/phylum/server/internal/core/storage" + "github.com/shroff/phylum/server/internal/core/user" ) type FileSystem interface { @@ -48,13 +49,13 @@ type FileSystem interface { DiskUsage(r Resource) (DiskUsageInfo, error) } -func OpenFileSystem(dbh *db.DbHandler, ctx context.Context, cs storage.Storage, username string, rootID uuid.UUID) FileSystem { +func Open(ctx context.Context, u user.User) FileSystem { return filesystem{ - db: dbh, ctx: ctx, - cs: cs, - rootID: rootID, - username: username, + db: db.Get(), + cs: storage.Get(), + rootID: u.Root, + username: u.Username, } } diff --git a/server/internal/core/user/user.go b/server/internal/core/user/user.go index 96ebe4cd..5ecbfa2a 100644 --- a/server/internal/core/user/user.go +++ b/server/internal/core/user/user.go @@ -3,7 +3,6 @@ package user import ( "github.com/google/uuid" "github.com/shroff/phylum/server/internal/core/db" - "github.com/shroff/phylum/server/internal/core/fs" ) const DefaultUserUsername = "phylum" @@ -36,7 +35,4 @@ type Manager interface { VerifyUserPassword(email, password string) (User, error) CreateAccessToken(username string) (string, error) ReadAccessToken(accessToken string) (User, error) - - // user_lists.go - GetSharedResources(u User) (result []fs.Resource, err error) } diff --git a/server/internal/core/user/user_lists.go b/server/internal/core/user/user_lists.go index 8db6a3c7..70f1cb2b 100644 --- a/server/internal/core/user/user_lists.go +++ b/server/internal/core/user/user_lists.go @@ -1,42 +1,35 @@ package user -import ( - "time" +// func (m manager) GetSharedResources(u User) (result []fs.Resource, err error) { +// // TODO: This doesn't take permissions into account. is this okay? +// res, err := m.db.Queries.SharedResources(m.ctx, db.SharedResourcesParams{Username: u.Username, UserHome: u.Home}) +// if err != nil { +// return +// } - "github.com/shroff/phylum/server/internal/core/db" - "github.com/shroff/phylum/server/internal/core/fs" -) - -func (m manager) GetSharedResources(u User) (result []fs.Resource, err error) { - // TODO: This doesn't take permissions into account. is this okay? - res, err := m.db.Queries.SharedResources(m.ctx, db.SharedResourcesParams{Username: u.Username, UserHome: u.Home}) - if err != nil { - return - } - - result = make([]fs.Resource, len(res)) - for i, r := range res { - var deleted *time.Time - if r.Deleted.Valid { - deleted = &r.Deleted.Time - } - result[i] = fs.Resource{ - ID: r.ID, - ParentID: r.Parent, - Name: r.Name, - Dir: r.Dir, - Created: r.Created.Time, - Modified: r.Modified.Time, - Deleted: deleted, - ContentSize: r.ContentSize, - ContentType: r.ContentType, - ContentSHA256: r.ContentSha256, - Permissions: string(r.Permissions), - // Not Needed - // Path: "", - // UserPermissions: 0, - // InheritedPermissions: "", - } - } - return -} +// result = make([]fs.Resource, len(res)) +// for i, r := range res { +// var deleted *time.Time +// if r.Deleted.Valid { +// deleted = &r.Deleted.Time +// } +// result[i] = fs.Resource{ +// ID: r.ID, +// ParentID: r.Parent, +// Name: r.Name, +// Dir: r.Dir, +// Created: r.Created.Time, +// Modified: r.Modified.Time, +// Deleted: deleted, +// ContentSize: r.ContentSize, +// ContentType: r.ContentType, +// ContentSHA256: r.ContentSha256, +// Permissions: string(r.Permissions), +// // Not Needed +// // Path: "", +// // UserPermissions: 0, +// // InheritedPermissions: "", +// } +// } +// return +// }