Add Models, Add Database

This commit is contained in:
hoernschen 2020-09-28 22:37:02 +02:00
parent 8f90344870
commit faee833481
21 changed files with 407 additions and 1 deletions

View file

@ -1,3 +1,98 @@
# Matrix # Matrix
Matrix Prototype in Go to test the energy efficiency Matrix Prototype in Go to test the energy efficiency
# API
## Begriffserklärung
Persisted Data Unit (PDU): Are transmitted between homerservers of one room. Part of the history (persisted). reply needed. (mostly events)
Ephemeral Data Unit (EDU): Are transmitted between 2 homeservers. Not part of the history (not persisted). No reply needed. (for example user presence, typing notification, etc.)
Transaction: Collection of up to 50 PDUs and 100 EDUs
Query: HTTP GET Request. Not persisted
## Client-Server
### Users
Check Username Availability: GET /_matrix/client/r0/register/available
Register: POST /_matrix/client/r0/register
Login: POST /_matrix/client/r0/login
Logout: POST /_matrix/client/r0/logout
Deactivate: POST /_matrix/client/r0/account/deactivate
Change Password: POST /_matrix/client/r0/account/password
Sync (After Login): GET /_matrix/client/r0/sync
### Rooms
Create Room: POST /_matrix/client/r0/createRoom
Get Room Members: GET /_matrix/client/r0/rooms/{roomId}/members /
GET /_matrix/client/r0/rooms/{roomId}/joined_members
Join Room: POST /_matrix/client/r0/rooms/{roomId}/join
Leave Room: POST /_matrix/client/r0/rooms/{roomId}/leave
### Events
Create Event: PUT /_matrix/client/r0/rooms/{roomId}/send/{eventType}/{txnId}
Create State Event: PUT /_matrix/client/r0/rooms/{roomId}/state/{eventType}/{stateKey}
Change Event: PUT /_matrix/client/r0/rooms/{roomId}/redact/{eventId}/{txnId}
Get Events: GET /_matrix/client/r0/rooms/{roomId}/messages
Get State Events: GET /_matrix/client/r0/rooms/{roomId}/state
Get Event: GET /_matrix/client/r0/rooms/{roomId}/event/{eventId}
Get State Event: GET /_matrix/client/r0/rooms/{roomId}/state/{eventType}/{stateKey}
## Server-Server
### General
Resolve Server Name: GET /.well-known/matrix/server
Get Server Implementation: GET /_matrix/federation/v1/version
### Keys
Get Signing Key: GET /_matrix/key/v2/server/{keyId}
Get Signing Key from another Server: GET /_matrix/key/v2/query/{serverName}/{keyId}
Get Signing Keys from multiple Servers: POST /_matrix/key/v2/query
### Rooms
Get Prep Information for Join: GET /_matrix/federation/v1/make_join/{roomId}/{userId}
Join Room: PUT /_matrix/federation/v2/send_join/{roomId}/{eventId}
Get Prep Information for Leave: GET /_matrix/federation/v1/make_leave/{roomId}/{userId}
Leave Room: PUT /_matrix/federation/v2/send_leave/{roomId}/{eventId}
### Events
Get State: GET /_matrix/federation/v1/state/{roomId} /
GET /_matrix/federation/v1/state_ids/{roomId}
Get Event: GET /_matrix/federation/v1/event/{eventId}
Sync Events (trough transactions with multiple events): PUT /_matrix/federation/v1/send/{txnId}
Backfill: GET /_matrix/federation/v1/backfill/{roomId} /
POST /_matrix/federation/v1/get_missing_events/{roomId}

View file

@ -0,0 +1,7 @@
package device
type Device struct {
Id string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Keys map[string]string `json:"keys,omitempty"`
}

View file

6
entities/event/edu.go Normal file
View file

@ -0,0 +1,6 @@
package event
type EDU struct {
Type string `json:"type,omitempty"`
Content string `json:"content,omitempty"`
}

10
entities/event/event.go Normal file
View file

@ -0,0 +1,10 @@
package event
type Event struct {
Id string `json:"id,omitempty"`
RoomId string `json:"roomId,omitempty"`
EventType string `json:"eventType,omitempty"`
Content string `json:"content,omitempty"`
Parent string `json:"parent,omitempty"`
Depth int `json:"depth,omitempty"`
}

View file

@ -0,0 +1,18 @@
package event
import (
"database/sql"
_ "github.com/mattn/go-sqlite3"
)
func InitDB(filepath string) *sql.DB {
db, err := sql.Open("sqlite3", filepath)
if err != nil {
panic(err)
}
if db == nil {
panic("db nil")
}
return db
}

13
entities/room/room.go Normal file
View file

@ -0,0 +1,13 @@
package room
import (
"nutfactory.org/Matrix/entities/event"
)
type Room struct {
Id string `json:"id,omitempty"`
Messages map[string]event.Event `json:"messages,omitempty"`
State map[string]event.Event `json:"state,omitempty"`
Members []string `json:"members,omitempty"`
Version string `json:"version,omitempty"`
}

