99 lines
3.8 KiB
Go
99 lines
3.8 KiB
Go
package main
|
|
|
|
import (
|
|
"crypto/tls"
|
|
"log"
|
|
"net/http"
|
|
"os"
|
|
|
|
"git.nutfactory.org/hoernschen/Matrix/config"
|
|
"git.nutfactory.org/hoernschen/Matrix/entities/device"
|
|
"git.nutfactory.org/hoernschen/Matrix/entities/event"
|
|
"git.nutfactory.org/hoernschen/Matrix/entities/general"
|
|
"git.nutfactory.org/hoernschen/Matrix/entities/room"
|
|
"git.nutfactory.org/hoernschen/Matrix/entities/user"
|
|
"git.nutfactory.org/hoernschen/Matrix/utils/database"
|
|
"git.nutfactory.org/hoernschen/Matrix/utils/router"
|
|
)
|
|
|
|
var keyPath = "./ssl.key"
|
|
var certPath = "./ssl.crt"
|
|
|
|
var routes = router.Routes{
|
|
|
|
// General
|
|
router.Route{"ResolveServerName", "GET", "/.well-known/matrix/server", general.ResolveServerName},
|
|
router.Route{"GetServerImplementation", "GET", "/_matrix/federation/v1/version", general.GetServerImplementation},
|
|
router.Route{"Reset", "GET", "/reset", general.Reset},
|
|
|
|
// Keys
|
|
router.Route{"GetSigningKey", "GET", "/_matrix/key/v2/server/{keyId}", device.GetServerSigningKeyHandler},
|
|
router.Route{"GetSigningKey", "GET", "/_matrix/key/v2/server", device.GetServerSigningKeyHandler},
|
|
|
|
// Users
|
|
router.Route{"CheckUsernameAvailability", "GET", "/_matrix/client/r0/register/available", user.CheckUsernameAvailabilityHandler},
|
|
router.Route{"Register", "POST", "/_matrix/client/r0/register", user.RegisterHandler},
|
|
router.Route{"Login", "POST", "/_matrix/client/r0/login", user.LoginHandler},
|
|
router.Route{"Logout", "POST", "/_matrix/client/r0/logout", user.LogoutHandler},
|
|
|
|
// Rooms
|
|
router.Route{"CreateRoom", "POST", "/_matrix/client/r0/createRoom", room.CreateRoomHandler},
|
|
router.Route{"GetRoomMembers", "GET", "/_matrix/client/r0/rooms/{roomId}/members", room.GetRoomMemberHandler},
|
|
router.Route{"JoinRoomUser", "POST", "/_matrix/client/r0/rooms/{roomId}/join", room.JoinRoomUserHandler},
|
|
|
|
router.Route{"GetPrepInfoToJoin", "GET", "/_matrix/federation/v1/make_join/{roomId}/{userId}", room.GetPrepInfoToJoinHandler},
|
|
router.Route{"JoinRoomServer", "PUT", "/_matrix/federation/v2/send_join/{roomId}/{eventId}", room.JoinRoomServerHandler},
|
|
|
|
// Events
|
|
router.Route{"CreateEvent", "PUT", "/_matrix/client/r0/rooms/{roomId}/send/{eventType}/{txnId}", event.SendMessageHandler},
|
|
router.Route{"CreateStateEvent", "PUT", "/_matrix/client/r0/rooms/{roomId}/state/{eventType}/{stateKey}", event.CreateStateEventHandler},
|
|
router.Route{"GetEventUser", "GET", "/_matrix/client/r0/rooms/{roomId}/event/{eventId}", event.GetEventUserHandler},
|
|
router.Route{"GetStateEvent", "GET", "/_matrix/client/r0/rooms/{roomId}/state/{eventType}/{stateKey}", event.GetStateEventHandler},
|
|
|
|
router.Route{"SyncEventsServer", "PUT", "/_matrix/federation/v1/send/{txnId}", event.SyncEventsServerHandler},
|
|
router.Route{"Backfill", "GET", "/_matrix/federation/v1/backfill/{roomId}", event.BackfillHandler},
|
|
}
|
|
|
|
func main() {
|
|
http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
|
|
config.Homeserver = "localhost"
|
|
config.Port = "80"
|
|
if len(os.Args) > 1 {
|
|
config.Homeserver = os.Args[1]
|
|
}
|
|
log.Printf("Start homeserver on name %s", config.Homeserver)
|
|
|
|
if err := device.InitServerSigningKey(); err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
config.VerifyKeys = make(map[string]map[string][]byte)
|
|
os.Remove("sqlite.db")
|
|
|
|
if err := database.InitDB("sqlite.db"); err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
defer database.DB.Close()
|
|
|
|
// TODO: Set Default Config Params here
|
|
config.HttpString = "https"
|
|
|
|
router := router.NewRouter(routes)
|
|
|
|
// TODO: Serve on Port 443 and 80 without Redirect
|
|
httpErr := http.ListenAndServeTLS(":443", certPath, keyPath, router)
|
|
if httpErr != nil {
|
|
log.Fatal(httpErr)
|
|
}
|
|
|
|
// TODO: TEST
|
|
httpErr = http.ListenAndServe(":80", 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)
|
|
}))
|
|
*/
|
|
}
|