Merge app and core packages

This commit is contained in:
Abhishek Shroff
2024-08-07 21:23:58 +05:30
parent dc5268c9dc
commit 7bee423382
21 changed files with 50 additions and 73 deletions
+2 -2
View File
@@ -5,10 +5,10 @@ import (
"github.com/shroff/phylum/server/internal/api/auth"
"github.com/shroff/phylum/server/internal/api/errors"
"github.com/shroff/phylum/server/internal/api/routes"
"github.com/shroff/phylum/server/internal/app"
"github.com/shroff/phylum/server/internal/core"
)
func Setup(r *gin.RouterGroup, a *app.App) {
func Setup(r *gin.RouterGroup, a *core.App) {
r.Use(handleApiError)
// Unauthenticated routes
+1 -1
View File
@@ -2,7 +2,7 @@ package auth
import (
"github.com/gin-gonic/gin"
"github.com/shroff/phylum/server/internal/app/core"
"github.com/shroff/phylum/server/internal/core"
)
const keyFileSystem = "filesystem"
+3 -3
View File
@@ -6,14 +6,14 @@ import (
"github.com/gin-gonic/gin"
"github.com/shroff/phylum/server/internal/api/errors"
"github.com/shroff/phylum/server/internal/app"
"github.com/shroff/phylum/server/internal/core"
"github.com/sirupsen/logrus"
)
const errCodeAuthRequred = "auth_required"
const errCodeTokenInvalid = "token_invalid"
func CreateBearerAuthHandler(a *app.App) func(c *gin.Context) {
func CreateBearerAuthHandler(a *core.App) func(c *gin.Context) {
return func(c *gin.Context) {
authHeader := c.GetHeader("Authorization")
if authHeader == "" {
@@ -29,7 +29,7 @@ func CreateBearerAuthHandler(a *app.App) func(c *gin.Context) {
userID, err := a.VerifyAccessToken(authParts[1])
if err != nil {
if errors.Is(err, app.ErrTokenExpired) || errors.Is(err, app.ErrTokenInvalid) {
if errors.Is(err, core.ErrTokenExpired) || errors.Is(err, core.ErrTokenInvalid) {
panic(errors.Err{Status: 401, Code: errCodeTokenInvalid})
}
panic(err)
+4 -4
View File
@@ -5,15 +5,15 @@ import (
"github.com/gin-gonic/gin"
"github.com/shroff/phylum/server/internal/api/errors"
"github.com/shroff/phylum/server/internal/app"
"github.com/shroff/phylum/server/internal/core"
)
func SetupAuthRoutes(r *gin.RouterGroup, a *app.App) {
func SetupAuthRoutes(r *gin.RouterGroup, a *core.App) {
group := r.Group("/auth")
group.POST("/login", createLoginRouteHandler(a))
}
func createLoginRouteHandler(a *app.App) func(c *gin.Context) {
func createLoginRouteHandler(a *core.App) func(c *gin.Context) {
return func(c *gin.Context) {
username, ok := c.GetQuery("username")
if !ok {
@@ -25,7 +25,7 @@ func createLoginRouteHandler(a *app.App) func(c *gin.Context) {
}
if token, err := a.CreateAccessToken(username, password); err != nil {
if errors.Is(err, app.ErrCredentialsInvalid) {
if errors.Is(err, core.ErrCredentialsInvalid) {
panic(errors.New(http.StatusUnauthorized, "credentials_invalid", ""))
}
panic(err)
-7
View File
@@ -1,7 +0,0 @@
package app
import (
"errors"
)
var ErrResourceNotFound = errors.New("resource not found")
+2 -2
View File
@@ -1,7 +1,7 @@
package appcmd
import (
"github.com/shroff/phylum/server/internal/app"
"github.com/shroff/phylum/server/internal/core"
"github.com/shroff/phylum/server/internal/db"
"github.com/shroff/phylum/server/internal/storage"
"github.com/sirupsen/logrus"
@@ -27,7 +27,7 @@ func SetupCommand(db **db.DbHandler, debug bool) *cobra.Command {
if cs, err = storage.Open(*db, viper.GetString("content-dir")); err != nil {
logrus.Fatal(err)
} else {
if err := app.Create(*db, cs, debug); err != nil {
if err := core.Create(*db, cs, debug); err != nil {
logrus.Fatal(err)
}
}
+6 -7
View File
@@ -8,8 +8,7 @@ import (
"strings"
"github.com/google/uuid"
"github.com/shroff/phylum/server/internal/app"
"github.com/shroff/phylum/server/internal/app/core"
"github.com/shroff/phylum/server/internal/core"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
@@ -105,7 +104,7 @@ func setupResourceRmCommand() *cobra.Command {
func setupResourceLsCommand() *cobra.Command {
details := func(r core.Resource, name string) string {
if owner, err := app.Default.UserByID(context.Background(), r.Owner()); err != nil {
if owner, err := core.Default.UserByID(context.Background(), r.Owner()); err != nil {
logrus.Fatal(err)
return ""
} else {
@@ -178,7 +177,7 @@ func setupResourceChownCommand() *cobra.Command {
username := args[0]
path := args[1]
var owner int32
if user, err := app.Default.UserByUsername(context.Background(), username); err != nil {
if user, err := core.Default.UserByUsername(context.Background(), username); err != nil {
logrus.Fatal(err)
} else {
owner = user.ID()
@@ -237,7 +236,7 @@ func openFileSystemFromFlags(cmd *cobra.Command) {
if value, err := cmd.Flags().GetInt32("user"); err != nil {
logrus.Fatal(err)
} else if value != 0 {
if user, err := app.Default.UserByID(context.Background(), value); err != nil {
if user, err := core.Default.UserByID(context.Background(), value); err != nil {
logrus.Fatal(err)
} else {
user = user
@@ -248,7 +247,7 @@ func openFileSystemFromFlags(cmd *cobra.Command) {
if value, err := cmd.Flags().GetString("username"); err != nil {
logrus.Fatal(err)
} else {
if user, err := app.Default.UserByUsername(context.Background(), value); err != nil {
if user, err := core.Default.UserByUsername(context.Background(), value); err != nil {
logrus.Fatal(err)
} else {
user = user
@@ -256,7 +255,7 @@ func openFileSystemFromFlags(cmd *cobra.Command) {
}
var err error
fs, err = app.Default.OpenFileSystem(context.Background(), user)
fs, err = core.Default.OpenFileSystem(context.Background(), user)
if err != nil {
logrus.Fatal(err)
}
+5 -5
View File
@@ -8,7 +8,7 @@ import (
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
"github.com/shroff/phylum/server/internal/api"
"github.com/shroff/phylum/server/internal/app"
"github.com/shroff/phylum/server/internal/core"
"github.com/shroff/phylum/server/internal/webdav"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
@@ -23,9 +23,9 @@ func setupServeCommand() *cobra.Command {
config := viper.GetViper()
engine := createEngine(config.GetBool("log_body"), config.GetBool("cors_enabled"), config.GetStringSlice("cors_origins"))
webdav.SetupHandler(engine.Group(config.GetString("webdav_prefix")), app.Default)
webdav.SetupHandler(engine.Group(config.GetString("webdav_prefix")), core.Default)
api.Setup(engine.Group("/api/v1"), app.Default)
api.Setup(engine.Group("/api/v1"), core.Default)
server := endless.NewServer(config.GetString("listen"), engine)
server.BeforeBegin = func(addr string) {
@@ -58,11 +58,11 @@ func setupServeCommand() *cobra.Command {
}
func createEngine(logBody bool, corsEnabled bool, corsOrigins []string) *gin.Engine {
if !app.Default.Debug {
if !core.Default.Debug {
gin.SetMode(gin.ReleaseMode)
}
engine := gin.Default()
if app.Default.Debug && logBody {
if core.Default.Debug && logBody {
engine.Use(logBodyMiddleware)
}
+4 -4
View File
@@ -6,7 +6,7 @@ import (
"strings"
"syscall"
"github.com/shroff/phylum/server/internal/app"
"github.com/shroff/phylum/server/internal/core"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"golang.org/x/term"
@@ -61,7 +61,7 @@ func setupUserAddCommand() *cobra.Command {
}
}
if err := app.Default.CreateUser(context.Background(), username, displayName, password, nil); err != nil {
if err := core.Default.CreateUser(context.Background(), username, displayName, password, nil); err != nil {
logrus.Fatal(err)
}
},
@@ -76,7 +76,7 @@ func setupUserListCommand() *cobra.Command {
Use: "list",
Short: "List Users",
Run: func(cmd *cobra.Command, args []string) {
users, err := app.Default.ListUsers(context.Background())
users, err := core.Default.ListUsers(context.Background())
if err != nil {
logrus.Fatal(err)
}
@@ -103,7 +103,7 @@ func setupUserLoginCommand() *cobra.Command {
}
password := string(bytes)
accessToken, err := app.Default.CreateAccessToken(username, password)
accessToken, err := core.Default.CreateAccessToken(username, password)
if err != nil {
logrus.Fatal(err)
}
@@ -1,4 +1,4 @@
package app
package core
import (
"context"
@@ -6,7 +6,6 @@ import (
"github.com/google/uuid"
"github.com/jackc/pgx/v5"
"github.com/shroff/phylum/server/internal/app/core"
"github.com/shroff/phylum/server/internal/db"
"github.com/shroff/phylum/server/internal/storage"
)
@@ -15,7 +14,7 @@ const defaultUserName = "phylum"
type App struct {
Debug bool
rootfs core.FileSystem
rootfs FileSystem
db *db.DbHandler
cs storage.Storage
}
@@ -29,17 +28,6 @@ func Create(db *db.DbHandler, cs storage.Storage, debug bool) error {
cs: cs,
}
// if root, err := Default.UserByUsername(context.Background(), "phylum"); err != nil {
// return err
// } else if fs, err := Default.OpenFileSystem(context.Background(), root.ID()); err != nil {
// return err
// } else {
// Default.rootfs = fs
// if _, err := fs.ResourceByPath("/home"); err != nil {
// _, err := fs.CreateMemberResource(fs.Root(), uuid.New(), "home", true)
// return err
// }
// }
return Default.setupAppData()
}
@@ -114,6 +102,6 @@ func (a App) setupAppData() error {
})
}
func (a App) OpenFileSystem(ctx context.Context, user core.User) (core.FileSystem, error) {
return core.OpenFileSystem(a.db, ctx, a.cs, user)
func (a App) OpenFileSystem(ctx context.Context, user User) (FileSystem, error) {
return OpenFileSystem(a.db, ctx, a.cs, user)
}
@@ -1,4 +1,4 @@
package app
package core
import (
"context"
@@ -8,7 +8,6 @@ import (
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgtype"
"github.com/shroff/phylum/server/internal/app/core"
"github.com/shroff/phylum/server/internal/cryptutil"
"github.com/shroff/phylum/server/internal/db"
"golang.org/x/exp/rand"
@@ -25,7 +24,7 @@ var ErrCredentialsInvalid = errors.New("credentials invalid")
var ErrTokenInvalid = errors.New("token invalid")
var ErrTokenExpired = errors.New("token expired")
func (a App) VerifyUserPassword(ctx context.Context, username, password string) (core.User, error) {
func (a App) VerifyUserPassword(ctx context.Context, username, password string) (User, error) {
if user, err := a.db.Queries().UserByUsername(context.Background(), username); err != nil {
if errors.Is(err, pgx.ErrNoRows) {
return nil, ErrCredentialsInvalid
@@ -61,7 +60,7 @@ func (a App) CreateAccessToken(username, password string) (db.AccessToken, error
}
}
func (a App) VerifyAccessToken(accessToken string) (core.User, error) {
func (a App) VerifyAccessToken(accessToken string) (User, error) {
token, err := a.db.Queries().AccessTokenById(context.Background(), accessToken)
if errors.Is(err, pgx.ErrNoRows) {
return nil, ErrTokenInvalid
@@ -1,4 +1,4 @@
package app
package core
import (
"context"
@@ -6,7 +6,6 @@ import (
"github.com/google/uuid"
"github.com/jackc/pgx/v5"
"github.com/shroff/phylum/server/internal/app/core"
"github.com/shroff/phylum/server/internal/cryptutil"
"github.com/shroff/phylum/server/internal/db"
)
@@ -39,14 +38,14 @@ func (a App) CreateUser(ctx context.Context, username, displayName, password str
}
}
func (a App) ListUsers(ctx context.Context) ([]core.User, error) {
func (a App) ListUsers(ctx context.Context) ([]User, error) {
results, err := a.db.Queries().ListUsers(ctx)
if err != nil {
return nil, err
}
users := make([]core.User, len(results))
users := make([]User, len(results))
for i, r := range results {
users[i] = core.user{
users[i] = user{
id: r.ID,
username: r.Username,
displayName: r.DisplayName,
@@ -56,7 +55,7 @@ func (a App) ListUsers(ctx context.Context) ([]core.User, error) {
return users, nil
}
func (a App) UserByUsername(ctx context.Context, username string) (core.User, error) {
func (a App) UserByUsername(ctx context.Context, username string) (User, error) {
result, err := a.db.Queries().UserByUsername(ctx, username)
if err != nil {
if errors.Is(err, pgx.ErrNoRows) {
@@ -72,7 +71,7 @@ func (a App) UserByUsername(ctx context.Context, username string) (core.User, er
}, nil
}
func (a App) UserByID(ctx context.Context, userID int32) (core.User, error) {
func (a App) UserByID(ctx context.Context, userID int32) (User, error) {
result, err := a.db.Queries().UserByID(ctx, userID)
if err != nil {
if errors.Is(err, pgx.ErrNoRows) {
+3 -3
View File
@@ -5,17 +5,17 @@ import (
"net/http"
"github.com/gin-gonic/gin"
"github.com/shroff/phylum/server/internal/app"
"github.com/shroff/phylum/server/internal/core"
"github.com/sirupsen/logrus"
)
const keyFileSystem = "filesystem"
func createBasicAuthHandler(a *app.App) func(c *gin.Context) {
func createBasicAuthHandler(a *core.App) func(c *gin.Context) {
return func(c *gin.Context) {
if username, pass, ok := c.Request.BasicAuth(); !ok {
} else if user, err := a.VerifyUserPassword(c.Request.Context(), username, pass); err != nil {
if errors.Is(err, app.ErrCredentialsInvalid) {
if errors.Is(err, core.ErrCredentialsInvalid) {
c.Header("WWW-Authenticate", "Basic realm=\"Phylum WebDAV\"")
c.AbortWithStatus(http.StatusUnauthorized)
} else {
+3 -4
View File
@@ -10,18 +10,17 @@ import (
"github.com/gin-gonic/gin"
"github.com/google/uuid"
"github.com/shroff/phylum/server/internal/app"
"github.com/shroff/phylum/server/internal/app/core"
"github.com/shroff/phylum/server/internal/core"
webdav "github.com/shroff/phylum/server/internal/webdav/impl"
"github.com/sirupsen/logrus"
)
type handler struct {
app *app.App
app *core.App
prefix string
}
func SetupHandler(r *gin.RouterGroup, app *app.App) {
func SetupHandler(r *gin.RouterGroup, app *core.App) {
logrus.Info("Setting up WebDAV access at " + r.BasePath())
handler := &handler{
app: app,
+1 -1
View File
@@ -12,7 +12,7 @@ import (
"path"
"path/filepath"
"github.com/shroff/phylum/server/internal/app/core"
"github.com/shroff/phylum/server/internal/core"
)
// A FileSystem implements access to a collection of named files. The elements
+1 -1
View File
@@ -11,7 +11,7 @@ import (
"net/http"
"strconv"
"github.com/shroff/phylum/server/internal/app/core"
"github.com/shroff/phylum/server/internal/core"
)
// Proppatch describes a property update instruction as defined in RFC 4918.
@@ -12,7 +12,7 @@ import (
"strings"
"time"
"github.com/shroff/phylum/server/internal/app/core"
"github.com/shroff/phylum/server/internal/core"
)
var htmlReplacer = strings.NewReplacer(
+1 -1
View File
@@ -17,7 +17,7 @@ import (
"strings"
"time"
"github.com/shroff/phylum/server/internal/app/core"
"github.com/shroff/phylum/server/internal/core"
)
type Handler struct {