-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
66 lines (55 loc) · 1.44 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
package main
import (
"fmt"
"log/slog"
"net/http"
"os"
"time"
httptools "github.com/XDoubleU/essentia/pkg/communication/http"
"github.com/XDoubleU/essentia/pkg/database/postgres"
"github.com/XDoubleU/essentia/pkg/logging"
sentrytools "github.com/XDoubleU/essentia/pkg/sentry"
)
type application struct {
logger *slog.Logger
config Config
db postgres.DB
}
func NewApp(logger *slog.Logger, config Config, db postgres.DB) application {
spandb := postgres.NewSpanDB(db)
return application{
logger: logger,
config: config,
db: spandb,
}
}
func main() {
cfg := NewConfig(slog.New(slog.NewTextHandler(os.Stdout, nil)))
logger := slog.New(
sentrytools.NewLogHandler(cfg.Env, slog.NewTextHandler(os.Stdout, nil)),
)
db, err := postgres.Connect(
logger,
cfg.DBDsn,
25, //nolint:mnd //no magic number
"15m",
30, //nolint:mnd //no magic number
30*time.Second, //nolint:mnd //no magic number
5*time.Minute, //nolint:mnd //no magic number
)
if err != nil {
panic(err)
}
app := NewApp(logger, cfg, db)
srv := &http.Server{
Addr: fmt.Sprintf(":%d", app.config.Port),
Handler: app.Routes(),
IdleTimeout: time.Minute,
ReadTimeout: 5 * time.Second, //nolint:gomnd //no magic number
WriteTimeout: 10 * time.Second, //nolint:gomnd //no magic number
}
err = httptools.Serve(logger, srv, app.config.Env)
if err != nil {
logger.Error("failed to serve server", logging.ErrAttr(err))
}
}