feat: load config from file
This commit is contained in:
@@ -1,16 +1,49 @@
|
|||||||
package config
|
package config
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
)
|
||||||
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
NetInterface string
|
NetInterface string `json:"net_interface"`
|
||||||
Htdocs string
|
Htdocs string `json:"htdocs"`
|
||||||
Profile bool
|
Profile bool `json:"profile"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var static_config *Config = nil
|
||||||
|
|
||||||
func GetConfig() *Config {
|
func GetConfig() *Config {
|
||||||
//TODO: Load config from file
|
if static_config != nil {
|
||||||
return &Config{
|
return static_config
|
||||||
NetInterface: "127.0.0.1:80",
|
|
||||||
Htdocs: "./htdocs",
|
|
||||||
Profile: false,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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