Improve error handling in main

This commit is contained in:
Peter Stuifzand 2019-03-23 20:48:47 +01:00
parent 3d4c4744fe
commit 96bade5a53
WARNING! Although there is a key with this ID in the database it does not verify this commit! This commit is SUSPICIOUS.
GPG Key ID: 374322D56E5209E8

View File

@ -24,6 +24,7 @@ import (
"time" "time"
"github.com/gomodule/redigo/redis" "github.com/gomodule/redigo/redis"
"github.com/pkg/errors"
"p83.nl/go/ekster/pkg/auth" "p83.nl/go/ekster/pkg/auth"
"p83.nl/go/ekster/pkg/server" "p83.nl/go/ekster/pkg/server"
@ -97,16 +98,16 @@ type App struct {
} }
// Run runs the app // Run runs the app
func (app *App) Run() { func (app *App) Run() error {
app.backend.run() app.backend.run()
app.hubBackend.run() app.hubBackend.run()
log.Printf("Listening on port %d\n", app.options.Port) log.Printf("Listening on port %d\n", app.options.Port)
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", app.options.Port), nil)) return http.ListenAndServe(fmt.Sprintf(":%d", app.options.Port), nil)
} }
// NewApp initializes the App // NewApp initializes the App
func NewApp(options AppOptions) *App { func NewApp(options AppOptions) (*App, error) {
app := &App{ app := &App{
options: options, options: options,
} }
@ -137,12 +138,12 @@ func NewApp(options AppOptions) *App {
if !options.Headless { if !options.Headless {
handler, err := newMainHandler(app.backend, options.BaseURL, options.TemplateDir, options.pool) handler, err := newMainHandler(app.backend, options.BaseURL, options.TemplateDir, options.pool)
if err != nil { if err != nil {
log.Fatal(err) return nil, errors.Wrap(err, "could not create main handler")
} }
http.Handle("/", handler) http.Handle("/", handler)
} }
return app return app, nil
} }
func main() { func main() {
@ -204,5 +205,13 @@ func main() {
pool := newPool(options.RedisServer) pool := newPool(options.RedisServer)
options.pool = pool options.pool = pool
NewApp(options).Run() app, err := NewApp(options)
if err != nil {
log.Fatal(err)
}
err = app.Run()
if err != nil {
log.Fatal(err)
}
} }