Error channels; notify users of restart
This commit is contained in:
parent
3880bd1701
commit
d4e07e81c5
3 changed files with 25 additions and 15 deletions
12
main.go
12
main.go
|
@ -151,6 +151,7 @@ func readRegistration(file string) mxlib.Registration {
|
||||||
func main() {
|
func main() {
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
|
// Read configuration
|
||||||
config_file := readConfig()
|
config_file := readConfig()
|
||||||
config = &config_file
|
config = &config_file
|
||||||
|
|
||||||
|
@ -163,13 +164,12 @@ func main() {
|
||||||
reg_file := readRegistration(config.Registration)
|
reg_file := readRegistration(config.Registration)
|
||||||
registration = ®_file
|
registration = ®_file
|
||||||
|
|
||||||
errch, err := StartAppService()
|
// Start appservice and web management interface
|
||||||
if err != nil {
|
errch := make(chan error)
|
||||||
log.Fatal(err)
|
StartAppService(errch)
|
||||||
}
|
StartWeb(errch)
|
||||||
|
|
||||||
StartWeb()
|
|
||||||
|
|
||||||
|
// Wait for an error somewhere
|
||||||
err = <-errch
|
err = <-errch
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
|
|
24
server.go
24
server.go
|
@ -15,17 +15,17 @@ import (
|
||||||
|
|
||||||
var mx *mxlib.Client
|
var mx *mxlib.Client
|
||||||
|
|
||||||
func StartAppService() (chan error, error) {
|
func StartAppService(errch chan error) error {
|
||||||
mx = mxlib.NewClient(config.Server, registration.AsToken)
|
mx = mxlib.NewClient(config.Server, registration.AsToken)
|
||||||
|
|
||||||
err := InitDb()
|
err := InitDb()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
err = mx.RegisterUser(registration.SenderLocalpart)
|
err = mx.RegisterUser(registration.SenderLocalpart)
|
||||||
if mxe, ok := err.(*mxlib.MxError); !ok || mxe.ErrCode != "M_USER_IN_USE" {
|
if mxe, ok := err.(*mxlib.MxError); !ok || mxe.ErrCode != "M_USER_IN_USE" {
|
||||||
return nil, err
|
return err
|
||||||
}
|
}
|
||||||
if err == nil {
|
if err == nil {
|
||||||
// If Easybridge account was created, update avatar and display name
|
// If Easybridge account was created, update avatar and display name
|
||||||
|
@ -33,11 +33,11 @@ func StartAppService() (chan error, error) {
|
||||||
Path: "easybridge.jpg",
|
Path: "easybridge.jpg",
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return err
|
||||||
}
|
}
|
||||||
err = mx.ProfileDisplayname(ezbrMxId(), fmt.Sprintf("Easybridge (%s)", EASYBRIDGE_SYSTEM_PROTOCOL))
|
err = mx.ProfileDisplayname(ezbrMxId(), fmt.Sprintf("Easybridge (%s)", EASYBRIDGE_SYSTEM_PROTOCOL))
|
||||||
if err != nil {
|
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("/_matrix/app/v1/transactions/{txnId}", handleTxn)
|
||||||
router.HandleFunc("/transactions/{txnId}", handleTxn)
|
router.HandleFunc("/transactions/{txnId}", handleTxn)
|
||||||
|
|
||||||
errch := make(chan error)
|
|
||||||
go func() {
|
go func() {
|
||||||
log.Printf("Starting HTTP server on %s", config.ASBindAddr)
|
log.Printf("Starting HTTP server on %s", config.ASBindAddr)
|
||||||
err := http.ListenAndServe(config.ASBindAddr, checkTokenAndLog(router))
|
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 {
|
func checkTokenAndLog(handler http.Handler) http.Handler {
|
||||||
|
|
4
web.go
4
web.go
|
@ -21,7 +21,7 @@ const SESSION_NAME = "easybridge_session"
|
||||||
var sessionsStore sessions.Store = nil
|
var sessionsStore sessions.Store = nil
|
||||||
var userKeys = map[string]*[32]byte{}
|
var userKeys = map[string]*[32]byte{}
|
||||||
|
|
||||||
func StartWeb() {
|
func StartWeb(errch chan error) {
|
||||||
session_key := blake2b.Sum256([]byte(config.SessionKey))
|
session_key := blake2b.Sum256([]byte(config.SessionKey))
|
||||||
sessionsStore = sessions.NewCookieStore(session_key[:])
|
sessionsStore = sessions.NewCookieStore(session_key[:])
|
||||||
|
|
||||||
|
@ -39,7 +39,7 @@ func StartWeb() {
|
||||||
go func() {
|
go func() {
|
||||||
err := http.ListenAndServe(config.WebBindAddr, logRequest(r))
|
err := http.ListenAndServe(config.WebBindAddr, logRequest(r))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal("Cannot start http server: ", err)
|
errch <- err
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue