diff options
author | Alex Auvolat <alex@adnab.me> | 2020-02-16 19:30:49 +0100 |
---|---|---|
committer | Alex Auvolat <alex@adnab.me> | 2020-02-16 19:30:49 +0100 |
commit | 046ec6380b7bb363e537ade7fd254b5505dde32d (patch) | |
tree | 05d8778d02a0c815d75491937e1e41a906450a58 /appservice | |
parent | 225fc84f097aa615239df6deece647a19794234a (diff) | |
download | easybridge-046ec6380b7bb363e537ade7fd254b5505dde32d.tar.gz easybridge-046ec6380b7bb363e537ade7fd254b5505dde32d.zip |
Some infrastructure
Diffstat (limited to 'appservice')
-rw-r--r-- | appservice/account.go | 32 | ||||
-rw-r--r-- | appservice/db.go | 21 | ||||
-rw-r--r-- | appservice/server.go | 72 |
3 files changed, 125 insertions, 0 deletions
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, "{}") +} |