allow missing urns and fingers files, if default

This commit is contained in:
Gustavo Maronato 2023-09-20 01:40:57 -03:00
parent 8ad2fd2fd6
commit c16b039d3f
No known key found for this signature in database
4 changed files with 27 additions and 16 deletions

View file

@ -15,7 +15,7 @@ const (
// DefaultURNPath is the default file path to the URN alias file. // DefaultURNPath is the default file path to the URN alias file.
DefaultURNPath = "urns.yml" DefaultURNPath = "urns.yml"
// DefaultFingerPath is the default file path to the webfinger definition file. // DefaultFingerPath is the default file path to the webfinger definition file.
DefaultFingerPath = "finger.yml" DefaultFingerPath = "fingers.yml"
) )
// ErrInvalidConfig is returned when the config is invalid. // ErrInvalidConfig is returned when the config is invalid.

View file

@ -59,14 +59,14 @@ func TestConfig_Validate(t *testing.T) {
name: "empty host", name: "empty host",
cfg: &config.Config{ cfg: &config.Config{
Host: "", Host: "",
Port: "1234", Port: config.DefaultPort,
}, },
wantErr: true, wantErr: true,
}, },
{ {
name: "empty port", name: "empty port",
cfg: &config.Config{ cfg: &config.Config{
Host: "example.com", Host: config.DefaultHost,
Port: "", Port: "",
}, },
wantErr: true, wantErr: true,
@ -74,7 +74,7 @@ func TestConfig_Validate(t *testing.T) {
{ {
name: "invalid addr", name: "invalid addr",
cfg: &config.Config{ cfg: &config.Config{
Host: "example.com", Host: config.DefaultHost,
Port: "invalid", Port: "invalid",
}, },
wantErr: true, wantErr: true,
@ -82,8 +82,8 @@ func TestConfig_Validate(t *testing.T) {
{ {
name: "empty urn path", name: "empty urn path",
cfg: &config.Config{ cfg: &config.Config{
Host: "example.com", Host: config.DefaultHost,
Port: "1234", Port: config.DefaultPort,
URNPath: "", URNPath: "",
}, },
wantErr: true, wantErr: true,
@ -91,9 +91,9 @@ func TestConfig_Validate(t *testing.T) {
{ {
name: "empty finger path", name: "empty finger path",
cfg: &config.Config{ cfg: &config.Config{
Host: "example.com", Host: config.DefaultHost,
Port: "1234", Port: config.DefaultPort,
URNPath: "urns.yml", URNPath: config.DefaultURNPath,
FingerPath: "", FingerPath: "",
}, },
wantErr: true, wantErr: true,
@ -101,10 +101,10 @@ func TestConfig_Validate(t *testing.T) {
{ {
name: "valid", name: "valid",
cfg: &config.Config{ cfg: &config.Config{
Host: "example.com", Host: config.DefaultHost,
Port: "1234", Port: config.DefaultPort,
URNPath: "urns.yml", URNPath: config.DefaultURNPath,
FingerPath: "finger.yml", FingerPath: config.DefaultFingerPath,
}, },
wantErr: false, wantErr: false,
}, },

View file

@ -60,7 +60,7 @@ func TestStartServer(t *testing.T) {
t.Run("fails to start", func(t *testing.T) { t.Run("fails to start", func(t *testing.T) {
t.Parallel() t.Parallel()
ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond*100) ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond*200)
defer cancel() defer cancel()
cfg := config.NewConfig() cfg := config.NewConfig()

View file

@ -42,9 +42,15 @@ func NewFingerReader() *FingerReader {
func (f *FingerReader) ReadFiles(cfg *config.Config) error { func (f *FingerReader) ReadFiles(cfg *config.Config) error {
// Read URNs file // Read URNs file
file, err := os.ReadFile(cfg.URNPath) file, err := os.ReadFile(cfg.URNPath)
if err != nil { if err != nil {
return fmt.Errorf("error opening URNs file: %w", err) // If the file does not exist and the path is the default, set the URNs to an empty map
if os.IsNotExist(err) && cfg.URNPath == config.DefaultURNPath {
f.URNSFile = []byte("")
} else {
return fmt.Errorf("error opening URNs file: %w", err)
}
} }
f.URNSFile = file f.URNSFile = file
@ -52,7 +58,12 @@ func (f *FingerReader) ReadFiles(cfg *config.Config) error {
// Read fingers file // Read fingers file
file, err = os.ReadFile(cfg.FingerPath) file, err = os.ReadFile(cfg.FingerPath)
if err != nil { if err != nil {
return fmt.Errorf("error opening fingers file: %w", err) // If the file does not exist and the path is the default, set the fingers to an empty map
if os.IsNotExist(err) && cfg.FingerPath == config.DefaultFingerPath {
f.FingersFile = []byte("")
} else {
return fmt.Errorf("error opening fingers file: %w", err)
}
} }
f.FingersFile = file f.FingersFile = file