Improve error handling while loading
This commit is contained in:
parent
ab673a0f2d
commit
8493175f4c
|
@ -116,7 +116,11 @@ func NewApp(options AppOptions) (*App, error) {
|
||||||
options: options,
|
options: options,
|
||||||
}
|
}
|
||||||
|
|
||||||
app.backend = loadMemoryBackend(options.pool)
|
backend, err := loadMemoryBackend(options.pool)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
app.backend = backend
|
||||||
app.backend.AuthEnabled = options.AuthEnabled
|
app.backend.AuthEnabled = options.AuthEnabled
|
||||||
app.backend.baseURL = options.BaseURL
|
app.backend.baseURL = options.BaseURL
|
||||||
app.backend.hubIncomingBackend.pool = options.pool
|
app.backend.hubIncomingBackend.pool = options.pool
|
||||||
|
@ -199,7 +203,10 @@ func main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
if createBackend {
|
if createBackend {
|
||||||
createMemoryBackend()
|
err := createMemoryBackend()
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Error while saving backend.json: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
// TODO(peter): automatically gather this information from login or otherwise
|
// TODO(peter): automatically gather this information from login or otherwise
|
||||||
log.Println(`Config file "backend.json" is created in the current directory.`)
|
log.Println(`Config file "backend.json" is created in the current directory.`)
|
||||||
|
|
|
@ -15,6 +15,7 @@ import (
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/pkg/errors"
|
||||||
"p83.nl/go/ekster/pkg/auth"
|
"p83.nl/go/ekster/pkg/auth"
|
||||||
"p83.nl/go/ekster/pkg/fetch"
|
"p83.nl/go/ekster/pkg/fetch"
|
||||||
"p83.nl/go/ekster/pkg/microsub"
|
"p83.nl/go/ekster/pkg/microsub"
|
||||||
|
@ -90,7 +91,7 @@ func (b *memoryBackend) load() error {
|
||||||
filename := "backend.json"
|
filename := "backend.json"
|
||||||
f, err := os.Open(filename)
|
f, err := os.Open(filename)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic("cant open backend.json")
|
return err
|
||||||
}
|
}
|
||||||
defer f.Close()
|
defer f.Close()
|
||||||
jw := json.NewDecoder(f)
|
jw := json.NewDecoder(f)
|
||||||
|
@ -119,30 +120,31 @@ func (b *memoryBackend) refreshChannels() {
|
||||||
b.lock.RUnlock()
|
b.lock.RUnlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *memoryBackend) save() {
|
func (b *memoryBackend) save() error {
|
||||||
filename := "backend.json"
|
filename := "backend.json"
|
||||||
f, _ := os.Create(filename)
|
f, err := os.Create(filename)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
defer f.Close()
|
defer f.Close()
|
||||||
jw := json.NewEncoder(f)
|
jw := json.NewEncoder(f)
|
||||||
jw.SetIndent("", " ")
|
jw.SetIndent("", " ")
|
||||||
b.lock.RLock()
|
b.lock.RLock()
|
||||||
defer b.lock.RUnlock()
|
defer b.lock.RUnlock()
|
||||||
jw.Encode(b)
|
return jw.Encode(b)
|
||||||
}
|
}
|
||||||
|
|
||||||
func loadMemoryBackend(pool *redis.Pool) *memoryBackend {
|
func loadMemoryBackend(pool *redis.Pool) (*memoryBackend, error) {
|
||||||
backend := &memoryBackend{pool: pool}
|
backend := &memoryBackend{pool: pool}
|
||||||
err := backend.load()
|
err := backend.load()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Error while loadingbackend: %v\n", err)
|
return nil, errors.Wrap(err, "while loading backend")
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
backend.refreshChannels()
|
backend.refreshChannels()
|
||||||
|
return backend, nil
|
||||||
return backend
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func createMemoryBackend() {
|
func createMemoryBackend() error {
|
||||||
backend := memoryBackend{}
|
backend := memoryBackend{}
|
||||||
backend.lock.Lock()
|
backend.lock.Lock()
|
||||||
|
|
||||||
|
@ -163,7 +165,7 @@ func createMemoryBackend() {
|
||||||
|
|
||||||
backend.lock.Unlock()
|
backend.lock.Unlock()
|
||||||
|
|
||||||
backend.save()
|
return backend.save()
|
||||||
}
|
}
|
||||||
|
|
||||||
// ChannelsGetList gets channels
|
// ChannelsGetList gets channels
|
||||||
|
|
Loading…
Reference in New Issue
Block a user