feat: load config from file
This commit is contained in:
@@ -1,16 +1,49 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
NetInterface string
|
||||
Htdocs string
|
||||
Profile bool
|
||||
NetInterface string `json:"net_interface"`
|
||||
Htdocs string `json:"htdocs"`
|
||||
Profile bool `json:"profile"`
|
||||
}
|
||||
|
||||
var static_config *Config = nil
|
||||
|
||||
func GetConfig() *Config {
|
||||
//TODO: Load config from file
|
||||
return &Config{
|
||||
NetInterface: "127.0.0.1:80",
|
||||
Htdocs: "./htdocs",
|
||||
Profile: false,
|
||||
if static_config != nil {
|
||||
return static_config
|
||||
}
|
||||
|
||||
file, err := os.Open("config.json")
|
||||
if err != nil {
|
||||
fmt.Println("No config file found")
|
||||
return &Config{
|
||||
NetInterface: "127.0.0.1:80",
|
||||
Htdocs: "./htdocs",
|
||||
Profile: false,
|
||||
}
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
var config Config
|
||||
err = json.NewDecoder(file).Decode(&config)
|
||||
if err != nil {
|
||||
fmt.Println("Error decoding config file:", err)
|
||||
return &Config{
|
||||
NetInterface: "127.0.0.1:80",
|
||||
Htdocs: "./htdocs",
|
||||
Profile: false,
|
||||
}
|
||||
}
|
||||
static_config = &config
|
||||
return &config
|
||||
}
|
||||
|
||||
func ResetConfig() {
|
||||
static_config = nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user