View file

View file

@ -0,0 +1,11 @@
package transaction
import (
"nutfactory.org/Matrix/entities/event"
)
type Transaction struct {
Id string `json:"id,omitempty"`
PDUS map[string]event.Event `json:"pdus,omitempty"`
EDUS []event.EDU `json:"edus,omitempty"`
}

12
entities/user/user.go Normal file
View file

@ -0,0 +1,12 @@
package user
import (
"nutfactory.org/Matrix/entities/device"
)
type User struct {
Id string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Password string `json:"password,omitempty"`
Devices map[string]device.Device `json:"devices,omitempty"`
}

View file

8
go.mod Normal file
View file

@ -0,0 +1,8 @@
module nutfactory.org/Matrix
go 1.14
require (
github.com/gorilla/mux v1.8.0
github.com/mattn/go-sqlite3 v1.14.3
)

4
go.sum Normal file
View file

@ -0,0 +1,4 @@
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
github.com/mattn/go-sqlite3 v1.14.3 h1:j7a/xn1U6TKA/PHHxqZuzh64CdtRc7rU9M+AvkOl5bA=
github.com/mattn/go-sqlite3 v1.14.3/go.mod h1:WVKg1VTActs4Qso6iwGbiFih2UIHo0ENGwNd0Lj+XmI=

89
main.go Normal file
View file

@ -0,0 +1,89 @@
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"nutfactory.org/Matrix/utils"
"nutfactory.org/Matrix/utils/router"
)
var keyPath = "./ssl.key"
var certPath = "./ssl.crt"
//var htmlPath = "./html/"
var routes = router.Routes{
// General
router.Route{"ResolveServerName", "GET", "/.well-known/matrix/server", Test},
router.Route{"GetServerImplementation", "GET", "/_matrix/federation/v1/version", Test},
// Keys
router.Route{"GetSigningKey", "GET", "/_matrix/key/v2/server/{keyId}", Test},
router.Route{"GetSigningKeyFromServer", "GET", "/_matrix/key/v2/query/{serverName}/{keyId}", Test},
router.Route{"GetSigningKeyFromMultipleServer", "GET", "/_matrix/key/v2/query", Test},
// Users
router.Route{"CheckUsernameAvailability", "GET", "/_matrix/client/r0/register/available", Test},
router.Route{"Register", "POST", "/_matrix/client/r0/register", Test},
router.Route{"Login", "POST", "/_matrix/client/r0/login", Test},
router.Route{"Logout", "POST", "/_matrix/client/r0/logout", Test},
router.Route{"Deactivate", "POST", "/_matrix/client/r0/account/deactivate", Test},
router.Route{"ChangePassword", "POST", "/_matrix/client/r0/account/password", Test},
router.Route{"Sync", "GET", "/_matrix/client/r0/sync", Test},
// Rooms
router.Route{"CreateRoom", "POST", "/_matrix/client/r0/createRoom", Test},
router.Route{"GetRoomMembers", "GET", "/_matrix/client/r0/rooms/{roomId}/members", Test},
router.Route{"JoinRoomUser", "POST", "/_matrix/client/r0/rooms/{roomId}/join", Test},
router.Route{"LeaveRoomUser", "POST", "/_matrix/client/r0/rooms/{roomId}/leave", Test},
router.Route{"GetPrepInfoToJoin", "GET", "/_matrix/federation/v1/make_join/{roomId}/{userId}", Test},
router.Route{"JoinRoomServer", "PUT", "/_matrix/federation/v2/send_join/{roomId}/{eventId}", Test},
router.Route{"GetPrepInfoToLeave", "GET", "/_matrix/federation/v1/make_leave/{roomId}/{userId}", Test},
router.Route{"LeaveRoomServer", "PUT", "/_matrix/federation/v2/send_leave/{roomId}/{eventId}", Test},
// Events
router.Route{"CreateEvent", "PUT", "/_matrix/client/r0/rooms/{roomId}/send/{eventType}/{txnId}", Test},
router.Route{"CreateStateEvent", "PUT", "/_matrix/client/r0/rooms/{roomId}/state/{eventType}/{stateKey}", Test},
router.Route{"ChangeEvent", "PUT", "/_matrix/client/r0/rooms/{roomId}/redact/{eventId}/{txnId}", Test},
router.Route{"GetEvents", "GET", "/_matrix/client/r0/rooms/{roomId}/messages", Test},
router.Route{"GetStateEventsUser", "GET", "/_matrix/client/r0/rooms/{roomId}/state", Test},
router.Route{"GetEventUser", "GET", "/_matrix/client/r0/rooms/{roomId}/event/{eventId}", Test},
router.Route{"GetStateEvent", "GET", "/_matrix/client/r0/rooms/{roomId}/state/{eventType}/{stateKey}", Test},
router.Route{"GetStateEventsServer", "GET", "/_matrix/federation/v1/state/{roomId}", Test},
router.Route{"GetEventServer", "GET", "/_matrix/federation/v1/event/{eventId}", Test},
router.Route{"SyncEventsServer", "PUT", "/_matrix/federation/v1/send/{txnId}", Test},
router.Route{"Backfill", "GET", "/_matrix/federation/v1/backfill/{roomId}", Test},
router.Route{"GetMissingEvents", "POST", "/_matrix/federation/v1/get_missing_events/{roomId}", Test},
}
func Test(w http.ResponseWriter, r *http.Request) {
fmt.Printf("TEST")
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusOK)
if err := json.NewEncoder(w).Encode("Test"); err != nil {
panic(err)
}
}
func main() {
db := utils.InitDB("sqlite.db")
defer db.Close()
router := router.NewRouter(routes)
//router.PathPrefix("/").Handler(http.FileServer(http.Dir(htmlPath)))
httpErr := http.ListenAndServeTLS(":443", certPath, keyPath, router)
if httpErr != nil {
log.Fatal(httpErr)
}
go http.ListenAndServe(":80", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "https://"+r.Host+r.URL.String(), http.StatusMovedPermanently)
}))
}

