dsfx/dsfx-server/config.go

53 lines
1.0 KiB
Go
Raw Normal View History

2025-03-08 15:07:27 -05:00
package main
import (
"crypto/ecdsa"
"crypto/x509"
"encoding/json"
"encoding/pem"
"fmt"
"os"
)
type Config struct {
Host string `json:"host"`
Port int `json:"port"`
MasterKeyPath string `json:"masterKeyPath"`
DataDirectory string `json:"dataDirectory"`
}
// LoadConfig loads the configuration from the given path.
func LoadConfig(path string) (*Config, error) {
var config Config
configFile, err := os.Open(path)
if err != nil {
return nil, err
}
defer configFile.Close()
err = json.NewDecoder(configFile).Decode(&config)
if err != nil {
return nil, err
}
return &config, nil
}
func LoadMasterKey(path string) (*ecdsa.PrivateKey, error) {
masterKeyFile, err := os.ReadFile(path)
if err != nil {
return nil, err
}
// The second argument is not an error.
derEncoded, _ := pem.Decode(masterKeyFile)
if derEncoded == nil {
return nil, fmt.Errorf("failed to decode master key file")
}
masterKey, err := x509.ParseECPrivateKey(derEncoded.Bytes)
if err != nil {
return nil, err
}
return masterKey, nil
}