2024-12-05 20:36:58 -05:00
|
|
|
// Package gateway provides the default routes for the Sonr hway.
|
2024-12-02 14:27:18 -05:00
|
|
|
package gateway
|
|
|
|
|
|
|
|
|
|
import (
|
2024-12-13 15:10:27 -05:00
|
|
|
"os"
|
|
|
|
|
"path/filepath"
|
|
|
|
|
"strings"
|
|
|
|
|
|
2024-12-02 14:27:18 -05:00
|
|
|
"github.com/labstack/echo/v4"
|
2024-12-10 13:40:41 -05:00
|
|
|
"github.com/onsonr/sonr/internal/gateway/context"
|
2024-12-13 15:10:27 -05:00
|
|
|
"github.com/onsonr/sonr/internal/gateway/handlers"
|
|
|
|
|
"github.com/onsonr/sonr/internal/gateway/models"
|
2024-12-05 20:36:58 -05:00
|
|
|
"github.com/onsonr/sonr/pkg/common/response"
|
2024-12-11 12:24:04 -05:00
|
|
|
config "github.com/onsonr/sonr/pkg/config/hway"
|
2024-12-13 15:10:27 -05:00
|
|
|
"gorm.io/driver/postgres"
|
|
|
|
|
"gorm.io/driver/sqlite"
|
2024-12-10 13:40:41 -05:00
|
|
|
"gorm.io/gorm"
|
2024-12-02 14:27:18 -05:00
|
|
|
)
|
|
|
|
|
|
2024-12-11 12:24:04 -05:00
|
|
|
func RegisterRoutes(e *echo.Echo, env config.Hway, db *gorm.DB) error {
|
2024-12-02 14:27:18 -05:00
|
|
|
// Custom error handler for gateway
|
2024-12-05 20:36:58 -05:00
|
|
|
e.HTTPErrorHandler = response.RedirectOnError("http://localhost:3000")
|
|
|
|
|
|
2024-12-06 21:31:20 -05:00
|
|
|
// Inject session middleware with database connection
|
2024-12-10 13:40:41 -05:00
|
|
|
e.Use(context.Middleware(db, env))
|
2024-12-06 21:31:20 -05:00
|
|
|
|
2024-12-13 15:10:27 -05:00
|
|
|
// Register View Handlers
|
|
|
|
|
e.GET("/", handlers.RenderIndex)
|
|
|
|
|
e.GET("/register", handlers.RenderProfileCreate)
|
|
|
|
|
e.POST("/register/passkey", handlers.RenderPasskeyCreate)
|
|
|
|
|
e.POST("/register/loading", handlers.RenderVaultLoading)
|
|
|
|
|
|
|
|
|
|
// Register Validation Handlers
|
|
|
|
|
e.PUT("/register/profile/submit", handlers.ValidateProfileSubmit)
|
|
|
|
|
e.PUT("/register/passkey/submit", handlers.ValidateCredentialSubmit)
|
2024-12-06 21:31:20 -05:00
|
|
|
return nil
|
2024-12-02 14:27:18 -05:00
|
|
|
}
|
2024-12-13 15:10:27 -05:00
|
|
|
|
|
|
|
|
// NewGormDB initializes and returns a configured database connection
|
|
|
|
|
func NewDB(env config.Hway) (*gorm.DB, error) {
|
|
|
|
|
// Try PostgreSQL first if DSN is provided
|
|
|
|
|
if dsn := env.GetPsqlDSN(); dsn != "" && !strings.Contains(dsn, "password= ") {
|
|
|
|
|
db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{})
|
|
|
|
|
if err == nil {
|
|
|
|
|
// Test the connection
|
|
|
|
|
sqlDB, err := db.DB()
|
|
|
|
|
if err == nil {
|
|
|
|
|
if err = sqlDB.Ping(); err == nil {
|
|
|
|
|
// Successfully connected to PostgreSQL
|
|
|
|
|
db.AutoMigrate(&models.Credential{})
|
|
|
|
|
db.AutoMigrate(&models.Session{})
|
|
|
|
|
db.AutoMigrate(&models.User{})
|
|
|
|
|
return db, nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Fall back to SQLite
|
|
|
|
|
path := formatDBPath(env.GetSqliteFile())
|
|
|
|
|
db, err := gorm.Open(sqlite.Open(path), &gorm.Config{})
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Migrate the schema
|
|
|
|
|
db.AutoMigrate(&models.Credential{})
|
|
|
|
|
db.AutoMigrate(&models.Session{})
|
|
|
|
|
db.AutoMigrate(&models.User{})
|
|
|
|
|
return db, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func formatDBPath(fileName string) string {
|
|
|
|
|
configDir := filepath.Join(os.Getenv("XDG_CONFIG_HOME"), "hway")
|
|
|
|
|
if err := os.MkdirAll(configDir, 0o755); err != nil {
|
|
|
|
|
// If we can't create the directory, fall back to current directory
|
|
|
|
|
return configDir
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return filepath.Join(configDir, fileName)
|
|
|
|
|
}
|