tercul-backend/internal/app/app.go
google-labs-jules[bot] 1bb3e23c47 refactor(api): Consolidate server setup
This commit refactors the API server startup logic in `cmd/api/main.go` to simplify the application's architecture.

Key changes:
- Consolidates the three separate HTTP servers (GraphQL API, GraphQL Playground, and Prometheus metrics) into a single `http.Server` instance.
- Uses a single `http.ServeMux` to route requests to the appropriate handlers on distinct paths (`/query`, `/playground`, `/metrics`).
- Removes the now-redundant `PlaygroundPort` from the application's configuration.

This change simplifies the server startup and shutdown logic, reduces resource usage, and makes the application's entry point cleaner and easier to maintain.
2025-10-05 12:18:57 +00:00

105 lines
3.8 KiB
Go

package app
import (
"tercul/internal/app/analytics"
"tercul/internal/app/author"
"tercul/internal/app/book"
"tercul/internal/app/bookmark"
"tercul/internal/app/category"
"tercul/internal/app/collection"
"tercul/internal/app/comment"
"tercul/internal/app/like"
"tercul/internal/app/localization"
"tercul/internal/app/tag"
"tercul/internal/app/translation"
"tercul/internal/app/user"
"tercul/internal/app/auth"
"tercul/internal/app/work"
"tercul/internal/domain"
auth_domain "tercul/internal/domain/auth"
localization_domain "tercul/internal/domain/localization"
"tercul/internal/domain/search"
work_domain "tercul/internal/domain/work"
platform_auth "tercul/internal/platform/auth"
)
import "tercul/internal/app/authz"
// Dependencies holds all external dependencies for the application.
type Dependencies struct {
WorkRepo work_domain.WorkRepository
UserRepo domain.UserRepository
AuthorRepo domain.AuthorRepository
TranslationRepo domain.TranslationRepository
CommentRepo domain.CommentRepository
LikeRepo domain.LikeRepository
BookmarkRepo domain.BookmarkRepository
CollectionRepo domain.CollectionRepository
TagRepo domain.TagRepository
CategoryRepo domain.CategoryRepository
BookRepo domain.BookRepository
PublisherRepo domain.PublisherRepository
SourceRepo domain.SourceRepository
CopyrightRepo domain.CopyrightRepository
MonetizationRepo domain.MonetizationRepository
AnalyticsRepo analytics.Repository
AuthRepo auth_domain.AuthRepository
LocalizationRepo localization_domain.LocalizationRepository
SearchClient search.SearchClient
AnalyticsService analytics.Service
JWTManager platform_auth.JWTManagement
}
// Application is a container for all the application-layer services.
type Application struct {
Author *author.Service
Book *book.Service
Bookmark *bookmark.Service
Category *category.Service
Collection *collection.Service
Comment *comment.Service
Like *like.Service
Tag *tag.Service
Translation *translation.Service
User *user.Service
Localization *localization.Service
Auth *auth.Service
Authz *authz.Service
Work *work.Service
Analytics analytics.Service
}
func NewApplication(deps Dependencies) *Application {
authzService := authz.NewService(deps.WorkRepo, deps.TranslationRepo)
authorService := author.NewService(deps.AuthorRepo)
bookService := book.NewService(deps.BookRepo, authzService)
bookmarkService := bookmark.NewService(deps.BookmarkRepo, deps.AnalyticsService)
categoryService := category.NewService(deps.CategoryRepo)
collectionService := collection.NewService(deps.CollectionRepo)
commentService := comment.NewService(deps.CommentRepo, authzService, deps.AnalyticsService)
likeService := like.NewService(deps.LikeRepo, deps.AnalyticsService)
tagService := tag.NewService(deps.TagRepo)
translationService := translation.NewService(deps.TranslationRepo, authzService)
userService := user.NewService(deps.UserRepo, authzService)
localizationService := localization.NewService(deps.LocalizationRepo)
authService := auth.NewService(deps.UserRepo, deps.JWTManager)
workService := work.NewService(deps.WorkRepo, deps.SearchClient, authzService)
return &Application{
Author: authorService,
Book: bookService,
Bookmark: bookmarkService,
Category: categoryService,
Collection: collectionService,
Comment: commentService,
Like: likeService,
Tag: tagService,
Translation: translationService,
User: userService,
Localization: localizationService,
Auth: authService,
Authz: authzService,
Work: workService,
Analytics: deps.AnalyticsService,
}
}