indieauth-server/internal/config/config.go
Dan Anglin 87e7c90f1e
All checks were successful
CI / Tests (pull_request) Successful in 14s
CI / Style (pull_request) Successful in 9s
use filepath.Clean to fix gosec G304
2024-10-15 17:20:23 +01:00

38 lines
611 B
Go

package config
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
)
type Config struct {
BindAddress string `json:"bindAddress"`
Port int32 `json:"port"`
Domain string `json:"domain"`
}
func NewConfig(path string) (Config, error) {
path = filepath.Clean(path)
data, err := os.ReadFile(path)
if err != nil {
return Config{}, fmt.Errorf(
"unable to read the config from %q: %w",
path,
err,
)
}
var cfg Config
if err := json.Unmarshal(data, &cfg); err != nil {
return Config{}, fmt.Errorf(
"unable to decode the JSON data: %w",
err,
)
}
return cfg, nil
}