Skip to content

Commit

Permalink
feat: add config
Browse files Browse the repository at this point in the history
  • Loading branch information
Q00 committed Dec 8, 2023
1 parent 7e46d77 commit d5ff10b
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 0 deletions.
17 changes: 17 additions & 0 deletions config-dev.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[context]
timeout = 2

[aws]
accessKeyID = "YOUR_ACCESS_KEY"
secretKey = "YOUR_SECRET_KEY"
region = "ap-northeast-2"

[aws.dynamoDB.Room]
tableName = "room-dev"
readCapacityUnits = 1
writeCapacityUnits = 1

[aws.dynamoDB.Chat]
tableName = "chat-dev"
readCapacityUnits = 1
writeCapacityUnits = 1
17 changes: 17 additions & 0 deletions config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[context]
timeout = 2

[aws]
accessKeyID = "YOUR_ACCESS_KEY"
secretKey = "YOUR_SECRET_KEY"
region = "ap-northeast-2"

[aws.dynamoDB.Room]
tableName = "room-dev"
readCapacityUnits = 1
writeCapacityUnits = 1

[aws.dynamoDB.Chat]
tableName = "chat-dev"
readCapacityUnits = 1
writeCapacityUnits = 1
77 changes: 77 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package config

import (
"fmt"
"github.com/spf13/viper"
"log"
"strings"
)

type ServerInfo struct {
Port int64
}

type Context struct {
Timeout int
}

type LogInfo struct {
Level string
}

type PhaseInfo struct {
Level string
}

type AWSInfo struct {
Region string `mapstructure:"region"`
AccessKeyID string `mapstructure:"accessKeyID"`
SecretKey string `mapstructure:"secretKey"`
Dynamo DynamoDB `mapstructure:"dynamoDB"`
}

type DynamoDB struct {
Room DynamoInfo `mapstructure:"room"`
Chat DynamoInfo `mapstructure:"chat"`
}

type DynamoInfo struct {
TableName string `mapstructure:"tableName"`
ReadCapacityUnits int `mapstructure:"readCapacityUnits"`
WriteCapacityUnits int `mapstructure:"writeCapacityUnits"`
}

type Configuration struct {
Phase PhaseInfo `mapstructure:"phase"`
Server ServerInfo `mapstructure:"server"`
Log LogInfo `mapstructure:"logging"`
AWS AWSInfo `mapstructure:"aws"`
}

func loadSingleton() {
viper.SetConfigName("config")
viper.SetConfigType("toml")
viper.AddConfigPath(".")
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
viper.AutomaticEnv()

err := viper.ReadInConfig()
if err != nil {
fmt.Println(err)
log.Fatal("config read error")
panic(err)
}
}

// LoadConfig comment something
func LoadConfig() *Configuration {
loadSingleton()
config := &Configuration{}
err := viper.Unmarshal(&config)
if err != nil {
log.Fatal("config unmarshal error")
panic(err)
}

return config
}

0 comments on commit d5ff10b

Please sign in to comment.