From 046ec6380b7bb363e537ade7fd254b5505dde32d Mon Sep 17 00:00:00 2001 From: Alex Auvolat Date: Sun, 16 Feb 2020 19:30:49 +0100 Subject: Some infrastructure --- appservice/account.go | 32 +++++++++++++++++++++++ appservice/db.go | 21 +++++++++++++++ appservice/server.go | 72 +++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 125 insertions(+) create mode 100644 appservice/account.go create mode 100644 appservice/db.go create mode 100644 appservice/server.go (limited to 'appservice') diff --git a/appservice/account.go b/appservice/account.go new file mode 100644 index 0000000..533e01e --- /dev/null +++ b/appservice/account.go @@ -0,0 +1,32 @@ +package appservice + +import ( + . "git.deuxfleurs.fr/Deuxfleurs/easybridge/connector" +) + +type Account struct { + MatrixUser string + AccountName string + Protocol string + Conn Connector +} + +func (a *Account) Joined(roomId RoomID) { + // TODO +} + +func (a *Account) Left(roomId RoomID) { + // TODO +} + +func (a *Account) UserInfoUpdated(user UserID, info *UserInfo) { + // TODO +} + +func (a *Account) RoomInfoUpdated(roomId RoomID, info *RoomInfo) { + // TODO +} + +func (a *Account) Event(event *Event) { + // TODO +} diff --git a/appservice/db.go b/appservice/db.go new file mode 100644 index 0000000..2c71312 --- /dev/null +++ b/appservice/db.go @@ -0,0 +1,21 @@ +package appservice + +import ( + "github.com/jinzhu/gorm" + _ "github.com/jinzhu/gorm/dialects/mysql" + _ "github.com/jinzhu/gorm/dialects/postgres" + _ "github.com/jinzhu/gorm/dialects/sqlite" +) + +var db *gorm.DB + +func InitDb() error { + var err error + + db, err = gorm.Open(config.DbType, config.DbPath) + if err != nil { + return err + } + + return nil +} diff --git a/appservice/server.go b/appservice/server.go new file mode 100644 index 0000000..395d383 --- /dev/null +++ b/appservice/server.go @@ -0,0 +1,72 @@ +package appservice + +import ( + "encoding/json" + "fmt" + "strings" + "log" + "net/http" + + "git.deuxfleurs.fr/Deuxfleurs/easybridge/mxlib" + "github.com/gorilla/mux" +) + +type Config struct { + HttpBindAddr string + Server string + DbType string + DbPath string +} + + +var registration *mxlib.Registration +var config *Config + +func Start(r *mxlib.Registration, c *Config) (chan error, error) { + registration = r + config = c + + err := InitDb() + if err != nil { + return nil, err + } + + router := mux.NewRouter() + router.HandleFunc("/_matrix/app/v1/transactions/{txnId}", handleTxn) + + errch := make(chan error) + go func() { + log.Printf("Starting HTTP server on %s", config.HttpBindAddr) + err := http.ListenAndServe(config.HttpBindAddr, checkTokenAndLog(router)) + if err != nil { + errch <- err + } + }() + + return errch, nil +} + +func checkTokenAndLog(handler http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + r.ParseForm() + if strings.Join(r.Form["access_token"], "") != registration.HsToken { + http.Error(w, "Wrong or no token provided", http.StatusUnauthorized) + return + } + + log.Printf("%s %s %s\n", r.RemoteAddr, r.Method, r.URL) + handler.ServeHTTP(w, r) + }) +} + +func handleTxn(w http.ResponseWriter, r *http.Request) { + var txn mxlib.Transaction + err := json.NewDecoder(r.Body).Decode(&txn) + if err != nil { + http.Error(w, err.Error(), http.StatusBadRequest) + } + + log.Printf("Got transaction %#v\n", txn) + + fmt.Fprintf(w, "{}") +} -- cgit v1.2.3