24
ssl.crt Normal file
View file

@ -0,0 +1,24 @@
-----BEGIN CERTIFICATE-----
MIIEGDCCAwCgAwIBAgIJANNCajLtjHULMA0GCSqGSIb3DQEBCwUAMIGgMQswCQYD
VQQGEwJERTEWMBQGA1UECAwNTmllZGVyc2FjaHNlbjERMA8GA1UEBwwIQnJhbXNj
aGUxITAfBgNVBAoMGEludGVybmV0IFdpZGdpdHMgUHR5IEx0ZDETMBEGA1UEAwwK
aG9lcm5zY2hlbjEuMCwGCSqGSIb3DQEJARYfanVsaWFuLmhvZXJuc2NoZW1leWVy
QGdtYWlsLmNvbTAeFw0xODA4MTAxMzQ0MDJaFw0yODA4MDcxMzQ0MDJaMIGgMQsw
CQYDVQQGEwJERTEWMBQGA1UECAwNTmllZGVyc2FjaHNlbjERMA8GA1UEBwwIQnJh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UY8PJ9FM+6RTy4F8oVbwmS5jcrAoGBAK4prusC3SzCH0Cdqk9q
HMbL9DrMcACOmGKHz1EhhHe5KNJsiNHP86Jl2SMWVW7AV30O2VyQnt/eMmPdZ7Dw
CwY2AZsvZ6zj+MFfQSovs6qJFMASDr65gKSjz8vHNxH2CxPNtE4qOfmUxfNmplvB
Kb4cY7xotuqvIIdPe3pxa0sJAoGBAMflLXdE7LQRHrqECxpOg0wG/f8mdUbldHGn
70J+MzEQi9v0ypKy3AmmmkWs3iwvNg9O+BTr7k/QF4Hnba/+yzcneGYVXQsOA4Vw
24JJXrCq+LcXMvX0FPzDeYUwa2KLB7MHASuKhf4XYf2wkoUFCA1mpIuageaFscc4
6IxdWCWJAoGAXXk8bBRj0YksNCP41KVmxLqoky+vd115BjBxmiEK8K9NK1eFUGZe
K8/FeX3JmFrxAS+N+LeP+XVgyF1triDZ0ix8gn2bSY3skZHsUw62B6w7xWXzqgx4
rZM+GfS/QY2N9ubqze1m/vROSf65iHakZf+mxE+uf2BCi1WOxp7KARE=
-----END RSA PRIVATE KEY-----

View file

@ -0,0 +1,20 @@
package utils
import (
"database/sql"
"log"
_ "github.com/mattn/go-sqlite3"
)
func InitDB(filepath string) *sql.DB {
log.Printf("Init DB")
db, err := sql.Open("sqlite3", filepath)
if err != nil {
panic(err)
}
if db == nil {
panic("db nil")
}
return db
}

23
utils/logger.go Normal file
View file

@ -0,0 +1,23 @@
package utils
import (
"log"
"net/http"
"time"
)
func APILogger(inner http.Handler, name string) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
start := time.Now()
inner.ServeHTTP(w, r)
log.Printf(
"%s\t%s\t%s\t%s",
r.Method,
r.RequestURI,
name,
time.Since(start),
)
})
}

12
utils/router/route.go Normal file
View file

@ -0,0 +1,12 @@
package router
import "net/http"
type Route struct {
Name string
Method string
Pattern string
HandlerFunc http.HandlerFunc
}
type Routes []Route

27
utils/router/router.go Normal file
View file

@ -0,0 +1,27 @@
package router
import (
"net/http"
"github.com/gorilla/mux"
"nutfactory.org/Matrix/utils"
)
func NewRouter(routes Routes) *mux.Router {
router := mux.NewRouter().StrictSlash(true)
for _, route := range routes {
var handler http.Handler
handler = route.HandlerFunc
handler = utils.APILogger(handler, route.Name)
router.
Methods(route.Method).
Path(route.Pattern).
Name(route.Name).
Handler(handler)
}
return router
}