From d4e07e81c532bf41fe827d9594cdcb505a7f7028 Mon Sep 17 00:00:00 2001 From: Alex Auvolat Date: Fri, 28 Feb 2020 17:42:15 +0100 Subject: Error channels; notify users of restart --- main.go | 12 ++++++------ server.go | 24 +++++++++++++++++------- web.go | 4 ++-- 3 files changed, 25 insertions(+), 15 deletions(-) diff --git a/main.go b/main.go index 339f6c7..e6c3f82 100644 --- a/main.go +++ b/main.go @@ -151,6 +151,7 @@ func readRegistration(file string) mxlib.Registration { func main() { flag.Parse() + // Read configuration config_file := readConfig() config = &config_file @@ -163,13 +164,12 @@ func main() { reg_file := readRegistration(config.Registration) registration = ®_file - errch, err := StartAppService() - if err != nil { - log.Fatal(err) - } - - StartWeb() + // Start appservice and web management interface + errch := make(chan error) + StartAppService(errch) + StartWeb(errch) + // Wait for an error somewhere err = <-errch if err != nil { log.Fatal(err) diff --git a/server.go b/server.go index f3eb898..e914f3b 100644 --- a/server.go +++ b/server.go @@ -15,17 +15,17 @@ import ( var mx *mxlib.Client -func StartAppService() (chan error, error) { +func StartAppService(errch chan error) error { mx = mxlib.NewClient(config.Server, registration.AsToken) err := InitDb() if err != nil { - return nil, err + return err } err = mx.RegisterUser(registration.SenderLocalpart) if mxe, ok := err.(*mxlib.MxError); !ok || mxe.ErrCode != "M_USER_IN_USE" { - return nil, err + return err } if err == nil { // If Easybridge account was created, update avatar and display name @@ -33,11 +33,11 @@ func StartAppService() (chan error, error) { Path: "easybridge.jpg", }) if err != nil { - return nil, err + return err } err = mx.ProfileDisplayname(ezbrMxId(), fmt.Sprintf("Easybridge (%s)", EASYBRIDGE_SYSTEM_PROTOCOL)) if err != nil { - return nil, err + return err } } @@ -45,7 +45,6 @@ func StartAppService() (chan error, error) { router.HandleFunc("/_matrix/app/v1/transactions/{txnId}", handleTxn) router.HandleFunc("/transactions/{txnId}", handleTxn) - errch := make(chan error) go func() { log.Printf("Starting HTTP server on %s", config.ASBindAddr) err := http.ListenAndServe(config.ASBindAddr, checkTokenAndLog(router)) @@ -54,7 +53,18 @@ func StartAppService() (chan error, error) { } }() - return errch, nil + // Notify users that Easybridge has restarted + go func() { + var users []DbAccountConfig + db.Model(&DbAccountConfig{}).Select("mx_user_id").Group("mx_user_id").Find(&users) + for _, u := range users { + ezbrSystemSendf(u.MxUserID, + "Easybridge has restarted, please visit %s or open configuration widget to reconnect to your accounts.", + config.WebURL) + } + }() + + return nil } func checkTokenAndLog(handler http.Handler) http.Handler { diff --git a/web.go b/web.go index d50d91c..d5846c5 100644 --- a/web.go +++ b/web.go @@ -21,7 +21,7 @@ const SESSION_NAME = "easybridge_session" var sessionsStore sessions.Store = nil var userKeys = map[string]*[32]byte{} -func StartWeb() { +func StartWeb(errch chan error) { session_key := blake2b.Sum256([]byte(config.SessionKey)) sessionsStore = sessions.NewCookieStore(session_key[:]) @@ -39,7 +39,7 @@ func StartWeb() { go func() { err := http.ListenAndServe(config.WebBindAddr, logRequest(r)) if err != nil { - log.Fatal("Cannot start http server: ", err) + errch <- err } }() } -- cgit v1.2.3