🏗️ Switch repositories from memory to sqlite

This commit is contained in:
Maxim Lebedev 2023-12-20 07:29:09 +06:00
parent 90d4e4d071
commit 3d457d0034
Signed by: toby3d
GPG Key ID: 1F14E25B7C119FC5

29
main.go
View File

@ -14,18 +14,20 @@ import (
"path/filepath" "path/filepath"
"time" "time"
"golang.org/x/text/feature/plural"
"github.com/caarlos0/env/v10" "github.com/caarlos0/env/v10"
"github.com/jmoiron/sqlx"
"golang.org/x/text/feature/plural"
"golang.org/x/text/language" "golang.org/x/text/language"
"golang.org/x/text/message" "golang.org/x/text/message"
_ "modernc.org/sqlite"
"source.toby3d.me/toby3d/hub/internal/domain" "source.toby3d.me/toby3d/hub/internal/domain"
hubhttprelivery "source.toby3d.me/toby3d/hub/internal/hub/delivery/http" hubhttprelivery "source.toby3d.me/toby3d/hub/internal/hub/delivery/http"
hubucase "source.toby3d.me/toby3d/hub/internal/hub/usecase" hubucase "source.toby3d.me/toby3d/hub/internal/hub/usecase"
"source.toby3d.me/toby3d/hub/internal/middleware" "source.toby3d.me/toby3d/hub/internal/middleware"
subscriptionmemoryrepo "source.toby3d.me/toby3d/hub/internal/subscription/repository/memory" subscriptionsqliterepo "source.toby3d.me/toby3d/hub/internal/subscription/repository/sqlite"
subscriptionucase "source.toby3d.me/toby3d/hub/internal/subscription/usecase" subscriptionucase "source.toby3d.me/toby3d/hub/internal/subscription/usecase"
topicmemoryrepo "source.toby3d.me/toby3d/hub/internal/topic/repository/memory" topicsqliterepo "source.toby3d.me/toby3d/hub/internal/topic/repository/sqlite"
topicucase "source.toby3d.me/toby3d/hub/internal/topic/usecase" topicucase "source.toby3d.me/toby3d/hub/internal/topic/usecase"
"source.toby3d.me/toby3d/hub/internal/urlutil" "source.toby3d.me/toby3d/hub/internal/urlutil"
) )
@ -55,23 +57,36 @@ func init() {
func main() { func main() {
ctx := context.Background() ctx := context.Background()
static, err := fs.Sub(static, filepath.Join("web"))
if err != nil {
logger.Fatalln(err)
}
config := new(domain.Config) config := new(domain.Config)
if err := env.ParseWithOptions(config, env.Options{ if err = env.ParseWithOptions(config, env.Options{
Prefix: "HUB_", Prefix: "HUB_",
UseFieldNameByDefault: true, UseFieldNameByDefault: true,
}); err != nil { }); err != nil {
logger.Fatalln(err) logger.Fatalln(err)
} }
static, err := fs.Sub(static, filepath.Join("web")) db, err := sqlx.Open("sqlite", config.DB)
if err != nil {
logger.Fatalf("cannot open database on path %s: %s", config.DB, err)
}
topics, err := topicsqliterepo.NewSQLiteTopicRepository(db)
if err != nil {
logger.Fatalln(err)
}
subscriptions, err := subscriptionsqliterepo.NewSQLiteSubscriptionRepository(db)
if err != nil { if err != nil {
logger.Fatalln(err) logger.Fatalln(err)
} }
client := &http.Client{Timeout: 5 * time.Second} client := &http.Client{Timeout: 5 * time.Second}
matcher := language.NewMatcher(message.DefaultCatalog.Languages()) matcher := language.NewMatcher(message.DefaultCatalog.Languages())
subscriptions := subscriptionmemoryrepo.NewMemorySubscriptionRepository()
topics := topicmemoryrepo.NewMemoryTopicRepository()
topicService := topicucase.NewTopicUseCase(topics, client) topicService := topicucase.NewTopicUseCase(topics, client)
subscriptionService := subscriptionucase.NewSubscriptionUseCase(subscriptions, topics, client) subscriptionService := subscriptionucase.NewSubscriptionUseCase(subscriptions, topics, client)
hubService := hubucase.NewHubUseCase(topics, subscriptions, client, config.BaseURL) hubService := hubucase.NewHubUseCase(topics, subscriptions, client, config.BaseURL)