Build and Deploy / test-backend (push) Successful in 1m11s
Build and Deploy / lint-frontend (push) Successful in 21s
Build and Deploy / build-backend (push) Successful in 58s
Build and Deploy / deploy (push) Canceled after 0s
Build and Deploy / build-frontend (push) Canceled after 5m38s
113 lines
4.8 KiB
Go
113 lines
4.8 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"log"
|
|
|
|
"github.com/gin-contrib/cors"
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"github.com/dusiburg/fancy-api/db"
|
|
"github.com/dusiburg/fancy-api/internal/cache"
|
|
"github.com/dusiburg/fancy-api/internal/config"
|
|
"github.com/dusiburg/fancy-api/internal/handler"
|
|
"github.com/dusiburg/fancy-api/internal/middleware"
|
|
"github.com/dusiburg/fancy-api/internal/model"
|
|
"github.com/dusiburg/fancy-api/internal/provider/collocations"
|
|
"github.com/dusiburg/fancy-api/internal/provider/dictionary"
|
|
"github.com/dusiburg/fancy-api/internal/provider/examples"
|
|
"github.com/dusiburg/fancy-api/internal/provider/kotosoft"
|
|
"github.com/dusiburg/fancy-api/internal/provider/llm"
|
|
"github.com/dusiburg/fancy-api/internal/provider/translation"
|
|
"github.com/dusiburg/fancy-api/internal/repository"
|
|
"github.com/dusiburg/fancy-api/internal/service"
|
|
)
|
|
|
|
func main() {
|
|
cfg := config.Load()
|
|
redisConn, err := cache.NewRedis(context.Background(), cfg.RedisConnectionString)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
pool, err := db.NewPool(context.Background(), cfg.PostgresConnectionString)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
r := gin.Default()
|
|
|
|
r.Use(cors.New(cors.Config{
|
|
AllowOrigins: []string{cfg.FrontendUrl},
|
|
AllowMethods: []string{"GET", "POST", "PATCH", "DELETE", "OPTIONS"},
|
|
AllowHeaders: []string{"Content-Type", "Authorization"},
|
|
AllowCredentials: true,
|
|
}))
|
|
|
|
deeplProvider := translation.NewDeepLProvider(cfg.DeepLAPIKey)
|
|
kotosoftClient := kotosoft.NewClient(cfg.KotosoftIDAPIUrl)
|
|
|
|
usageTracker := service.NewUsageTracker(redisConn.Client())
|
|
// openRouterProvider := llm.NewRecordingProvider(llm.NewOpenRouterProvider(cfg.OpenRouterAPIKey), usageTracker)
|
|
groqProvider := llm.NewRecordingProvider(llm.NewGroqProvider(cfg.GroqAPIKey), usageTracker)
|
|
|
|
collocationsLLMProvider := collocations.NewLLMProvider(groqProvider)
|
|
examplesLLMProvider := examples.NewLLMProvider(groqProvider)
|
|
dictionaryLLMProvider := dictionary.NewLLMProvider(groqProvider)
|
|
dictionaryProvider := dictionary.NewLanguageRouter(
|
|
dictionary.NewFallbackProvider(dictionary.NewFreeDictionaryProvider(), dictionaryLLMProvider),
|
|
map[model.Language]dictionary.Provider{
|
|
model.LangIT: dictionaryLLMProvider,
|
|
model.LangRU: dictionaryLLMProvider,
|
|
},
|
|
)
|
|
|
|
collectionRepository := repository.NewCollectionRepository(pool)
|
|
userRepository := repository.NewUserRepository(pool)
|
|
|
|
translationService := service.NewTranslationService(deeplProvider, redisConn.Client())
|
|
collectionService := service.NewCollectionService(collectionRepository)
|
|
dictionaryService := service.NewDictionaryService(dictionaryProvider, redisConn.Client(), collectionService)
|
|
collocationsService := service.NewCollocationsService(collocationsLLMProvider, redisConn.Client())
|
|
examplesService := service.NewExamplesService(examplesLLMProvider, redisConn.Client())
|
|
sessionService := service.NewSessionService(redisConn.Client(), cfg.SessionSecret)
|
|
authService := service.NewAuthService(kotosoftClient, userRepository, sessionService)
|
|
|
|
translationHandler := handler.NewTranslationHandler(translationService)
|
|
dictionaryHandler := handler.NewDictionaryHandler(dictionaryService)
|
|
collocationsHandler := handler.NewCollocationsHandler(collocationsService)
|
|
examplesHadnler := handler.NewExamplesHandler(examplesService)
|
|
usageHandler := handler.NewUsageHandler(service.NewUsageService(usageTracker, deeplProvider, cfg.AIMonthlyTokenLimit))
|
|
collectionHandler := handler.CreateCollectionHandler(collectionService)
|
|
authHandler := handler.NewAuthHandler(authService)
|
|
|
|
authMiddleware := middleware.RequireAuth(sessionService)
|
|
|
|
r.POST("/api/translate", authMiddleware, translationHandler.Translate)
|
|
r.GET("/api/dictionary/:lang/:word", authMiddleware, dictionaryHandler.Lookup)
|
|
r.GET("/api/collocations/:lang/:word", authMiddleware, collocationsHandler.GenerateCollocations)
|
|
r.GET("/api/examples/:lang/:word", authMiddleware, examplesHadnler.GenerateExamples)
|
|
r.GET("/api/usage", authMiddleware, usageHandler.Get)
|
|
|
|
r.POST("/api/auth/session", authHandler.CreateSession)
|
|
r.GET("/api/auth/me", authMiddleware, authHandler.Me)
|
|
r.DELETE("/api/auth/session", authMiddleware, authHandler.DeleteSession)
|
|
|
|
collections := r.Group("/api/collections", authMiddleware)
|
|
{
|
|
collections.GET("", collectionHandler.GetCollections)
|
|
collections.POST("", collectionHandler.CreateCollection)
|
|
collections.GET("/:id", collectionHandler.GetCollectionByID)
|
|
collections.GET("/search/:lang/:word", collectionHandler.GetCollectionsByWord)
|
|
collections.PATCH("/:id", collectionHandler.UpdateCollectionByID)
|
|
collections.POST("/:id/words", collectionHandler.AddWords)
|
|
collections.DELETE("/:id/words", collectionHandler.DeleteWord)
|
|
collections.DELETE("/:id", collectionHandler.DeleteCollection)
|
|
}
|
|
|
|
log.Printf("server starting on :%s", cfg.Port)
|
|
if err := r.Run(":" + cfg.Port); err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
}
|