Moved config module to utils package
This commit is contained in:
parent
2d6d9c3959
commit
5888a7821b
4
main.go
4
main.go
|
@ -9,10 +9,12 @@ import (
|
||||||
|
|
||||||
"github.com/oschwald/geoip2-golang"
|
"github.com/oschwald/geoip2-golang"
|
||||||
"github.com/thoj/go-ircevent"
|
"github.com/thoj/go-ircevent"
|
||||||
|
"irc_bot/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
config, err := LoadConfig("config.json")
|
// Load configuration
|
||||||
|
config, err := utils.LoadConfig("config.json")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Error loading config: %v", err)
|
log.Fatalf("Error loading config: %v", err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,8 +1,9 @@
|
||||||
package main
|
package utils
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"os"
|
"os"
|
||||||
|
"fmt"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
|
@ -15,18 +16,18 @@ type Config struct {
|
||||||
GeoIPASN string `json:"geoip_asn"`
|
GeoIPASN string `json:"geoip_asn"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func LoadConfig(filePath string) (*Config, error) {
|
func LoadConfig(file string) (Config, error) {
|
||||||
file, err := os.Open(filePath)
|
var config Config
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
defer file.Close()
|
|
||||||
|
|
||||||
config := &Config{}
|
configFile, err := os.Open(file)
|
||||||
decoder := json.NewDecoder(file)
|
|
||||||
err = decoder.Decode(config)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return config, fmt.Errorf("error opening config file: %v", err)
|
||||||
|
}
|
||||||
|
defer configFile.Close()
|
||||||
|
|
||||||
|
err = json.NewDecoder(configFile).Decode(&config)
|
||||||
|
if err != nil {
|
||||||
|
return config, fmt.Errorf("error decoding config file: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return config, nil
|
return config, nil
|
Loading…
Reference in New Issue