Matrix/main.go

93 lines
4 KiB
Go
Raw Normal View History

2020-09-28 20:37:02 +00:00
package main
import (
"encoding/json"
"log"
"net/http"
2020-09-29 07:44:35 +00:00
"os"
2020-09-28 20:37:02 +00:00
"nutfactory.org/Matrix/entities/user"
2020-10-01 15:45:57 +00:00
"nutfactory.org/Matrix/utils/database"
2020-09-28 20:37:02 +00:00
"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", user.CheckUsernameAvailability},
router.Route{"Register", "POST", "/_matrix/client/r0/register", user.Register},
router.Route{"Login", "POST", "/_matrix/client/r0/login", user.Login},
router.Route{"Logout", "POST", "/_matrix/client/r0/logout", user.Logout},
router.Route{"Deactivate", "POST", "/_matrix/client/r0/account/deactivate", user.Deactivate},
router.Route{"ChangePassword", "POST", "/_matrix/client/r0/account/password", user.ChangePassword},
router.Route{"Sync", "GET", "/_matrix/client/r0/sync", user.Sync},
2020-09-28 20:37:02 +00:00
// 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() {
2020-09-29 07:44:35 +00:00
// TODO: Remove later
os.Remove("sqlite.db")
_ = database.InitDB("sqlite.db")
defer database.DB.Close()
2020-09-28 20:37:02 +00:00
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)
}))
}