-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
120 lines (108 loc) · 3.04 KB
/
Copy pathmain.go
File metadata and controls
120 lines (108 loc) · 3.04 KB
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
108
109
110
111
112
113
114
115
116
117
118
119
120
package main
import (
"database/sql"
"fmt"
"log"
"os"
"github.com/aria3ppp/watchlist-server/internal/app"
"github.com/aria3ppp/watchlist-server/internal/auth"
"github.com/aria3ppp/watchlist-server/internal/config"
"github.com/aria3ppp/watchlist-server/internal/hasher"
"github.com/aria3ppp/watchlist-server/internal/repo"
"github.com/aria3ppp/watchlist-server/internal/search"
"github.com/aria3ppp/watchlist-server/internal/server"
"github.com/aria3ppp/watchlist-server/internal/storage"
"github.com/elastic/go-elasticsearch/v8"
"github.com/labstack/echo/v4"
_ "github.com/lib/pq"
"github.com/minio/minio-go/v7"
"github.com/minio/minio-go/v7/pkg/credentials"
"go.uber.org/zap"
"golang.org/x/crypto/bcrypt"
)
func main() {
var logFile *os.File
if config.Config.Server.Production {
var err error
logFile, err = os.Create(config.Config.Server.Logfile)
if err != nil {
log.Panicf(
"failed creating log file %q: %s",
config.Config.Server.Logfile,
err,
)
}
}
logger := newZapLogger(logFile)
defer logger.Sync()
dsn := fmt.Sprintf(
"postgres://%s:%s@%s:%d/%s?sslmode=disable",
config.Config.Postgres.User,
config.Config.Postgres.Password,
config.Config.Postgres.Host,
config.Config.Postgres.Port,
config.Config.Postgres.DB,
)
db, err := sql.Open("postgres", dsn)
if err != nil {
logger.Panic("failed openning databse connection", zap.Error(err))
}
err = db.Ping()
if err != nil {
logger.Panic("failed to ping database connection", zap.Error(err))
}
repository := repo.NewRepository(db)
hasher := hasher.NewBcrypt(bcrypt.DefaultCost)
signingKey, err := auth.ECPrivateKeyFromBase64(
[]byte(config.Config.Auth.ECDSASigningKeyBase64),
logger,
)
if err != nil {
logger.Panic("failed parsing signing key", zap.Error(err))
}
auth := auth.NewAuth(
signingKey,
config.Config.Auth.ExpireInSecs.Jwt,
config.Config.Auth.ExpireInSecs.Refresh,
)
esClient, err := elasticsearch.NewClient(elasticsearch.Config{
Addresses: []string{config.Config.Elasticsearch.Url},
Logger: &esCustomLogger{logger},
})
if err != nil {
logger.Panic("failed creating elasticsearch client", zap.Error(err))
}
searchService, err := search.NewElasticSearch(esClient)
if err != nil {
logger.Panic("failed initializing search service", zap.Error(err))
}
minioClient, err := minio.New(config.Config.MinIO.Url, &minio.Options{
Creds: credentials.NewStaticV4(
config.Config.MinIO.RootUser,
config.Config.MinIO.RootPassword,
"",
),
})
if err != nil {
logger.Panic("failed creating minio client", zap.Error(err))
}
storageService, err := storage.NewMinIO(minioClient)
if err != nil {
logger.Panic("failed initializing storage service", zap.Error(err))
}
application := app.NewApplication(
repository,
auth,
searchService,
hasher,
storageService,
)
server := server.NewServer(
application,
echo.New(),
func(ctx echo.Context, s string) (any, error) { return auth.ParseJwtToken(s) },
echo.MustSubFS(openapiFS, "openapi"),
logger,
)
server.Run(fmt.Sprintf(":%d", config.Config.Server.Port))
}