Files
tesseract/internal/service/config.go

48 lines
1.0 KiB
Go
Raw Normal View History

2024-11-12 00:31:10 +00:00
package service
import (
"encoding/json"
"io"
"path/filepath"
)
type Config struct {
2024-12-02 17:15:39 +00:00
Port int `json:"port"`
2024-11-12 00:31:10 +00:00
DatabasePath string `json:"databasePath"`
TemplateDirectoryPath string `json:"templateDirectoryPath"`
2024-11-17 18:10:35 +00:00
HostKeyDirectoryPath string `json:"hostKeyDirectoryPath"`
2024-11-12 00:31:10 +00:00
HostName string `json:"hostName"`
2024-12-02 23:03:06 +00:00
Debug bool `json:"debug"`
2024-11-12 00:31:10 +00:00
}
2024-12-02 17:15:39 +00:00
const defaultPort = 8080
2024-11-12 00:31:10 +00:00
func ReadConfigFrom(reader io.Reader) (Config, error) {
var config Config
err := json.NewDecoder(reader).Decode(&config)
if err != nil {
return Config{}, err
}
config.DatabasePath, err = filepath.Abs(config.DatabasePath)
if err != nil {
return Config{}, err
}
config.TemplateDirectoryPath, err = filepath.Abs(config.TemplateDirectoryPath)
if err != nil {
return Config{}, err
}
2024-11-17 18:10:35 +00:00
config.HostKeyDirectoryPath, err = filepath.Abs(config.HostKeyDirectoryPath)
if err != nil {
return Config{}, err
}
2024-12-02 17:15:39 +00:00
if config.Port == 0 {
config.Port = defaultPort
}
2024-11-12 00:31:10 +00:00
return config, nil
}