Error channels; notify users of restart

This commit is contained in:
Alex 2020-02-28 17:42:15 +01:00
parent 3880bd1701
commit d4e07e81c5
3 changed files with 25 additions and 15 deletions

12
main.go
View file

@ -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 = &reg_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)

View file

@ -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 {

4
web.go
View file

@ -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
}
}()
}