mirror of
https://codeberg.org/shroff/phylum.git
synced 2026-05-12 15:18:38 -05:00
Create internal/app package for easy access to globals
This commit is contained in:
@@ -4,22 +4,14 @@ import (
|
||||
"os"
|
||||
"path"
|
||||
|
||||
"github.com/shroff/phylum/server/internal/auth"
|
||||
"github.com/shroff/phylum/server/internal/core"
|
||||
"github.com/shroff/phylum/server/internal/app"
|
||||
"github.com/shroff/phylum/server/internal/sql"
|
||||
"github.com/shroff/phylum/server/internal/storage"
|
||||
"github.com/shroff/phylum/server/internal/user"
|
||||
"github.com/sirupsen/logrus"
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
var debug bool = false
|
||||
var fs *core.FileSystem
|
||||
var db *sql.DbHandler
|
||||
var userManager *user.Manager
|
||||
var storageManager *storage.Manager
|
||||
var sessionManager *auth.Manager
|
||||
|
||||
func SetupCommand() {
|
||||
viper.SetEnvPrefix("phylum")
|
||||
@@ -55,34 +47,18 @@ func SetupCommand() {
|
||||
logrus.SetLevel(logrus.TraceLevel)
|
||||
}
|
||||
|
||||
dsn := viper.GetString("database_url")
|
||||
|
||||
var err error
|
||||
db, err = sql.NewDb(dsn, debug && viper.GetBool("trace_sql"), viper.GetBool("auto_migrate"))
|
||||
if err != nil {
|
||||
if db, err := sql.NewDb(viper.GetString("database_url"), debug && viper.GetBool("trace_sql"), viper.GetBool("auto_migrate")); err != nil {
|
||||
logrus.Fatal(err)
|
||||
} else if err := app.Initialize(db); err != nil {
|
||||
logrus.Fatal(err)
|
||||
}
|
||||
|
||||
userManager, err = user.NewManager(db)
|
||||
if err != nil {
|
||||
logrus.Fatal(err)
|
||||
}
|
||||
sessionManager, err = auth.NewManager(db, userManager)
|
||||
if err != nil {
|
||||
logrus.Fatal(err)
|
||||
}
|
||||
|
||||
storageManager, err = storage.NewManager(db)
|
||||
if err != nil {
|
||||
logrus.Fatal(err)
|
||||
}
|
||||
fs = core.OpenFileSystem(db, storageManager)
|
||||
}
|
||||
|
||||
defer func() {
|
||||
if db != nil {
|
||||
if app.Db != nil {
|
||||
logrus.Info("Closing datbase connection")
|
||||
db.Close()
|
||||
app.Db.Close()
|
||||
}
|
||||
}()
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@ package command
|
||||
import (
|
||||
"strconv"
|
||||
|
||||
"github.com/shroff/phylum/server/internal/app"
|
||||
"github.com/sirupsen/logrus"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
@@ -28,7 +29,7 @@ func setupSchemaMigrateCommand() *cobra.Command {
|
||||
if err != nil {
|
||||
logrus.Fatal(err)
|
||||
}
|
||||
if err = db.Migrate(v); err != nil {
|
||||
if err = app.Db.Migrate(v); err != nil {
|
||||
logrus.Fatal(err)
|
||||
}
|
||||
},
|
||||
|
||||
@@ -7,6 +7,8 @@ import (
|
||||
"github.com/fvbock/endless"
|
||||
"github.com/gin-contrib/cors"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/shroff/phylum/server/internal/api"
|
||||
"github.com/shroff/phylum/server/internal/app"
|
||||
webdav "github.com/shroff/phylum/server/internal/handler_webdav"
|
||||
"github.com/sirupsen/logrus"
|
||||
"github.com/spf13/cobra"
|
||||
@@ -21,7 +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")), fs, userManager)
|
||||
webdav.SetupHandler(engine.Group(config.GetString("webdav_prefix")), app.Fs, app.UserManager)
|
||||
|
||||
api.Setup(engine.Group("/api/v1"))
|
||||
|
||||
server := endless.NewServer(config.GetString("listen"), engine)
|
||||
server.BeforeBegin = func(addr string) {
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/shroff/phylum/server/internal/app"
|
||||
"github.com/sirupsen/logrus"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
@@ -30,19 +31,19 @@ func setupSiloCreateCommand() *cobra.Command {
|
||||
id := uuid.New()
|
||||
|
||||
username := args[0]
|
||||
user, err := userManager.FindUser(context.Background(), username)
|
||||
user, err := app.UserManager.FindUser(context.Background(), username)
|
||||
if err != nil {
|
||||
logrus.Fatal("User not found: " + username)
|
||||
}
|
||||
|
||||
storageName := args[1]
|
||||
storage := storageManager.Find(storageName)
|
||||
storage := app.StorageManager.Find(storageName)
|
||||
if storage == nil {
|
||||
logrus.Fatal("Storage not found: " + storageName)
|
||||
}
|
||||
|
||||
name := args[2]
|
||||
if err := fs.CreateSilo(context.Background(), id, user.Username, storageName, name); err != nil {
|
||||
if err := app.Fs.CreateSilo(context.Background(), id, user.Username, storageName, name); err != nil {
|
||||
logrus.Fatal(err)
|
||||
}
|
||||
logrus.Info("Created " + id.String())
|
||||
@@ -57,7 +58,7 @@ func setupSiloListCommand() *cobra.Command {
|
||||
Args: cobra.ExactArgs(0),
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
ctx := context.Background()
|
||||
silos, err := fs.ListSilos(ctx)
|
||||
silos, err := app.Fs.ListSilos(ctx)
|
||||
if err != nil {
|
||||
logrus.Fatal(err)
|
||||
}
|
||||
@@ -81,7 +82,7 @@ func setupSiloDeleteCommand() *cobra.Command {
|
||||
if err != nil {
|
||||
logrus.Fatal("Not an ID: " + args[0])
|
||||
}
|
||||
if err := fs.DeleteSilo(context.Background(), id); err != nil {
|
||||
if err := app.Fs.DeleteSilo(context.Background(), id); err != nil {
|
||||
logrus.Fatal(err)
|
||||
}
|
||||
},
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/shroff/phylum/server/internal/app"
|
||||
"github.com/shroff/phylum/server/internal/storage"
|
||||
"github.com/sirupsen/logrus"
|
||||
"github.com/spf13/cobra"
|
||||
@@ -30,7 +31,7 @@ func setupStorageCreateCommand() *cobra.Command {
|
||||
Args: cobra.ExactArgs(2),
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
name := args[0]
|
||||
if storageManager.Find(name) != nil {
|
||||
if app.StorageManager.Find(name) != nil {
|
||||
logrus.Fatal("Storage backand already exists: " + name)
|
||||
}
|
||||
|
||||
@@ -52,7 +53,7 @@ func setupStorageCreateCommand() *cobra.Command {
|
||||
params[paramName] = val
|
||||
}
|
||||
|
||||
if err := storageManager.Create(name, driver, params); err != nil {
|
||||
if err := app.StorageManager.Create(name, driver, params); err != nil {
|
||||
logrus.Fatal(err)
|
||||
}
|
||||
|
||||
@@ -67,7 +68,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 storageManager.All() {
|
||||
for k, v := range app.StorageManager.All() {
|
||||
logrus.Info(fmt.Sprintf("%s: %s", k, v))
|
||||
}
|
||||
},
|
||||
|
||||
@@ -8,7 +8,7 @@ import (
|
||||
"strings"
|
||||
"syscall"
|
||||
|
||||
"github.com/shroff/phylum/server/internal/cryptutil"
|
||||
"github.com/shroff/phylum/server/internal/app"
|
||||
"github.com/sirupsen/logrus"
|
||||
"github.com/spf13/cobra"
|
||||
"golang.org/x/term"
|
||||
@@ -68,12 +68,9 @@ func setupUserCreateCommand() *cobra.Command {
|
||||
logrus.Fatal("Password does not match confirmation")
|
||||
}
|
||||
|
||||
hash, err := cryptutil.GenerateArgon2EncodedHash(password, cryptutil.DefaultArgon2Params())
|
||||
if err != nil {
|
||||
if err := app.UserManager.CreateUser(context.Background(), username, displayName, password); err != nil {
|
||||
logrus.Fatal(err)
|
||||
}
|
||||
|
||||
userManager.CreateUser(context.Background(), username, displayName, hash)
|
||||
},
|
||||
}
|
||||
return cmd
|
||||
@@ -84,7 +81,7 @@ func setupUserListCommand() *cobra.Command {
|
||||
Use: "list",
|
||||
Short: "List Users",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
users, err := userManager.ListUsers(context.Background())
|
||||
users, err := app.UserManager.ListUsers(context.Background())
|
||||
if err != nil {
|
||||
logrus.Fatal(err)
|
||||
}
|
||||
@@ -111,7 +108,7 @@ func setupUserLoginCommand() *cobra.Command {
|
||||
}
|
||||
password := string(bytes)
|
||||
|
||||
accessToken, err := sessionManager.CreateAccessToken(username, password, "cmd")
|
||||
accessToken, err := app.AuthManager.CreateAccessToken(username, password, "cmd")
|
||||
if err != nil {
|
||||
logrus.Fatal(err)
|
||||
}
|
||||
@@ -128,7 +125,7 @@ func setupUserVerifyTokenCommand() *cobra.Command {
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
token := strings.TrimSpace(args[0])
|
||||
|
||||
session, err := sessionManager.VerifyAccessToken(token)
|
||||
session, err := app.AuthManager.VerifyAccessToken(token)
|
||||
if err != nil {
|
||||
logrus.Fatal(err)
|
||||
}
|
||||
|
||||
@@ -9,13 +9,12 @@ import (
|
||||
"context"
|
||||
)
|
||||
|
||||
const createUser = `-- name: CreateUser :one
|
||||
const createUser = `-- name: CreateUser :exec
|
||||
INSERT INTO users(
|
||||
username, display_name, password_hash
|
||||
) VALUES (
|
||||
$1, $2, $3
|
||||
)
|
||||
RETURNING username, display_name, password_hash, deleted
|
||||
`
|
||||
|
||||
type CreateUserParams struct {
|
||||
@@ -24,16 +23,9 @@ type CreateUserParams struct {
|
||||
PasswordHash string
|
||||
}
|
||||
|
||||
func (q *Queries) CreateUser(ctx context.Context, arg CreateUserParams) (User, error) {
|
||||
row := q.db.QueryRow(ctx, createUser, arg.Username, arg.DisplayName, arg.PasswordHash)
|
||||
var i User
|
||||
err := row.Scan(
|
||||
&i.Username,
|
||||
&i.DisplayName,
|
||||
&i.PasswordHash,
|
||||
&i.Deleted,
|
||||
)
|
||||
return i, err
|
||||
func (q *Queries) CreateUser(ctx context.Context, arg CreateUserParams) error {
|
||||
_, err := q.db.Exec(ctx, createUser, arg.Username, arg.DisplayName, arg.PasswordHash)
|
||||
return err
|
||||
}
|
||||
|
||||
const listUsers = `-- name: ListUsers :many
|
||||
|
||||
@@ -3,6 +3,7 @@ package user
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/shroff/phylum/server/internal/cryptutil"
|
||||
"github.com/shroff/phylum/server/internal/sql"
|
||||
)
|
||||
|
||||
@@ -14,9 +15,10 @@ func NewManager(db *sql.DbHandler) (*Manager, error) {
|
||||
return &Manager{db: db}, nil
|
||||
}
|
||||
|
||||
func (m Manager) CreateUser(ctx context.Context, username, displayName, passwordHash string) error {
|
||||
_, err := m.db.Queries().CreateUser(ctx, sql.CreateUserParams{Username: username, DisplayName: displayName, PasswordHash: passwordHash})
|
||||
if err != nil {
|
||||
func (m Manager) CreateUser(ctx context.Context, username, displayName, password string) error {
|
||||
if hash, err := cryptutil.GenerateArgon2EncodedHash(password, cryptutil.DefaultArgon2Params()); err != nil {
|
||||
return err
|
||||
} else if err = m.db.Queries().CreateUser(ctx, sql.CreateUserParams{Username: username, DisplayName: displayName, PasswordHash: hash}); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
|
||||
@@ -1,10 +1,9 @@
|
||||
-- name: CreateUser :one
|
||||
-- name: CreateUser :exec
|
||||
INSERT INTO users(
|
||||
username, display_name, password_hash
|
||||
) VALUES (
|
||||
$1, $2, $3
|
||||
)
|
||||
RETURNING *;
|
||||
);
|
||||
|
||||
-- name: UserByUsername :one
|
||||
SELECT * from users WHERE username = $1;
|
||||
|
||||
Reference in New Issue
Block a user