new: Add parameters to specify the configuration file

This commit is contained in:
2024-12-19 16:27:24 +08:00
parent a646b3cb4f
commit 50c365a96a
2 changed files with 30 additions and 27 deletions

View File

@@ -1,13 +1,5 @@
package config
import (
"io"
"log"
"os"
"gopkg.in/yaml.v3"
)
type MConfig struct {
Host string `yaml:"host"`
Port int `yaml:"port"`
@@ -21,21 +13,3 @@ type MConfig struct {
}
var MConfigInstance *MConfig
func init() {
configFile, err := os.OpenFile("config.yaml", os.O_RDONLY, os.ModePerm)
if err != nil {
log.Fatal(err)
}
defer configFile.Close()
configByte, err := io.ReadAll(configFile)
if err != nil {
log.Fatal(err)
}
err = yaml.Unmarshal(configByte, &MConfigInstance)
if err != nil {
log.Fatal(err)
}
}

31
main.go
View File

@@ -1,12 +1,41 @@
package main
import (
"flag"
"gopkg.in/yaml.v3"
"io"
"log"
"os"
"MollyBlog/config"
"MollyBlog/internal/mApp"
"MollyBlog/utils"
)
func init() {
// parse arguments
configFilePath := flag.String("c", "config.yaml", "config file")
flag.Parse()
// load config
configFile, err := os.OpenFile(*configFilePath, os.O_RDONLY, os.ModePerm)
if err != nil {
log.Fatal(err)
}
defer configFile.Close()
configByte, err := io.ReadAll(configFile)
if err != nil {
log.Fatal(err)
}
err = yaml.Unmarshal(configByte, &config.MConfigInstance)
if err != nil {
log.Fatal(err)
}
// create need files
postSrc := config.MConfigInstance.Storage.SRC
postDst := config.MConfigInstance.Storage.DST
utils.Mkdir(postSrc)
@@ -20,5 +49,5 @@ func init() {
func main() {
mapp := mApp.NewMApp(config.MConfigInstance)
mapp.Run(true)
mapp.Run(false)
}