2019-07-19 07:39:53 +00:00
|
|
|
package internal
|
2019-07-18 14:10:43 +00:00
|
|
|
|
|
|
|
import (
|
2019-07-31 15:01:20 +00:00
|
|
|
"errors"
|
2019-07-18 14:10:43 +00:00
|
|
|
"net/http"
|
2019-07-31 15:01:20 +00:00
|
|
|
"strconv"
|
2019-07-18 14:10:43 +00:00
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
2019-07-25 15:14:37 +00:00
|
|
|
|
2019-08-03 14:19:18 +00:00
|
|
|
"github.com/signaller-matrix/signaller/internal/models/capabilities"
|
2019-07-18 14:10:43 +00:00
|
|
|
)
|
|
|
|
|
2019-07-19 16:03:29 +00:00
|
|
|
var currServer *Server
|
2019-07-19 07:39:53 +00:00
|
|
|
|
2019-07-18 14:10:43 +00:00
|
|
|
type Server struct {
|
|
|
|
httpServer *http.Server
|
|
|
|
router *mux.Router
|
|
|
|
|
|
|
|
Address string
|
2019-07-25 15:14:37 +00:00
|
|
|
|
|
|
|
Capabilities capabilities.Capabilities
|
|
|
|
Backend Backend
|
2019-07-18 14:10:43 +00:00
|
|
|
}
|
|
|
|
|
2019-07-29 14:08:13 +00:00
|
|
|
func NewServer(port int) (*Server, error) {
|
|
|
|
|
2019-07-18 14:10:43 +00:00
|
|
|
router := mux.NewRouter()
|
|
|
|
router.HandleFunc("/_matrix/client/versions", VersionHandler)
|
|
|
|
router.HandleFunc("/_matrix/client/r0/login", LoginHandler)
|
|
|
|
router.HandleFunc("/_matrix/client/r0/logout", LogoutHandler)
|
2019-07-24 14:15:07 +00:00
|
|
|
router.HandleFunc("/_matrix/client/r0/logout/all", LogoutAllHandler)
|
2019-07-18 14:10:43 +00:00
|
|
|
router.HandleFunc("/_matrix/client/r0/register", RegisterHandler)
|
2019-07-23 14:37:02 +00:00
|
|
|
router.HandleFunc("/_matrix/client/r0/account/whoami", WhoAmIHandler)
|
2019-07-24 15:04:45 +00:00
|
|
|
router.HandleFunc("/_matrix/client/r0/joined_rooms", JoinedRoomsHandler)
|
2019-07-25 16:56:31 +00:00
|
|
|
router.HandleFunc("/_matrix/client/r0/account/password", PasswordHandler)
|
2019-07-18 14:10:43 +00:00
|
|
|
router.HandleFunc("/_matrix/client/r0/sync", SyncHandler)
|
2019-07-25 15:14:37 +00:00
|
|
|
router.HandleFunc("/_matrix/client/r0/capabilities", CapabilitiesHandler)
|
2019-07-29 14:32:38 +00:00
|
|
|
router.HandleFunc("/_matrix/client/r0/devices", DevicesHandler)
|
2019-07-31 15:01:20 +00:00
|
|
|
router.HandleFunc("/_matrix/client/r0/directory/list/room/{roomID}", listRoomHandler)
|
2019-08-01 14:29:43 +00:00
|
|
|
router.HandleFunc("/_matrix/client/r0/rooms/{roomId}/leave", leaveRoomHandler)
|
2019-08-02 13:17:18 +00:00
|
|
|
router.HandleFunc("/_matrix/client/r0/register/available", registerAvailableHandler)
|
2019-08-02 17:55:35 +00:00
|
|
|
router.HandleFunc("/_matrix/client/r0/publicRooms", publicRoomsHandler)
|
2019-08-05 10:12:48 +00:00
|
|
|
router.HandleFunc("/_matrix/client/r0/user/{userId}/filter/{filterID}", GetFilterHandler).Methods("GET")
|
2019-08-04 16:46:31 +00:00
|
|
|
router.HandleFunc("/_matrix/client/r0/user/{userId}/filter", AddFilterHandler).Methods("POST")
|
2019-08-06 15:28:39 +00:00
|
|
|
router.HandleFunc("/_matrix/client/r0/directory/room/{roomAlias}", roomAliasHandler).Methods(http.MethodPut, http.MethodGet, http.MethodDelete)
|
2019-08-04 16:46:31 +00:00
|
|
|
|
2019-07-18 14:10:43 +00:00
|
|
|
router.HandleFunc("/", RootHandler)
|
|
|
|
|
2019-07-29 14:08:13 +00:00
|
|
|
if port <= 0 || port > 65535 {
|
|
|
|
return nil, errors.New("invalid port number")
|
|
|
|
}
|
|
|
|
|
2019-07-18 14:10:43 +00:00
|
|
|
httpServer := new(http.Server)
|
2019-07-29 14:08:13 +00:00
|
|
|
httpServer.Addr = ":" + strconv.Itoa(port)
|
2019-07-18 14:10:43 +00:00
|
|
|
httpServer.Handler = router
|
|
|
|
|
|
|
|
server := &Server{
|
|
|
|
httpServer: httpServer,
|
|
|
|
router: router}
|
|
|
|
|
2019-07-19 16:03:29 +00:00
|
|
|
currServer = server
|
2019-07-29 14:08:13 +00:00
|
|
|
return server, nil
|
2019-07-18 14:10:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (server *Server) Run() error {
|
|
|
|
return server.httpServer.ListenAndServe() // TODO: custom port
|
|
|
|
}
|