Skip to content

Commit

Permalink
🔧 Config 로직 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
aroxu committed Jun 9, 2023
1 parent b3f7e79 commit ad22bbd
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 37 deletions.
86 changes: 57 additions & 29 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,50 +11,71 @@ import (

var ConfigContent Config

const configTemplate = `{
"server": "https://clip.aroxu.me/download?mc_version=1.19.4",
"debug": false,
"debug_port": 5005,
"restart": true,
"memory": 4,
"api_port": 8080,
"plugin_api_port": 8081,
"plugins": [
"https://github.com/monun/auto-reloader/releases/download/0.0.6/auto-reloader-0.0.6.jar"
],
"jarArgs": ["nogui"]
}`
const configFileName = "launcher.conf.json"

const (
defaultConfigVersion = 1
defaultServer = "https://clip.aroxu.me/download?mc_version=1.19.4"
defaultDebug = false
defaultDebugPort = 5005
defaultRestart = true
defaultMemory = 2
defaultAPIPort = 8080
defaultPluginPort = 8081
)

var (
defaultPlugins = []string{}
defaultJarArgs = []string{"nogui"}
defaultWebConsoleDisabledCmds = []string{}
)

type Config struct {
Server string `json:"server"`
Debug bool `json:"debug"`
DebugPort int `json:"debug_port"`
Restart bool `json:"restart"`
Memory int `json:"memory"`
APIPort int `json:"api_port"`
PluginPort int `json:"plugin_api_port"`
Plugins []string `json:"plugins"`
JarArgs []string `json:"jarArgs"`
ConfigVersion int `json:"config_version"`
Server string `json:"server"`
Debug bool `json:"debug"`
DebugPort int `json:"debug_port"`
Restart bool `json:"restart"`
Memory int `json:"memory"`
APIPort int `json:"api_port"`
PluginPort int `json:"plugin_api_port"`
Plugins []string `json:"plugins"`
JarArgs []string `json:"jarArgs"`
WebConsoleDisabledCmds []string `json:"webconsole_disabled_cmds"`
}

func LoadConfig() Config {
var config Config
defaultConfig := Config{
ConfigVersion: defaultConfigVersion,
Server: defaultServer,
Debug: defaultDebug,
DebugPort: defaultDebugPort,
Restart: defaultRestart,
Memory: defaultMemory,
APIPort: defaultAPIPort,
PluginPort: defaultPluginPort,
Plugins: defaultPlugins,
JarArgs: defaultJarArgs,
WebConsoleDisabledCmds: defaultWebConsoleDisabledCmds,
}
currentPath, _ := os.Getwd()
configPath := currentPath + "/launcher.conf.json"
configPath := currentPath + "/" + configFileName

if _, err := os.Stat(configPath); os.IsNotExist(err) {
logger.Warn(i18n.Get("config.notfound"))
generateConfig()
saveConfig(defaultConfig)
}

configData, loadFileErr := os.ReadFile(configPath)
if loadFileErr != nil {
logger.Error(strings.ReplaceAll(i18n.Get("config.loaderror"), "$error", loadFileErr.Error()))
}

if strings.TrimSpace(string(configData)) == "" {
logger.Error(i18n.Get("config.empty"))
configData = []byte(configTemplate)
generateConfig()
saveConfig(defaultConfig)
return defaultConfig
}

loadConfigErr := json.Unmarshal([]byte(configData), &config)
Expand All @@ -76,20 +97,27 @@ func LoadConfig() Config {
if config.Memory < 2 {
logger.Fatal(i18n.Get("config.memory.invalid"))
}
if config.ConfigVersion != defaultConfigVersion {

logger.Warn(i18n.Get("config.version.different"))

}
}
return config
}

func generateConfig() {
serverConfFile, errGenConf := os.Create("launcher.conf.json")
func saveConfig(data Config) {
file, _ := json.MarshalIndent(data, "", " ")

serverConfFile, errGenConf := os.Create(configFileName)

if errGenConf != nil {
logger.Fatal(strings.ReplaceAll(i18n.Get("config.create_failed"), "$error", errGenConf.Error()))
}

defer serverConfFile.Close()

_, errWrtConf := serverConfFile.WriteString(configTemplate)
_, errWrtConf := serverConfFile.Write(file)

if errWrtConf != nil {
logger.Fatal(strings.ReplaceAll(i18n.Get("config.write_failed"), "$error", errWrtConf.Error()))
Expand Down
17 changes: 9 additions & 8 deletions i18n/en_US.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,15 @@ var en_US map[string]string = map[string]string{
"version.info": "MC-Dashify launcher v.$version",

// WebConsole
"webconsole.started1": "+----------------------------+",
"webconsole.started2": "| WebConsole Server Started! |",
"webconsole.chk.valid.prev.connection": "[WebConsole] Checking Valid Previous Connections...",
"webconsole.restoring.prev.connection": "[WebConsole] Restoring Previous Connection: $connection",
"webconsole.connection.closed": "[WebConsole] Connection Closed: $connection",
"webconsole.connection.closed.error": "[WebConsole] Connection from $remote closed due to following error: $error",
"webconsole.started1": "+----------------------------+",
"webconsole.started2": "| WebConsole Server Started! |",
"webconsole.chk.valid.prev.connection": "[WebConsole] Checking Valid Previous Connections...",
"webconsole.restoring.prev.connection": "[WebConsole] Restoring Previous Connection: $connection",
"webconsole.connection.closed": "[WebConsole] Connection Closed: $connection",
"webconsole.connection.closed.error": "[WebConsole] Connection from $remote closed due to following error: $error",
"webconsole.connection.close.msg.send.fail": "[WebConsole] Failed to send connection close message to $remote. Error detail: $error",
"webconsole.connection.opened": "[WebConsole] Connection Opened: $connection",
"webconsole.connection.cmd.received": "[WebConsole] FROM $remote CMD: $command",
"webconsole.connection.opened": "[WebConsole] Connection Opened: $connection",
"webconsole.connection.cmd.received": "[WebConsole] FROM $remote CMD: $command",

// Config
"config.notfound": "Config file not found. Creating new one...",
Expand All @@ -70,6 +70,7 @@ var en_US map[string]string = map[string]string{
"config.create_failed": "Failed to create config file. Please check your permission. Error detail: $error",
"config.write_failed": "Failed to write config file. Please check your permission. Error detail: $error",
"config.created": "Config file created successfully.",
"config.version.different": "Config file version is different. Consider updating your config file.",
"config.server.empty": "Server file path or URL is empty.",
"config.memory.invalid": "Invalid memory option detected. Memory is set in GB and requires at least 2 GB. Please check the config file.",
"config.debug_port.invalid": "Invalid JVM debugging port settings found. Please check the config file.",
Expand Down
1 change: 1 addition & 0 deletions i18n/ko_KR.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ var ko_KR map[string]string = map[string]string{
"config.create_failed": "구성 설정 파일을 생성할 수 없습니다. 권한을 확인해주세요. 자세한 오류 내용은 다음과 같습니다: $error",
"config.write_failed": "구성 설정 파일을 작성할 수 없습니다. 권한을 확인해주세요. 자세한 오류 내용은 다음과 같습니다: $error",
"config.created": "구성 설정 파일을 성공적으로 생성하였습니다.",
"config.version.different": "구성 설정 버전이 다릅니다. 구성 설정 파일을 업데이트 할 것을 권장합니다.",
"config.server.empty": "서버 파일의 경로나 URL이 비어있습니다.",
"config.memory.invalid": "올바르지 않은 메모리 설정을 발견하였습니다. 메모리는 GB 단위로 설정되며, 최소 2GB 이상이 필요합니다. 구성 설정 파일을 확인해주세요.",
"config.debug_port.invalid": "올바르지 않은 JVM 디버깅 포트 설정을 발견하였습니다. 구성 설정 파일을 확인해주세요.",
Expand Down

0 comments on commit ad22bbd

Please sign in to comment.