refactor: Flatten directory structure

Move project files from uzdb/ subdirectory to root directory for cleaner project structure.

Changes:
- Move frontend/ to root
- Move internal/ to root
- Move build/ to root
- Move all config files (go.mod, wails.json, etc.) to root
- Remove redundant uzdb/ subdirectory nesting

Project structure is now:
├── frontend/        # React application
├── internal/        # Go backend
├── build/           # Wails build assets
├── doc/             # Design documentation
├── main.go          # Entry point
└── ...

🤖 Generated with Qoder
This commit is contained in:
loveuer
2026-04-04 07:14:00 -07:00
parent 5a83e86bc9
commit 9874561410
83 changed files with 0 additions and 46 deletions

View File

@@ -0,0 +1,382 @@
package services
import (
"context"
"fmt"
"time"
"go.uber.org/zap"
"gorm.io/gorm"
"uzdb/internal/config"
"uzdb/internal/database"
"uzdb/internal/models"
"uzdb/internal/utils"
)
// ConnectionService manages database connections
type ConnectionService struct {
db *gorm.DB
connManager *database.ConnectionManager
encryptSvc *EncryptionService
}
// NewConnectionService creates a new connection service
func NewConnectionService(
db *gorm.DB,
connManager *database.ConnectionManager,
encryptSvc *EncryptionService,
) *ConnectionService {
return &ConnectionService{
db: db,
connManager: connManager,
encryptSvc: encryptSvc,
}
}
// GetAllConnections returns all user connections
func (s *ConnectionService) GetAllConnections(ctx context.Context) ([]models.UserConnection, error) {
var connections []models.UserConnection
result := s.db.WithContext(ctx).Find(&connections)
if result.Error != nil {
return nil, fmt.Errorf("failed to get connections: %w", result.Error)
}
// Mask passwords in response
for i := range connections {
connections[i].Password = s.encryptSvc.MaskPasswordForLogging(connections[i].Password)
}
config.GetLogger().Debug("retrieved all connections",
zap.Int("count", len(connections)))
return connections, nil
}
// GetConnectionByID returns a connection by ID
func (s *ConnectionService) GetConnectionByID(ctx context.Context, id string) (*models.UserConnection, error) {
var conn models.UserConnection
result := s.db.WithContext(ctx).First(&conn, "id = ?", id)
if result.Error != nil {
if result.Error == gorm.ErrRecordNotFound {
return nil, models.ErrNotFound
}
return nil, fmt.Errorf("failed to get connection: %w", result.Error)
}
return &conn, nil
}
// CreateConnection creates a new connection
func (s *ConnectionService) CreateConnection(ctx context.Context, req *models.CreateConnectionRequest) (*models.UserConnection, error) {
// Validate request
if err := req.Validate(); err != nil {
return nil, models.ErrValidationFailed
}
// Encrypt password
encryptedPassword := req.Password
if req.Password != "" {
var err error
encryptedPassword, err = s.encryptSvc.EncryptPassword(req.Password)
if err != nil {
return nil, fmt.Errorf("failed to encrypt password: %w", err)
}
}
conn := &models.UserConnection{
ID: utils.GenerateID(),
Name: req.Name,
Type: req.Type,
Host: req.Host,
Port: req.Port,
Username: req.Username,
Password: encryptedPassword,
Database: req.Database,
SSLMode: req.SSLMode,
Timeout: req.Timeout,
}
if conn.Timeout <= 0 {
conn.Timeout = 30
}
result := s.db.WithContext(ctx).Create(conn)
if result.Error != nil {
return nil, fmt.Errorf("failed to create connection: %w", result.Error)
}
// Mask password in response
conn.Password = s.encryptSvc.MaskPasswordForLogging(conn.Password)
config.GetLogger().Info("connection created",
zap.String("id", conn.ID),
zap.String("name", conn.Name),
zap.String("type", string(conn.Type)))
return conn, nil
}
// UpdateConnection updates an existing connection
func (s *ConnectionService) UpdateConnection(ctx context.Context, id string, req *models.UpdateConnectionRequest) (*models.UserConnection, error) {
// Get existing connection
existing, err := s.GetConnectionByID(ctx, id)
if err != nil {
return nil, err
}
// Update fields
if req.Name != "" {
existing.Name = req.Name
}
if req.Type != "" {
existing.Type = req.Type
}
if req.Host != "" {
existing.Host = req.Host
}
if req.Port > 0 {
existing.Port = req.Port
}
if req.Username != "" {
existing.Username = req.Username
}
if req.Password != "" {
// Encrypt new password
encryptedPassword, err := s.encryptSvc.EncryptPassword(req.Password)
if err != nil {
return nil, fmt.Errorf("failed to encrypt password: %w", err)
}
existing.Password = encryptedPassword
}
if req.Database != "" {
existing.Database = req.Database
}
if req.SSLMode != "" {
existing.SSLMode = req.SSLMode
}
if req.Timeout > 0 {
existing.Timeout = req.Timeout
}
result := s.db.WithContext(ctx).Save(existing)
if result.Error != nil {
return nil, fmt.Errorf("failed to update connection: %w", result.Error)
}
// Remove cached connection if exists
if err := s.connManager.RemoveConnection(id); err != nil {
config.GetLogger().Warn("failed to remove cached connection", zap.Error(err))
}
// Mask password in response
existing.Password = s.encryptSvc.MaskPasswordForLogging(existing.Password)
config.GetLogger().Info("connection updated",
zap.String("id", id),
zap.String("name", existing.Name))
return existing, nil
}
// DeleteConnection deletes a connection
func (s *ConnectionService) DeleteConnection(ctx context.Context, id string) error {
// Check if connection exists
if _, err := s.GetConnectionByID(ctx, id); err != nil {
return err
}
// Remove from connection manager
if err := s.connManager.RemoveConnection(id); err != nil {
config.GetLogger().Warn("failed to remove from connection manager", zap.Error(err))
}
// Delete from database
result := s.db.WithContext(ctx).Delete(&models.UserConnection{}, "id = ?", id)
if result.Error != nil {
return fmt.Errorf("failed to delete connection: %w", result.Error)
}
config.GetLogger().Info("connection deleted", zap.String("id", id))
return nil
}
// TestConnection tests a database connection
func (s *ConnectionService) TestConnection(ctx context.Context, id string) (*models.ConnectionTestResult, error) {
// Get connection config
conn, err := s.GetConnectionByID(ctx, id)
if err != nil {
return nil, err
}
// Decrypt password
password, err := s.encryptSvc.DecryptPassword(conn.Password)
if err != nil {
return nil, fmt.Errorf("failed to decrypt password: %w", err)
}
startTime := time.Now()
// Create temporary connection
tempConn, err := s.connManager.GetConnection(conn, password)
if err != nil {
return &models.ConnectionTestResult{
Success: false,
Message: fmt.Sprintf("Connection failed: %v", err),
Duration: time.Since(startTime).Milliseconds(),
}, nil
}
// Get metadata
metadata, err := tempConn.GetMetadata()
if err != nil {
config.GetLogger().Warn("failed to get metadata", zap.Error(err))
}
return &models.ConnectionTestResult{
Success: true,
Message: "Connection successful",
Duration: time.Since(startTime).Milliseconds(),
Metadata: metadata,
}, nil
}
// ExecuteQuery executes a SQL query on a connection
func (s *ConnectionService) ExecuteQuery(ctx context.Context, connectionID, sql string) (*models.QueryResult, error) {
// Get connection config
conn, err := s.GetConnectionByID(ctx, connectionID)
if err != nil {
return nil, err
}
// Decrypt password
password, err := s.encryptSvc.DecryptPassword(conn.Password)
if err != nil {
return nil, fmt.Errorf("failed to decrypt password: %w", err)
}
// Get or create connection
dbConn, err := s.connManager.GetConnection(conn, password)
if err != nil {
return nil, fmt.Errorf("failed to get connection: %w", err)
}
// Execute query
startTime := time.Now()
var result *models.QueryResult
if utils.IsReadOnlyQuery(sql) {
result, err = dbConn.ExecuteQuery(sql)
} else {
result, err = dbConn.ExecuteStatement(sql)
}
duration := time.Since(startTime)
// Record in history
history := &models.QueryHistory{
ConnectionID: connectionID,
SQL: utils.TruncateString(sql, 10000),
Duration: duration.Milliseconds(),
Success: err == nil,
}
if result != nil {
history.RowsAffected = result.AffectedRows
}
if err != nil {
history.Error = err.Error()
}
s.db.WithContext(ctx).Create(history)
if err != nil {
return nil, fmt.Errorf("query execution failed: %w", err)
}
return result, nil
}
// GetTables returns all tables for a connection
func (s *ConnectionService) GetTables(ctx context.Context, connectionID string) ([]models.Table, error) {
// Get connection config
conn, err := s.GetConnectionByID(ctx, connectionID)
if err != nil {
return nil, err
}
// Decrypt password
password, err := s.encryptSvc.DecryptPassword(conn.Password)
if err != nil {
return nil, fmt.Errorf("failed to decrypt password: %w", err)
}
// Get or create connection
dbConn, err := s.connManager.GetConnection(conn, password)
if err != nil {
return nil, fmt.Errorf("failed to get connection: %w", err)
}
return dbConn.GetTables("")
}
// GetTableData returns data from a table
func (s *ConnectionService) GetTableData(
ctx context.Context,
connectionID, tableName string,
limit, offset int,
) (*models.QueryResult, error) {
// Validate limit
if limit <= 0 || limit > 1000 {
limit = 100
}
if offset < 0 {
offset = 0
}
// Build query based on connection type
conn, err := s.GetConnectionByID(ctx, connectionID)
if err != nil {
return nil, err
}
var query string
switch conn.Type {
case models.ConnectionTypeMySQL:
query = fmt.Sprintf("SELECT * FROM `%s` LIMIT %d OFFSET %d", tableName, limit, offset)
case models.ConnectionTypePostgreSQL:
query = fmt.Sprintf(`SELECT * FROM "%s" LIMIT %d OFFSET %d`, tableName, limit, offset)
case models.ConnectionTypeSQLite:
query = fmt.Sprintf(`SELECT * FROM "%s" LIMIT %d OFFSET %d`, tableName, limit, offset)
default:
return nil, models.ErrValidationFailed
}
return s.ExecuteQuery(ctx, connectionID, query)
}
// GetTableStructure returns the structure of a table
func (s *ConnectionService) GetTableStructure(ctx context.Context, connectionID, tableName string) (*models.TableStructure, error) {
// Get connection config
conn, err := s.GetConnectionByID(ctx, connectionID)
if err != nil {
return nil, err
}
// Decrypt password
password, err := s.encryptSvc.DecryptPassword(conn.Password)
if err != nil {
return nil, fmt.Errorf("failed to decrypt password: %w", err)
}
// Get or create connection
dbConn, err := s.connManager.GetConnection(conn, password)
if err != nil {
return nil, fmt.Errorf("failed to get connection: %w", err)
}
return dbConn.GetTableStructure(tableName)
}

