-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.go
218 lines (179 loc) · 6.3 KB
/
app.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package application
import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/guregu/dynamo"
"github.com/rabbitmq/amqp091-go"
"github.com/veedubyou/chord-paper-be/src/shared/config"
dynamolib "github.com/veedubyou/chord-paper-be/src/shared/lib/dynamo"
"github.com/veedubyou/chord-paper-be/src/shared/lib/rabbitmq"
trackentity "github.com/veedubyou/chord-paper-be/src/shared/track/entity"
trackstorage "github.com/veedubyou/chord-paper-be/src/shared/track/storage"
filestore "github.com/veedubyou/chord-paper-be/src/worker/internal/application/cloud_storage/store"
"github.com/veedubyou/chord-paper-be/src/worker/internal/application/executor"
"github.com/veedubyou/chord-paper-be/src/worker/internal/application/jobs/job_router"
"github.com/veedubyou/chord-paper-be/src/worker/internal/application/jobs/save_stems_to_db"
"github.com/veedubyou/chord-paper-be/src/worker/internal/application/jobs/split"
"github.com/veedubyou/chord-paper-be/src/worker/internal/application/jobs/split/splitter"
"github.com/veedubyou/chord-paper-be/src/worker/internal/application/jobs/split/splitter/file_splitter"
"github.com/veedubyou/chord-paper-be/src/worker/internal/application/jobs/start"
"github.com/veedubyou/chord-paper-be/src/worker/internal/application/jobs/transfer"
"github.com/veedubyou/chord-paper-be/src/worker/internal/application/jobs/transfer/download"
"github.com/veedubyou/chord-paper-be/src/worker/internal/application/worker"
"github.com/veedubyou/chord-paper-be/src/worker/internal/lib/cerr"
"github.com/veedubyou/chord-paper-be/src/worker/internal/lib/storagepath"
"google.golang.org/api/option"
"os"
)
func must[T any](t T, err error) T {
if err != nil {
panic(err)
}
return t
}
type App struct {
worker worker.QueueWorker
}
type Config struct {
RabbitMQURL string
RabbitMQQueueName string
DynamoConfig config.Dynamo
CloudStorageConfig config.CloudStorage
YoutubeDLBinPath string
YoutubeDLWorkingDirPath string
SpleeterBinPath string
SpleeterWorkingDirPath string
}
func NewApp(config Config) App {
consumerConn := must(amqp091.Dial(config.RabbitMQURL))
return App{
worker: newWorker(config, consumerConn),
}
}
func (a *App) Start() error {
err := a.worker.Start()
if err != nil {
return cerr.Wrap(err).Error("Failed to start worker")
}
return nil
}
func (a *App) Stop() {
a.worker.Stop()
}
func newWorker(config Config, consumerConn *amqp091.Connection) worker.QueueWorker {
publisher := newPublisher(config)
trackStore := trackstorage.NewDB(newDynamoDB(config.DynamoConfig))
queueWorker := must(worker.NewQueueWorkerFromConnection(
consumerConn,
config.RabbitMQQueueName,
newJobRouter(config, trackStore, publisher)))
return queueWorker
}
func newPublisher(config Config) *rabbitmq.QueuePublisher {
return rabbitmq.NewQueuePublisher(config.RabbitMQURL, config.RabbitMQQueueName)
}
func newDynamoDB(dynamoConfig config.Dynamo) dynamolib.DynamoDBWrapper {
dbSession := session.Must(session.NewSession())
var dbConfig *aws.Config
switch t := dynamoConfig.(type) {
case config.ProdDynamo:
dbConfig = aws.NewConfig().
WithCredentials(credentials.NewStaticCredentials(
t.AccessKeyID,
t.SecretAccessKey,
"",
)).
WithRegion(t.Region)
case config.LocalDynamo:
dbConfig = aws.NewConfig().
WithCredentials(credentials.NewStaticCredentials(
t.AccessKeyID,
t.SecretAccessKey,
"",
)).
WithRegion(t.Region).
WithEndpoint(t.Host)
default:
panic("Unexpected dynamo config type")
}
return dynamolib.DynamoDBWrapper{
DB: dynamo.New(dbSession, dbConfig),
}
}
func newGoogleFileStore(cloudStorageConfig config.CloudStorage) filestore.GoogleFileStore {
switch t := cloudStorageConfig.(type) {
case config.ProdCloudStorage:
return must(filestore.NewGoogleFileStore(
t.StorageHost,
option.WithCredentialsJSON([]byte(t.SecretKey)),
))
case config.LocalCloudStorage:
return must(filestore.NewGoogleFileStore(
t.StorageHost,
option.WithEndpoint(t.HostEndpoint),
option.WithAPIKey("fake_api_key"),
))
default:
panic("Unrecognized cloud storage config")
}
}
func newJobRouter(config Config, trackStore trackentity.Store, publisher rabbitmq.Publisher) job_router.JobRouter {
pathGenerator := storagepath.Generator{
Host: config.CloudStorageConfig.GetStorageHost(),
Bucket: config.CloudStorageConfig.GetBucket(),
}
return job_router.NewJobRouter(
trackStore,
publisher,
newStartJobHandler(trackStore),
newDownloadJobHandler(config, pathGenerator),
newSplitJobHandler(config, pathGenerator),
newSaveToDBJobHandler(trackStore))
}
func newStartJobHandler(trackStore trackentity.Store) start.JobHandler {
return start.NewJobHandler(trackStore)
}
func newDownloadJobHandler(config Config, pathGenerator storagepath.Generator) transfer.JobHandler {
if err := os.MkdirAll(config.YoutubeDLWorkingDirPath, os.ModePerm); err != nil {
panic(err)
}
youtubedler := download.NewYoutubeDLer(config.YoutubeDLBinPath, executor.BinaryFileExecutor{})
genericdler := download.NewGenericDLer()
selectdler := download.NewSelectDLer(youtubedler, genericdler)
trackStore := trackstorage.NewDB(newDynamoDB(config.DynamoConfig))
trackDownloader := must(transfer.NewTrackTransferrer(
selectdler,
trackStore,
newGoogleFileStore(config.CloudStorageConfig),
pathGenerator,
config.YoutubeDLWorkingDirPath,
))
return transfer.NewJobHandler(trackDownloader)
}
func newSplitJobHandler(config Config, pathGenerator storagepath.Generator) split.JobHandler {
if err := os.MkdirAll(config.SpleeterWorkingDirPath, os.ModePerm); err != nil {
panic(err)
}
localUsecase := must(file_splitter.NewLocalFileSplitter(
config.SpleeterWorkingDirPath,
config.SpleeterBinPath,
executor.BinaryFileExecutor{},
))
googleFileStore := newGoogleFileStore(config.CloudStorageConfig)
remoteUsecase := must(file_splitter.NewRemoteFileSplitter(
config.SpleeterWorkingDirPath,
googleFileStore,
localUsecase,
))
trackStore := trackstorage.NewDB(newDynamoDB(config.DynamoConfig))
songSplitUsecase := splitter.NewTrackSplitter(
remoteUsecase,
trackStore,
pathGenerator,
)
return split.NewJobHandler(songSplitUsecase)
}
func newSaveToDBJobHandler(trackStore trackentity.Store) save_stems_to_db.JobHandler {
return save_stems_to_db.NewJobHandler(trackStore)
}