mirror of
https://codeberg.org/shroff/phylum.git
synced 2026-01-04 02:31:14 -06:00
[server][core] Nearly remove all functionality from app.App
This commit is contained in:
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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{
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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))
|
||||
}
|
||||
},
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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
|
||||
// }
|
||||
|
||||
Reference in New Issue
Block a user