View File

@@ -0,0 +1,199 @@
package services
import (
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"encoding/base64"
"fmt"
"io"
"os"
"sync"
"go.uber.org/zap"
"uzdb/internal/config"
"uzdb/internal/models"
"uzdb/internal/utils"
)
// EncryptionService handles encryption and decryption of sensitive data
type EncryptionService struct {
key []byte
cipher cipher.Block
gcm cipher.AEAD
mu sync.RWMutex
}
var (
encryptionInstance *EncryptionService
encryptionOnce sync.Once
)
// GetEncryptionService returns the singleton encryption service instance
func GetEncryptionService() *EncryptionService {
return encryptionInstance
}
// InitEncryptionService initializes the encryption service
func InitEncryptionService(cfg *config.EncryptionConfig) (*EncryptionService, error) {
var err error
encryptionOnce.Do(func() {
encryptionInstance = &EncryptionService{}
err = encryptionInstance.init(cfg)
})
return encryptionInstance, err
}
// init initializes the encryption service with a key
func (s *EncryptionService) init(cfg *config.EncryptionConfig) error {
s.mu.Lock()
defer s.mu.Unlock()
// Try to load existing key or generate new one
key, err := s.loadOrGenerateKey(cfg)
if err != nil {
return fmt.Errorf("failed to load/generate key: %w", err)
}
s.key = key
// Create AES cipher
block, err := aes.NewCipher(key)
if err != nil {
return fmt.Errorf("failed to create cipher: %w", err)
}
s.cipher = block
// Create GCM mode
gcm, err := cipher.NewGCM(block)
if err != nil {
return fmt.Errorf("failed to create GCM: %w", err)
}
s.gcm = gcm
config.GetLogger().Info("encryption service initialized")
return nil
}
// loadOrGenerateKey loads existing key or generates a new one
func (s *EncryptionService) loadOrGenerateKey(cfg *config.EncryptionConfig) ([]byte, error) {
// Use provided key if available
if cfg.Key != "" {
key := []byte(cfg.Key)
// Ensure key is correct length (32 bytes for AES-256)
if len(key) < 32 {
// Pad key
padded := make([]byte, 32)
copy(padded, key)
key = padded
} else if len(key) > 32 {
key = key[:32]
}
return key, nil
}
// Try to load from file
if cfg.KeyFile != "" {
if data, err := os.ReadFile(cfg.KeyFile); err == nil {
key := []byte(data)
if len(key) >= 32 {
return key[:32], nil
}
}
}
// Generate new key
key := make([]byte, 32) // AES-256
if _, err := io.ReadFull(rand.Reader, key); err != nil {
return nil, fmt.Errorf("failed to generate key: %w", err)
}
// Save key to file if path provided
if cfg.KeyFile != "" {
if err := os.WriteFile(cfg.KeyFile, key, 0600); err != nil {
config.GetLogger().Warn("failed to save encryption key", zap.Error(err))
} else {
config.GetLogger().Info("encryption key generated and saved",
zap.String("path", cfg.KeyFile))
}
}
return key, nil
}
// Encrypt encrypts plaintext using AES-GCM
func (s *EncryptionService) Encrypt(plaintext string) (string, error) {
s.mu.RLock()
defer s.mu.RUnlock()
if s.gcm == nil {
return "", models.ErrEncryptionFailed
}
// Generate nonce
nonce := make([]byte, s.gcm.NonceSize())
if _, err := io.ReadFull(rand.Reader, nonce); err != nil {
return "", fmt.Errorf("failed to generate nonce: %w", err)
}
// Encrypt
ciphertext := s.gcm.Seal(nonce, nonce, []byte(plaintext), nil)
// Encode to base64
return base64.StdEncoding.EncodeToString(ciphertext), nil
}
// Decrypt decrypts ciphertext using AES-GCM
func (s *EncryptionService) Decrypt(ciphertext string) (string, error) {
s.mu.RLock()
defer s.mu.RUnlock()
if s.gcm == nil {
return "", models.ErrEncryptionFailed
}
// Decode from base64
data, err := base64.StdEncoding.DecodeString(ciphertext)
if err != nil {
return "", fmt.Errorf("failed to decode ciphertext: %w", err)
}
// Verify nonce size
nonceSize := s.gcm.NonceSize()
if len(data) < nonceSize {
return "", models.ErrEncryptionFailed
}
// Extract nonce and ciphertext
nonce, ciphertextBytes := data[:nonceSize], data[nonceSize:]
// Decrypt
plaintext, err := s.gcm.Open(nil, nonce, ciphertextBytes, nil)
if err != nil {
return "", fmt.Errorf("failed to decrypt: %w", err)
}
return string(plaintext), nil
}
// EncryptPassword encrypts a password for storage
func (s *EncryptionService) EncryptPassword(password string) (string, error) {
if password == "" {
return "", nil
}
return s.Encrypt(password)
}
// DecryptPassword decrypts a stored password
func (s *EncryptionService) DecryptPassword(encryptedPassword string) (string, error) {
if encryptedPassword == "" {
return "", nil
}
return s.Decrypt(encryptedPassword)
}
// MaskPasswordForLogging masks password for safe logging
func (s *EncryptionService) MaskPasswordForLogging(password string) string {
return utils.MaskPassword(password)
}

