init
This commit is contained in:
25
internal/adapter/config/app.go
Normal file
25
internal/adapter/config/app.go
Normal file
@ -0,0 +1,25 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/aykhans/oh-my-chat/internal/core/utils"
|
||||
)
|
||||
|
||||
type AppConfig struct {
|
||||
IsDev bool
|
||||
CORSAllowedOrigins string
|
||||
ListenerPort int
|
||||
SecretKey string
|
||||
JWTDuration *time.Duration
|
||||
}
|
||||
|
||||
func NewAppConfig() *AppConfig {
|
||||
return &AppConfig{
|
||||
IsDev: utils.GetEnvOrDefault("APP_IS_PROD", "true") == "true",
|
||||
CORSAllowedOrigins: utils.GetEnvOrDefault("APP_CORS_ALLOWED_ORIGINS", "*"),
|
||||
ListenerPort: Str2IntOrDie(GetEnvOrDie("APP_LISTENER_PORT")),
|
||||
SecretKey: GetEnvOrDie("APP_SECRET_KEY"),
|
||||
JWTDuration: Str2DurationOrDie(GetEnvOrDie("APP_JWT_DURATION")),
|
||||
}
|
||||
}
|
17
internal/adapter/config/config.go
Normal file
17
internal/adapter/config/config.go
Normal file
@ -0,0 +1,17 @@
|
||||
package config
|
||||
|
||||
type ContainerConfig struct {
|
||||
*AppConfig
|
||||
*PostgresConfig
|
||||
*KafkaConfig
|
||||
*ScyllaConfig
|
||||
}
|
||||
|
||||
func NewContainerConfig() *ContainerConfig {
|
||||
return &ContainerConfig{
|
||||
NewAppConfig(),
|
||||
NewPostgresConfig(),
|
||||
NewKafkaConfig(),
|
||||
NewScyllaConfig(),
|
||||
}
|
||||
}
|
44
internal/adapter/config/helper.go
Normal file
44
internal/adapter/config/helper.go
Normal file
@ -0,0 +1,44 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/aykhans/oh-my-chat/internal/adapter/logger"
|
||||
"github.com/aykhans/oh-my-chat/internal/core/utils"
|
||||
)
|
||||
|
||||
var log = logger.NewStdLogger()
|
||||
|
||||
func GetEnvOrDie(key string) string {
|
||||
value := os.Getenv(key)
|
||||
if value == "" {
|
||||
log.Error(
|
||||
"Error get environment variable",
|
||||
"error",
|
||||
fmt.Errorf("Environment variable "+key+" is not set"),
|
||||
)
|
||||
utils.ExitErr()
|
||||
}
|
||||
return value
|
||||
}
|
||||
|
||||
func Str2IntOrDie(value string) int {
|
||||
intValue, err := strconv.Atoi(value)
|
||||
if err != nil {
|
||||
log.Error("Error convert string to int", "error", err)
|
||||
utils.ExitErr()
|
||||
}
|
||||
return intValue
|
||||
}
|
||||
|
||||
func Str2DurationOrDie(value string) *time.Duration {
|
||||
duration, err := time.ParseDuration(value)
|
||||
if err != nil {
|
||||
log.Error("Error convert string to duration", "error", err)
|
||||
utils.ExitErr()
|
||||
}
|
||||
return &duration
|
||||
}
|
45
internal/adapter/config/kafka.go
Normal file
45
internal/adapter/config/kafka.go
Normal file
@ -0,0 +1,45 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/aykhans/oh-my-chat/internal/core/utils"
|
||||
)
|
||||
|
||||
type BootstrapServers []string
|
||||
|
||||
func (b *BootstrapServers) String() string {
|
||||
return strings.Join(*b, ",")
|
||||
}
|
||||
|
||||
type KafkaConfig struct {
|
||||
*KafkaProducerConfig
|
||||
*KafkaConsumerConfig
|
||||
}
|
||||
|
||||
type KafkaProducerConfig struct {
|
||||
BootstrapServers BootstrapServers
|
||||
}
|
||||
|
||||
type KafkaConsumerConfig struct {
|
||||
BootstrapServers BootstrapServers
|
||||
}
|
||||
|
||||
func NewKafkaConfig() *KafkaConfig {
|
||||
return &KafkaConfig{
|
||||
NewKafkaProducerConfig(),
|
||||
NewKafkaConsumerConfig(),
|
||||
}
|
||||
}
|
||||
|
||||
func NewKafkaProducerConfig() *KafkaProducerConfig {
|
||||
return &KafkaProducerConfig{
|
||||
BootstrapServers: utils.Str2StrSlice(GetEnvOrDie("KAFKA_PRODUCER_BOOTSTRAP_SERVERS")),
|
||||
}
|
||||
}
|
||||
|
||||
func NewKafkaConsumerConfig() *KafkaConsumerConfig {
|
||||
return &KafkaConsumerConfig{
|
||||
BootstrapServers: utils.Str2StrSlice(GetEnvOrDie("KAFKA_CONSUMER_BOOTSTRAP_SERVERS")),
|
||||
}
|
||||
}
|
19
internal/adapter/config/postgres.go
Normal file
19
internal/adapter/config/postgres.go
Normal file
@ -0,0 +1,19 @@
|
||||
package config
|
||||
|
||||
type PostgresConfig struct {
|
||||
User string
|
||||
Password string
|
||||
Host string
|
||||
Port string
|
||||
DBName string
|
||||
}
|
||||
|
||||
func NewPostgresConfig() *PostgresConfig {
|
||||
return &PostgresConfig{
|
||||
User: GetEnvOrDie("POSTGRES_USER"),
|
||||
Password: GetEnvOrDie("POSTGRES_PASSWORD"),
|
||||
Host: GetEnvOrDie("POSTGRES_HOST"),
|
||||
Port: GetEnvOrDie("POSTGRES_PORT"),
|
||||
DBName: GetEnvOrDie("POSTGRES_DB"),
|
||||
}
|
||||
}
|
21
internal/adapter/config/scylla.go
Normal file
21
internal/adapter/config/scylla.go
Normal file
@ -0,0 +1,21 @@
|
||||
package config
|
||||
|
||||
import "github.com/aykhans/oh-my-chat/internal/core/utils"
|
||||
|
||||
type ScyllaConfig struct {
|
||||
Hosts []string
|
||||
DataCenter string
|
||||
Keyspace string
|
||||
User string
|
||||
Password string
|
||||
}
|
||||
|
||||
func NewScyllaConfig() *ScyllaConfig {
|
||||
return &ScyllaConfig{
|
||||
Hosts: utils.Str2StrSlice(GetEnvOrDie("SCYLLA_HOSTS")),
|
||||
DataCenter: GetEnvOrDie("SCYLLA_DATACENTER"),
|
||||
Keyspace: GetEnvOrDie("SCYLLA_KEYSPACE"),
|
||||
User: GetEnvOrDie("SCYLLA_USER"),
|
||||
Password: GetEnvOrDie("SCYLLA_PASSWORD"),
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user