This repository has been archived by the owner on May 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
107 lines (90 loc) · 3.05 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
//go:generate go install github.com/valyala/quicktemplate/qtc@latest
//go:generate qtc -dir=web
//go:generate go install golang.org/x/text/cmd/gotext@latest
//go:generate gotext -srclang=en update -out=catalog_gen.go -lang=en,ru
package main
import (
"context"
"embed"
"io/fs"
"log"
"net/http"
"os"
"path/filepath"
"time"
"github.com/caarlos0/env/v10"
"github.com/jmoiron/sqlx"
"golang.org/x/text/language"
"golang.org/x/text/message"
_ "modernc.org/sqlite"
"source.toby3d.me/toby3d/hub/internal/domain"
hubhttprelivery "source.toby3d.me/toby3d/hub/internal/hub/delivery/http"
hubucase "source.toby3d.me/toby3d/hub/internal/hub/usecase"
"source.toby3d.me/toby3d/hub/internal/middleware"
subscriptionsqliterepo "source.toby3d.me/toby3d/hub/internal/subscription/repository/sqlite"
subscriptionucase "source.toby3d.me/toby3d/hub/internal/subscription/usecase"
topicsqliterepo "source.toby3d.me/toby3d/hub/internal/topic/repository/sqlite"
topicucase "source.toby3d.me/toby3d/hub/internal/topic/usecase"
"source.toby3d.me/toby3d/hub/internal/urlutil"
)
var logger = log.New(os.Stdout, "hub\t", log.LstdFlags)
//go:embed web/static/*
var static embed.FS
func main() {
ctx := context.Background()
static, err := fs.Sub(static, filepath.Join("web"))
if err != nil {
logger.Fatalln(err)
}
config := new(domain.Config)
if err = env.ParseWithOptions(config, env.Options{
Prefix: "HUB_",
UseFieldNameByDefault: true,
}); err != nil {
logger.Fatalln(err)
}
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 {
logger.Fatalln(err)
}
client := &http.Client{Timeout: 5 * time.Second}
matcher := language.NewMatcher(message.DefaultCatalog.Languages())
topicService := topicucase.NewTopicUseCase(topics, client)
subscriptionService := subscriptionucase.NewSubscriptionUseCase(subscriptions, topics, client)
hubService := hubucase.NewHubUseCase(topics, subscriptions, client, config.BaseURL)
handler := hubhttprelivery.NewHandler(hubhttprelivery.NewHandlerParams{
Hub: hubService,
Subscriptions: subscriptionService,
Topics: topicService,
Matcher: matcher,
Name: config.Name,
})
server := &http.Server{
Addr: config.Bind,
Handler: http.HandlerFunc(middleware.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
head, _ := urlutil.ShiftPath(r.URL.Path)
switch head {
case "":
handler.ServeHTTP(w, r)
case "static":
http.FileServer(http.FS(static)).ServeHTTP(w, r)
}
}).Intercept(middleware.LogFmt())),
ReadTimeout: 5 * time.Second,
WriteTimeout: 5 * time.Second,
ErrorLog: logger,
}
go hubService.ListenAndServe(ctx)
logger.Printf("started %s on %s: %s", config.Name, config.Bind, config.BaseURL.String())
if err = server.ListenAndServe(); err != nil {
logger.Fatalln(err)
}
}