mirror of
https://github.com/SamyRai/tercul-backend.git
synced 2025-12-27 02:51:34 +00:00
This commit addresses all the high-priority tasks outlined in the TASKS.md file, significantly improving the application's observability, completing key features, and refactoring critical parts of the codebase. ### Observability - **Centralized Logging:** Implemented a new structured, context-aware logging system using `zerolog`. A new logging middleware injects request-specific information (request ID, user ID, trace ID) into the logger, and all application logging has been refactored to use this new system. - **Prometheus Metrics:** Added Prometheus metrics for database query performance by creating a GORM plugin that automatically records query latency and totals. - **OpenTelemetry Tracing:** Fully instrumented all application services in `internal/app` and data repositories in `internal/data/sql` with OpenTelemetry tracing, providing deep visibility into application performance. ### Features - **Analytics:** Implemented like, comment, and bookmark counting. The respective command handlers now call the analytics service to increment counters when these actions are performed. - **Enrichment Tool:** Built a new, extensible `enrich` command-line tool to fetch data from external sources. The initial implementation enriches author data using the Open Library API. ### Refactoring & Fixes - **Decoupled Testing:** Refactored the testing utilities in `internal/testutil` to be database-agnostic, promoting the use of mock-based unit tests and improving test speed and reliability. - **Build Fixes:** Resolved numerous build errors, including a critical import cycle between the logging, observability, and authentication packages. - **Search Service:** Fixed the search service integration by implementing the `GetWorkContent` method in the localization service, allowing the search indexer to correctly fetch and index work content.
195 lines
5.6 KiB
Go
195 lines
5.6 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"net/http"
|
|
"os"
|
|
"os/signal"
|
|
"path/filepath"
|
|
"runtime"
|
|
"syscall"
|
|
"tercul/internal/app"
|
|
"tercul/internal/app/analytics"
|
|
graph "tercul/internal/adapters/graphql"
|
|
dbsql "tercul/internal/data/sql"
|
|
"tercul/internal/jobs/linguistics"
|
|
"tercul/internal/observability"
|
|
"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/pressly/goose/v3"
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
"github.com/weaviate/weaviate-go-client/v5/weaviate"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// runMigrations applies database migrations using goose.
|
|
func runMigrations(gormDB *gorm.DB) error {
|
|
sqlDB, err := gormDB.DB()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if err := goose.SetDialect("postgres"); err != nil {
|
|
return err
|
|
}
|
|
|
|
// This is brittle. A better approach might be to use an env var or config.
|
|
_, b, _, _ := runtime.Caller(0)
|
|
migrationsDir := filepath.Join(filepath.Dir(b), "../../internal/data/migrations")
|
|
|
|
log.Info(fmt.Sprintf("Applying database migrations from %s", migrationsDir))
|
|
if err := goose.Up(sqlDB, migrationsDir); err != nil {
|
|
return err
|
|
}
|
|
log.Info("Database migrations applied successfully")
|
|
return nil
|
|
}
|
|
|
|
// main is the entry point for the Tercul application.
|
|
func main() {
|
|
// Load configuration from environment variables
|
|
config.LoadConfig()
|
|
|
|
// Initialize logger
|
|
log.Init("tercul-api", config.Cfg.Environment)
|
|
obsLogger := observability.NewLogger("tercul-api", config.Cfg.Environment)
|
|
|
|
// Initialize OpenTelemetry Tracer Provider
|
|
tp, err := observability.TracerProvider("tercul-api", config.Cfg.Environment)
|
|
if err != nil {
|
|
log.Fatal(err, "Failed to initialize OpenTelemetry tracer")
|
|
}
|
|
defer func() {
|
|
if err := tp.Shutdown(context.Background()); err != nil {
|
|
log.Error(err, "Error shutting down tracer provider")
|
|
}
|
|
}()
|
|
|
|
// Initialize Prometheus metrics
|
|
reg := prometheus.NewRegistry()
|
|
metrics := observability.NewMetrics(reg) // Metrics are registered automatically
|
|
|
|
log.Info(fmt.Sprintf("Starting Tercul application in %s environment, version 1.0.0", config.Cfg.Environment))
|
|
|
|
// Initialize database connection
|
|
database, err := db.InitDB(metrics)
|
|
if err != nil {
|
|
log.Fatal(err, "Failed to initialize database")
|
|
}
|
|
defer db.Close()
|
|
|
|
if err := runMigrations(database); err != nil {
|
|
log.Fatal(err, "Failed to apply database migrations")
|
|
}
|
|
|
|
// Initialize Weaviate client
|
|
weaviateCfg := weaviate.Config{
|
|
Host: config.Cfg.WeaviateHost,
|
|
Scheme: config.Cfg.WeaviateScheme,
|
|
}
|
|
weaviateClient, err := weaviate.NewClient(weaviateCfg)
|
|
if err != nil {
|
|
log.Fatal(err, "Failed to create weaviate client")
|
|
}
|
|
|
|
// Create search client
|
|
searchClient := search.NewWeaviateWrapper(weaviateClient)
|
|
|
|
// Create repositories
|
|
repos := dbsql.NewRepositories(database)
|
|
|
|
// Create linguistics dependencies
|
|
analysisRepo := linguistics.NewGORMAnalysisRepository(database)
|
|
sentimentProvider, err := linguistics.NewGoVADERSentimentProvider()
|
|
if err != nil {
|
|
log.Fatal(err, "Failed to create sentiment provider")
|
|
}
|
|
|
|
// 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, metrics, obsLogger)
|
|
graphQLServer := &http.Server{
|
|
Addr: config.Cfg.ServerPort,
|
|
Handler: srv,
|
|
}
|
|
log.Info(fmt.Sprintf("GraphQL server created successfully on port %s", config.Cfg.ServerPort))
|
|
|
|
// Create GraphQL playground
|
|
playgroundHandler := playground.Handler("GraphQL", "/query")
|
|
playgroundServer := &http.Server{
|
|
Addr: config.Cfg.PlaygroundPort,
|
|
Handler: playgroundHandler,
|
|
}
|
|
log.Info(fmt.Sprintf("GraphQL playground created successfully on port %s", config.Cfg.PlaygroundPort))
|
|
|
|
// Create metrics server
|
|
metricsServer := &http.Server{
|
|
Addr: ":9090",
|
|
Handler: observability.PrometheusHandler(reg),
|
|
}
|
|
log.Info("Metrics server created successfully on port :9090")
|
|
|
|
// Start HTTP servers in goroutines
|
|
go func() {
|
|
log.Info(fmt.Sprintf("Starting GraphQL server on port %s", config.Cfg.ServerPort))
|
|
if err := graphQLServer.ListenAndServe(); err != nil && err != http.ErrServerClosed {
|
|
log.Fatal(err, "Failed to start GraphQL server")
|
|
}
|
|
}()
|
|
|
|
go func() {
|
|
log.Info(fmt.Sprintf("Starting GraphQL playground on port %s", config.Cfg.PlaygroundPort))
|
|
if err := playgroundServer.ListenAndServe(); err != nil && err != http.ErrServerClosed {
|
|
log.Fatal(err, "Failed to start GraphQL playground")
|
|
}
|
|
}()
|
|
|
|
go func() {
|
|
log.Info("Starting metrics server on port :9090")
|
|
if err := metricsServer.ListenAndServe(); err != nil && err != http.ErrServerClosed {
|
|
log.Fatal(err, "Failed to start metrics server")
|
|
}
|
|
}()
|
|
|
|
// Wait for interrupt signal to gracefully shutdown the servers
|
|
quit := make(chan os.Signal, 1)
|
|
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
|
|
<-quit
|
|
|
|
log.Info("Shutting down servers...")
|
|
|
|
// Graceful shutdown
|
|
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
|
defer cancel()
|
|
|
|
if err := graphQLServer.Shutdown(ctx); err != nil {
|
|
log.Error(err, "GraphQL server forced to shutdown")
|
|
}
|
|
|
|
if err := playgroundServer.Shutdown(ctx); err != nil {
|
|
log.Error(err, "GraphQL playground forced to shutdown")
|
|
}
|
|
|
|
if err := metricsServer.Shutdown(ctx); err != nil {
|
|
log.Error(err, "Metrics server forced to shutdown")
|
|
}
|
|
|
|
log.Info("All servers shutdown successfully")
|
|
} |