236
internal/services/query.go Normal file
View File

@@ -0,0 +1,236 @@
package services
import (
"context"
"fmt"
"time"
"go.uber.org/zap"
"gorm.io/gorm"
"uzdb/internal/config"
"uzdb/internal/models"
)
// QueryService handles query-related operations
type QueryService struct {
db *gorm.DB
}
// NewQueryService creates a new query service
func NewQueryService(db *gorm.DB) *QueryService {
return &QueryService{
db: db,
}
}
// GetQueryHistory returns query history with pagination
func (s *QueryService) GetQueryHistory(
ctx context.Context,
connectionID string,
page, pageSize int,
) ([]models.QueryHistory, int64, error) {
if page <= 0 {
page = 1
}
if pageSize <= 0 || pageSize > 100 {
pageSize = 20
}
var total int64
var history []models.QueryHistory
query := s.db.WithContext(ctx).Model(&models.QueryHistory{})
if connectionID != "" {
query = query.Where("connection_id = ?", connectionID)
}
// Get total count
if err := query.Count(&total).Error; err != nil {
return nil, 0, fmt.Errorf("failed to count history: %w", err)
}
// Get paginated results
offset := (page - 1) * pageSize
if err := query.Order("executed_at DESC").
Offset(offset).
Limit(pageSize).
Find(&history).Error; err != nil {
return nil, 0, fmt.Errorf("failed to get history: %w", err)
}
config.GetLogger().Debug("retrieved query history",
zap.String("connection_id", connectionID),
zap.Int("page", page),
zap.Int("page_size", pageSize),
zap.Int64("total", total))
return history, total, nil
}
// GetSavedQueries returns all saved queries
func (s *QueryService) GetSavedQueries(
ctx context.Context,
connectionID string,
) ([]models.SavedQuery, error) {
var queries []models.SavedQuery
query := s.db.WithContext(ctx)
if connectionID != "" {
query = query.Where("connection_id = ?", connectionID)
}
result := query.Order("name ASC").Find(&queries)
if result.Error != nil {
return nil, fmt.Errorf("failed to get saved queries: %w", result.Error)
}
return queries, nil
}
// GetSavedQueryByID returns a saved query by ID
func (s *QueryService) GetSavedQueryByID(ctx context.Context, id uint) (*models.SavedQuery, error) {
var query models.SavedQuery
result := s.db.WithContext(ctx).First(&query, "id = ?", id)
if result.Error != nil {
if result.Error == gorm.ErrRecordNotFound {
return nil, models.ErrNotFound
}
return nil, fmt.Errorf("failed to get saved query: %w", result.Error)
}
return &query, nil
}
// CreateSavedQuery creates a new saved query
func (s *QueryService) CreateSavedQuery(
ctx context.Context,
req *models.CreateSavedQueryRequest,
) (*models.SavedQuery, error) {
query := &models.SavedQuery{
Name: req.Name,
Description: req.Description,
SQL: req.SQL,
ConnectionID: req.ConnectionID,
Tags: req.Tags,
}
result := s.db.WithContext(ctx).Create(query)
if result.Error != nil {
return nil, fmt.Errorf("failed to create saved query: %w", result.Error)
}
config.GetLogger().Info("saved query created",
zap.Uint("id", query.ID),
zap.String("name", query.Name))
return query, nil
}
// UpdateSavedQuery updates an existing saved query
func (s *QueryService) UpdateSavedQuery(
ctx context.Context,
id uint,
req *models.UpdateSavedQueryRequest,
) (*models.SavedQuery, error) {
// Get existing query
existing, err := s.GetSavedQueryByID(ctx, id)
if err != nil {
return nil, err
}
// Update fields
if req.Name != "" {
existing.Name = req.Name
}
if req.Description != "" {
existing.Description = req.Description
}
if req.SQL != "" {
existing.SQL = req.SQL
}
if req.ConnectionID != "" {
existing.ConnectionID = req.ConnectionID
}
if req.Tags != "" {
existing.Tags = req.Tags
}
result := s.db.WithContext(ctx).Save(existing)
if result.Error != nil {
return nil, fmt.Errorf("failed to update saved query: %w", result.Error)
}
config.GetLogger().Info("saved query updated",
zap.Uint("id", id),
zap.String("name", existing.Name))
return existing, nil
}
// DeleteSavedQuery deletes a saved query
func (s *QueryService) DeleteSavedQuery(ctx context.Context, id uint) error {
// Check if exists
if _, err := s.GetSavedQueryByID(ctx, id); err != nil {
return err
}
result := s.db.WithContext(ctx).Delete(&models.SavedQuery{}, "id = ?", id)
if result.Error != nil {
return fmt.Errorf("failed to delete saved query: %w", result.Error)
}
config.GetLogger().Info("saved query deleted", zap.Uint("id", id))
return nil
}
// ClearOldHistory clears query history older than specified days
func (s *QueryService) ClearOldHistory(ctx context.Context, days int) (int64, error) {
if days <= 0 {
days = 30 // Default to 30 days
}
cutoffTime := time.Now().AddDate(0, 0, -days)
result := s.db.WithContext(ctx).
Where("executed_at < ?", cutoffTime).
Delete(&models.QueryHistory{})
if result.Error != nil {
return 0, fmt.Errorf("failed to clear old history: %w", result.Error)
}
config.GetLogger().Info("cleared old query history",
zap.Int64("deleted_count", result.RowsAffected),
zap.Int("days", days))
return result.RowsAffected, nil
}
// GetRecentQueries returns recent queries for quick access
func (s *QueryService) GetRecentQueries(
ctx context.Context,
connectionID string,
limit int,
) ([]models.QueryHistory, error) {
if limit <= 0 || limit > 50 {
limit = 10
}
var queries []models.QueryHistory
query := s.db.WithContext(ctx).
Where("connection_id = ? AND success = ?", connectionID, true).
Order("executed_at DESC").
Limit(limit).
Find(&queries)
if query.Error != nil {
return nil, fmt.Errorf("failed to get recent queries: %w", query.Error)
}
return queries, nil
}