Matrix/main.go
2020-09-29 09:44:35 +02:00

102 lines
4.1 KiB
Go

package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"strconv"
"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) {
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() {
// TODO: Remove later
os.Remove("sqlite.db")
db := utils.InitDB("sqlite.db")
defer db.Close()
rows, _ := db.Query("SELECT id, firstname, lastname FROM people")
var id int
var firstname string
var lastname string
for rows.Next() {
rows.Scan(&id, &firstname, &lastname)
fmt.Println(strconv.Itoa(id) + ": " + firstname + " " + lastname)
}
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)
}))
}