- settings: get filter choices from storage for settings validation - updater: update servers to the storage - storage: minimal deep copying and data duplication - storage: add merged servers mutex for thread safety - connection: filter servers in storage - formatter: format servers to Markdown in storage - PIA: get server by name from storage directly - Updater: get servers count from storage directly - Updater: equality check done in storage, fix #882
45 lines
1.1 KiB
Go
45 lines
1.1 KiB
Go
// Package storage defines interfaces to interact with the files persisted such as the list of servers.
|
|
package storage
|
|
|
|
import (
|
|
"sync"
|
|
|
|
"github.com/qdm12/gluetun/internal/models"
|
|
)
|
|
|
|
type Storage struct {
|
|
mergedServers models.AllServers
|
|
mergedMutex sync.RWMutex
|
|
// this is stored in memory to avoid re-parsing
|
|
// the embedded JSON file on every call to the
|
|
// SyncServers method.
|
|
hardcodedServers models.AllServers
|
|
logger Infoer
|
|
filepath string
|
|
}
|
|
|
|
type Infoer interface {
|
|
Info(s string)
|
|
}
|
|
|
|
// New creates a new storage and reads the servers from the
|
|
// embedded servers file and the file on disk.
|
|
// Passing an empty filepath disables writing servers to a file.
|
|
func New(logger Infoer, filepath string) (storage *Storage, err error) {
|
|
// A unit test prevents any error from being returned
|
|
// and ensures all providers are part of the servers returned.
|
|
hardcodedServers, _ := parseHardcodedServers()
|
|
|
|
storage = &Storage{
|
|
hardcodedServers: hardcodedServers,
|
|
logger: logger,
|
|
filepath: filepath,
|
|
}
|
|
|
|
if err := storage.syncServers(); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return storage, nil
|
|
}
|