refactor: move session package to pkg directory

This commit is contained in:
Prad Nukala
2024-12-11 12:07:39 -05:00
parent 0e0a46741f
commit 40f50bf37a
15 changed files with 16 additions and 18 deletions
-42
View File
@@ -1,42 +0,0 @@
package sessions
import (
"net/http"
"github.com/labstack/echo/v4"
"gorm.io/gorm"
)
var (
ErrInvalidCredentials = echo.NewHTTPError(http.StatusUnauthorized, "Invalid credentials")
ErrInvalidSubject = echo.NewHTTPError(http.StatusBadRequest, "Invalid subject")
ErrInvalidUser = echo.NewHTTPError(http.StatusBadRequest, "Invalid user")
ErrUserAlreadyExists = echo.NewHTTPError(http.StatusConflict, "User already exists")
ErrUserNotFound = echo.NewHTTPError(http.StatusNotFound, "User not found")
)
type User struct {
gorm.Model
Address string `json:"address"`
Handle string `json:"handle"`
Name string `json:"name"`
CID string `json:"cid"`
}
type Session struct {
gorm.Model
ID string `json:"id" gorm:"primaryKey"`
BrowserName string `json:"browserName"`
BrowserVersion string `json:"browserVersion"`
UserArchitecture string `json:"userArchitecture"`
Platform string `json:"platform"`
PlatformVersion string `json:"platformVersion"`
DeviceModel string `json:"deviceModel"`
UserHandle string `json:"userHandle"`
FirstName string `json:"firstName"`
LastInitial string `json:"lastInitial"`
VaultAddress string `json:"vaultAddress"`
HumanSum int `json:"humanSum"`
Challenge string `json:"challenge"`
}
-42
View File
@@ -1,42 +0,0 @@
package sessions
import (
"os"
"path/filepath"
"github.com/onsonr/sonr/internal/gateway/config"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
)
// NewGormDB initializes and returns a configured database connection
func NewGormDB(env config.Env) (*gorm.DB, error) {
path := formatDBPath(env.GetSqliteFile())
db, err := gorm.Open(sqlite.Open(path), &gorm.Config{})
if err != nil {
return nil, err
}
// Migrate the schema
db.AutoMigrate(&Session{})
db.AutoMigrate(&User{})
return db, nil
}
func formatDBPath(path string) string {
home := os.Getenv("HOME")
if home == "" {
home = os.Getenv("USERPROFILE")
}
if home == "" {
home = "."
}
configDir := filepath.Join(home, ".config", "hway")
if err := os.MkdirAll(configDir, 0o755); err != nil {
// If we can't create the directory, fall back to current directory
return path
}
return filepath.Join(configDir, path)
}
+1 -1
View File
@@ -5,8 +5,8 @@ import (
"strings"
"github.com/labstack/echo/v4"
"github.com/onsonr/sonr/internal/database/sessions"
"github.com/onsonr/sonr/pkg/common"
"github.com/onsonr/sonr/pkg/database/sessions"
"github.com/segmentio/ksuid"
)
+1 -1
View File
@@ -4,8 +4,8 @@ import (
"net/http"
"github.com/labstack/echo/v4"
"github.com/onsonr/sonr/internal/database/sessions"
"github.com/onsonr/sonr/internal/gateway/config"
"github.com/onsonr/sonr/pkg/database/sessions"
"gorm.io/gorm"
)
+1 -1
View File
@@ -2,7 +2,7 @@ package context
import (
"github.com/labstack/echo/v4"
"github.com/onsonr/sonr/internal/database/sessions"
"github.com/onsonr/sonr/pkg/database/sessions"
)
// ╭───────────────────────────────────────────────────────╮
@@ -1,5 +1,5 @@
// Code generated from Pkl module `sonr.motr.DWN`. DO NOT EDIT.
package types
package config
type Config struct {
IpfsGatewayUrl string `pkl:"ipfsGatewayUrl" json:"ipfsGatewayUrl,omitempty"`
@@ -1,5 +1,5 @@
// Code generated from Pkl module `sonr.motr.DWN`. DO NOT EDIT.
package types
package config
import (
"context"
@@ -1,5 +1,5 @@
// Code generated from Pkl module `sonr.motr.DWN`. DO NOT EDIT.
package types
package config
type Environment struct {
IsDevelopment bool `pkl:"isDevelopment" json:"isDevelopment,omitempty"`
@@ -1,5 +1,5 @@
// Code generated from Pkl module `sonr.motr.DWN`. DO NOT EDIT.
package types
package config
type Schema struct {
Version int `pkl:"version"`
@@ -5,7 +5,6 @@ import (
"github.com/ipfs/boxo/files"
"github.com/onsonr/sonr/internal/vault/embed"
"github.com/onsonr/sonr/internal/vault/types"
)
const SchemaVersion = 1
@@ -18,7 +17,7 @@ const (
)
// spawnVaultDirectory creates a new directory with the default files
func NewFS(cfg *types.Config) (files.Directory, error) {
func NewVaultFS(cfg *Config) (files.Directory, error) {
manifestBz, err := newWebManifestBytes()
if err != nil {
return nil, err
@@ -36,9 +35,9 @@ func NewFS(cfg *types.Config) (files.Directory, error) {
}), nil
}
// GetVaultConfig returns the default vault config
func GetVaultConfig(addr string, ucanCID string) *types.Config {
return &types.Config{
// NewVaultConfig returns the default vault config
func NewVaultConfig(addr string, ucanCID string) *Config {
return &Config{
MotrToken: ucanCID,
MotrAddress: addr,
IpfsGatewayUrl: "http://localhost:80",
@@ -1,5 +1,5 @@
// Code generated from Pkl module `sonr.motr.DWN`. DO NOT EDIT.
package types
package config
import "github.com/apple/pkl-go/pkl"
@@ -4,13 +4,12 @@ import (
"reflect"
"strings"
"github.com/onsonr/sonr/internal/vault/types"
"github.com/onsonr/sonr/pkg/common/models"
)
// DefaultSchema returns the default schema
func DefaultSchema() *types.Schema {
return &types.Schema{
func DefaultSchema() *Schema {
return &Schema{
Version: SchemaVersion,
Account: getSchema(&models.Account{}),
Asset: getSchema(&models.Asset{}),