diff options
author | Alex Auvolat <alex@adnab.me> | 2020-02-28 17:42:15 +0100 |
---|---|---|
committer | Alex Auvolat <alex@adnab.me> | 2020-02-28 17:42:15 +0100 |
commit | d4e07e81c532bf41fe827d9594cdcb505a7f7028 (patch) | |
tree | be440f4e0c0d37544e57aaf7b06baf93c0c26666 | |
parent | 3880bd170144b913fe52a1ef1f3687551d797973 (diff) | |
download | easybridge-d4e07e81c532bf41fe827d9594cdcb505a7f7028.tar.gz easybridge-d4e07e81c532bf41fe827d9594cdcb505a7f7028.zip |
Error channels; notify users of restart
-rw-r--r-- | main.go | 12 | ||||
-rw-r--r-- | server.go | 24 | ||||
-rw-r--r-- | web.go | 4 |
3 files changed, 25 insertions, 15 deletions
@@ -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) @@ -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 { @@ -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 } }() } |