SuperCoder/internal/app/services.go

65 lines
1.7 KiB
Go
Raw Normal View History

2025-03-23 19:19:08 +01:00
package app
import (
"context"
"database/sql"
2025-03-27 22:35:48 +01:00
"github.com/kujtimiihoxha/termai/internal/config"
2025-03-23 19:19:08 +01:00
"github.com/kujtimiihoxha/termai/internal/db"
2025-04-13 11:29:20 +02:00
"github.com/kujtimiihoxha/termai/internal/history"
2025-03-23 19:19:08 +01:00
"github.com/kujtimiihoxha/termai/internal/logging"
2025-04-03 15:20:15 +02:00
"github.com/kujtimiihoxha/termai/internal/lsp"
"github.com/kujtimiihoxha/termai/internal/lsp/watcher"
2025-03-23 22:25:31 +01:00
"github.com/kujtimiihoxha/termai/internal/message"
2025-03-25 13:04:36 +01:00
"github.com/kujtimiihoxha/termai/internal/permission"
2025-03-23 19:19:08 +01:00
"github.com/kujtimiihoxha/termai/internal/session"
)
type App struct {
Context context.Context
2025-03-25 13:04:36 +01:00
Sessions session.Service
Messages message.Service
2025-04-13 11:29:20 +02:00
Files history.Service
2025-03-25 13:04:36 +01:00
Permissions permission.Service
2025-03-23 19:19:08 +01:00
2025-04-03 15:20:15 +02:00
LSPClients map[string]*lsp.Client
2025-03-23 19:19:08 +01:00
}
func New(ctx context.Context, conn *sql.DB) *App {
2025-04-03 15:20:15 +02:00
cfg := config.Get()
2025-04-10 13:29:44 +02:00
logging.Info("Debug mode enabled")
2025-03-23 19:19:08 +01:00
q := db.New(conn)
2025-03-23 22:25:31 +01:00
sessions := session.NewService(ctx, q)
messages := message.NewService(ctx, q)
2025-04-13 11:29:20 +02:00
files := history.NewService(ctx, q)
2025-03-23 22:25:31 +01:00
2025-04-03 15:20:15 +02:00
app := &App{
2025-03-25 13:04:36 +01:00
Context: ctx,
Sessions: sessions,
Messages: messages,
2025-04-13 11:29:20 +02:00
Files: files,
Permissions: permission.NewPermissionService(),
2025-04-03 15:20:15 +02:00
LSPClients: make(map[string]*lsp.Client),
}
for name, client := range cfg.LSP {
lspClient, err := lsp.NewClient(ctx, client.Command, client.Args...)
2025-04-03 15:20:15 +02:00
workspaceWatcher := watcher.NewWorkspaceWatcher(lspClient)
if err != nil {
2025-04-10 13:29:44 +02:00
logging.Error("Failed to create LSP client for", name, err)
2025-04-03 15:20:15 +02:00
continue
}
_, err = lspClient.InitializeLSPClient(ctx, config.WorkingDirectory())
if err != nil {
2025-04-10 13:29:44 +02:00
logging.Error("Initialize failed", "error", err)
2025-04-03 15:20:15 +02:00
continue
}
go workspaceWatcher.WatchWorkspace(ctx, config.WorkingDirectory())
app.LSPClients[name] = lspClient
2025-03-23 19:19:08 +01:00
}
2025-04-03 15:20:15 +02:00
return app
2025-03-23 19:19:08 +01:00
}