init push
This commit is contained in:
80
cmd/worker/main.go
Normal file
80
cmd/worker/main.go
Normal file
@@ -0,0 +1,80 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"wucher/internal/config"
|
||||
)
|
||||
|
||||
const (
|
||||
workerJobEnvKey = "WORKER_JOB"
|
||||
workerJobEmail = "email"
|
||||
workerJobFileProcessing = "file_processing"
|
||||
workerJobAliasMail = "mail"
|
||||
workerJobAliasEmailWorker = "email_worker"
|
||||
workerJobAliasFile = "file"
|
||||
workerJobAliasFileWorker = "file_worker"
|
||||
)
|
||||
|
||||
var (
|
||||
workerRunFn = run
|
||||
workerFatalFn = log.Fatal
|
||||
runMailWorkerFn func(config.AppConfig) error = runMailWorker
|
||||
runFileWorkerFn func(config.AppConfig) error = runFileWorker
|
||||
)
|
||||
|
||||
func configFilePath() string {
|
||||
if v := os.Getenv("CONFIG_FILE"); v != "" {
|
||||
return v
|
||||
}
|
||||
basePath := os.Getenv("CONFIG_BASE_PATH")
|
||||
if basePath == "" {
|
||||
basePath = "/tmp"
|
||||
}
|
||||
filename := os.Getenv("CONFIG_FILENAME")
|
||||
if filename == "" {
|
||||
filename = "config.generated.json"
|
||||
}
|
||||
return basePath + "/" + filename
|
||||
}
|
||||
|
||||
func main() {
|
||||
if err := workerRunFn(); err != nil {
|
||||
workerFatalFn(err)
|
||||
}
|
||||
}
|
||||
|
||||
func run() error {
|
||||
appCfg, err := config.LoadFromFile(configFilePath())
|
||||
if err != nil {
|
||||
return fmt.Errorf("load config: %w", err)
|
||||
}
|
||||
|
||||
job := normalizeWorkerJob(os.Getenv(workerJobEnvKey))
|
||||
|
||||
switch job {
|
||||
case workerJobEmail:
|
||||
return runMailWorkerFn(appCfg)
|
||||
case workerJobFileProcessing:
|
||||
return runFileWorkerFn(appCfg)
|
||||
default:
|
||||
return errors.New("unsupported worker job: " + job)
|
||||
}
|
||||
}
|
||||
|
||||
func normalizeWorkerJob(raw string) string {
|
||||
job := strings.ToLower(strings.TrimSpace(raw))
|
||||
|
||||
switch job {
|
||||
case "", workerJobEmail, workerJobAliasMail, workerJobAliasEmailWorker:
|
||||
return workerJobEmail
|
||||
case workerJobFileProcessing, workerJobAliasFile, workerJobAliasFileWorker:
|
||||
return workerJobFileProcessing
|
||||
default:
|
||||
return job
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user