mirror of
https://github.com/SamyRai/tercul-backend.git
synced 2025-12-27 04:01:34 +00:00
This commit addresses a broken build state caused by a mid-stream architectural refactoring. The changes align the existing code with the new Domain-Driven Design (DDD-lite) structure outlined in `refactor.md`. Key changes include: - Defined missing domain interfaces for `Auth`, `Localization`, and `Search`. - Refactored application services to use a `Commands` and `Queries` pattern. - Updated GraphQL resolvers to call application services instead of accessing repositories directly. - Fixed dependency injection in `cmd/api/main.go` by removing the non-existent `ApplicationBuilder` and manually instantiating services. - Corrected numerous test files (`integration`, `unit`, and `repository` tests) to reflect the new architecture, including fixing mock objects and test suite setups. - Added missing database migrations for test schemas to resolve "no such table" errors. This effort successfully gets the application to a compilable state and passes a significant portion of the test suite, laying the groundwork for further development and fixing the remaining test failures.
134 lines
3.8 KiB
Go
134 lines
3.8 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"os"
|
|
"os/signal"
|
|
"syscall"
|
|
"tercul/internal/app"
|
|
"tercul/internal/app/analytics"
|
|
graph "tercul/internal/adapters/graphql"
|
|
"tercul/internal/data/sql"
|
|
"tercul/internal/jobs/linguistics"
|
|
"tercul/internal/platform/auth"
|
|
"tercul/internal/platform/config"
|
|
"tercul/internal/platform/db"
|
|
"tercul/internal/platform/log"
|
|
"tercul/internal/platform/search"
|
|
"time"
|
|
|
|
"github.com/99designs/gqlgen/graphql/playground"
|
|
"github.com/weaviate/weaviate-go-client/v5/weaviate"
|
|
)
|
|
|
|
// main is the entry point for the Tercul application.
|
|
func main() {
|
|
// Load configuration from environment variables
|
|
config.LoadConfig()
|
|
|
|
// Initialize structured logger with appropriate log level
|
|
log.SetDefaultLevel(log.InfoLevel)
|
|
log.LogInfo("Starting Tercul application",
|
|
log.F("environment", config.Cfg.Environment),
|
|
log.F("version", "1.0.0"))
|
|
|
|
// Initialize database connection
|
|
database, err := db.InitDB()
|
|
if err != nil {
|
|
log.LogFatal("Failed to initialize database", log.F("error", err))
|
|
}
|
|
defer db.Close()
|
|
|
|
// Initialize Weaviate client
|
|
weaviateCfg := weaviate.Config{
|
|
Host: config.Cfg.WeaviateHost,
|
|
Scheme: config.Cfg.WeaviateScheme,
|
|
}
|
|
weaviateClient, err := weaviate.NewClient(weaviateCfg)
|
|
if err != nil {
|
|
log.LogFatal("Failed to create weaviate client", log.F("error", err))
|
|
}
|
|
|
|
// Create search client
|
|
searchClient := search.NewWeaviateWrapper(weaviateClient)
|
|
|
|
// Create repositories
|
|
repos := sql.NewRepositories(database)
|
|
|
|
// Create linguistics dependencies
|
|
analysisRepo := linguistics.NewGORMAnalysisRepository(database)
|
|
sentimentProvider, err := linguistics.NewGoVADERSentimentProvider()
|
|
if err != nil {
|
|
log.LogFatal("Failed to create sentiment provider", log.F("error", err))
|
|
}
|
|
|
|
// Create application services
|
|
analyticsService := analytics.NewService(repos.Analytics, analysisRepo, repos.Translation, repos.Work, sentimentProvider)
|
|
|
|
// Create application
|
|
application := app.NewApplication(repos, searchClient, analyticsService)
|
|
|
|
// Create GraphQL server
|
|
resolver := &graph.Resolver{
|
|
App: application,
|
|
}
|
|
|
|
jwtManager := auth.NewJWTManager()
|
|
srv := NewServerWithAuth(resolver, jwtManager)
|
|
graphQLServer := &http.Server{
|
|
Addr: config.Cfg.ServerPort,
|
|
Handler: srv,
|
|
}
|
|
log.LogInfo("GraphQL server created successfully", log.F("port", config.Cfg.ServerPort))
|
|
|
|
// Create GraphQL playground
|
|
playgroundHandler := playground.Handler("GraphQL", "/query")
|
|
playgroundServer := &http.Server{
|
|
Addr: config.Cfg.PlaygroundPort,
|
|
Handler: playgroundHandler,
|
|
}
|
|
log.LogInfo("GraphQL playground created successfully", log.F("port", config.Cfg.PlaygroundPort))
|
|
|
|
// Start HTTP servers in goroutines
|
|
go func() {
|
|
log.LogInfo("Starting GraphQL server",
|
|
log.F("port", config.Cfg.ServerPort))
|
|
if err := graphQLServer.ListenAndServe(); err != nil && err != http.ErrServerClosed {
|
|
log.LogFatal("Failed to start GraphQL server",
|
|
log.F("error", err))
|
|
}
|
|
}()
|
|
|
|
go func() {
|
|
log.LogInfo("Starting GraphQL playground",
|
|
log.F("port", config.Cfg.PlaygroundPort))
|
|
if err := playgroundServer.ListenAndServe(); err != nil && err != http.ErrServerClosed {
|
|
log.LogFatal("Failed to start GraphQL playground",
|
|
log.F("error", err))
|
|
}
|
|
}()
|
|
|
|
// Wait for interrupt signal to gracefully shutdown the servers
|
|
quit := make(chan os.Signal, 1)
|
|
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
|
|
<-quit
|
|
|
|
log.LogInfo("Shutting down servers...")
|
|
|
|
// Graceful shutdown
|
|
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
|
defer cancel()
|
|
|
|
if err := graphQLServer.Shutdown(ctx); err != nil {
|
|
log.LogError("GraphQL server forced to shutdown",
|
|
log.F("error", err))
|
|
}
|
|
|
|
if err := playgroundServer.Shutdown(ctx); err != nil {
|
|
log.LogError("GraphQL playground forced to shutdown",
|
|
log.F("error", err))
|
|
}
|
|
|
|
log.LogInfo("All servers shutdown successfully")
|
|
} |