tercul-backend/internal/app/server_factory.go
google-labs-jules[bot] 8797cec718 Refactor: In-progress refactoring to fix build.
This commit includes the following changes:
- Refactored all data repositories in `internal/data/sql/` to use a consistent `sql` package and to align with the new `domain` models.
- Fixed the GraphQL structure by moving the server creation logic from `internal/app` to `cmd/api`, which resolved an import cycle.
- Corrected numerous incorrect import paths for packages like `graph`, `linguistics`, `syncjob`, and the legacy `models` package.
- Resolved several package and function redeclaration errors.
- Removed legacy migration code.
2025-09-05 15:11:30 +00:00

85 lines
2.4 KiB
Go

package app
import (
"net/http"
"tercul/internal/jobs/linguistics"
syncjob "tercul/internal/jobs/sync"
"tercul/internal/platform/auth"
"tercul/internal/platform/config"
"tercul/internal/platform/log"
"github.com/99designs/gqlgen/graphql/playground"
"github.com/hibiken/asynq"
)
// ServerFactory handles the creation of HTTP and background job servers
type ServerFactory struct {
appBuilder *ApplicationBuilder
}
// NewServerFactory creates a new ServerFactory
func NewServerFactory(appBuilder *ApplicationBuilder) *ServerFactory {
return &ServerFactory{
appBuilder: appBuilder,
}
}
// CreateBackgroundJobServers creates and configures background job servers
func (f *ServerFactory) CreateBackgroundJobServers() ([]*asynq.Server, error) {
log.LogInfo("Setting up background job servers")
redisOpt := asynq.RedisClientOpt{
Addr: config.Cfg.RedisAddr,
Password: config.Cfg.RedisPassword,
DB: config.Cfg.RedisDB,
}
var servers []*asynq.Server
// Setup data synchronization server
log.LogInfo("Setting up data synchronization server",
log.F("concurrency", config.Cfg.MaxRetries))
syncServer := asynq.NewServer(redisOpt, asynq.Config{Concurrency: config.Cfg.MaxRetries})
// Create sync job instance
syncJobInstance := syncjob.NewSyncJob(
f.appBuilder.GetDatabase(),
f.appBuilder.GetAsynqClient(),
)
// Register sync job handlers
syncjob.RegisterQueueHandlers(syncServer, syncJobInstance)
servers = append(servers, syncServer)
// Setup linguistic analysis server
log.LogInfo("Setting up linguistic analysis server",
log.F("concurrency", config.Cfg.MaxRetries))
// Create linguistic sync job
linguisticSyncJob := linguistics.NewLinguisticSyncJob(
f.appBuilder.GetDatabase(),
f.appBuilder.GetLinguistics().GetAnalyzer(),
f.appBuilder.GetAsynqClient(),
)
// Create linguistic server and register handlers
linguisticServer := asynq.NewServer(redisOpt, asynq.Config{Concurrency: config.Cfg.MaxRetries})
// Register linguistic handlers
linguisticMux := asynq.NewServeMux()
linguistics.RegisterLinguisticHandlers(linguisticMux, linguisticSyncJob)
// For now, we'll need to run the server with the mux when it's started
// This is a temporary workaround - in production, you'd want to properly configure the server
servers = append(servers, linguisticServer)
log.LogInfo("Background job servers created successfully",
log.F("serverCount", len(servers)))
return servers, nil
}