feat: Add ability to change listening port

This commit is contained in:
ChronosX88 2019-07-29 18:08:13 +04:00
parent 104e797af9
commit 1c7937dd22
Signed by: ChronosXYZ
GPG Key ID: 085A69A82C8C511A
2 changed files with 21 additions and 5 deletions

View File

@ -2,22 +2,29 @@ package main
import ( import (
"log" "log"
"strconv"
"github.com/nxshock/signaller/internal" "github.com/nxshock/signaller/internal"
"github.com/nxshock/signaller/internal/backends/memory" "github.com/nxshock/signaller/internal/backends/memory"
) )
var ( var (
server *internal.Server server *internal.Server
defaultPortNumber = 8008
) )
func init() { func init() {
server = internal.New() var err error
server, err = internal.NewServer(defaultPortNumber)
if err != nil {
panic(err)
}
server.Address = "localhost" server.Address = "localhost"
server.Backend = memory.NewBackend(server.Address) server.Backend = memory.NewBackend(server.Address)
server.Backend.Register("andrew", "1", "") server.Backend.Register("andrew", "1", "")
} }
func main() { func main() {
log.Println("Server started on port " + strconv.Itoa(defaultPortNumber))
log.Println(server.Run()) log.Println(server.Run())
} }

View File

@ -5,6 +5,10 @@ import (
"github.com/gorilla/mux" "github.com/gorilla/mux"
"errors"
"strconv"
"github.com/nxshock/signaller/internal/models/capabilities" "github.com/nxshock/signaller/internal/models/capabilities"
) )
@ -20,7 +24,8 @@ type Server struct {
Backend Backend Backend Backend
} }
func New() *Server { func NewServer(port int) (*Server, error) {
router := mux.NewRouter() router := mux.NewRouter()
router.HandleFunc("/_matrix/client/versions", VersionHandler) router.HandleFunc("/_matrix/client/versions", VersionHandler)
router.HandleFunc("/_matrix/client/r0/login", LoginHandler) router.HandleFunc("/_matrix/client/r0/login", LoginHandler)
@ -34,8 +39,12 @@ func New() *Server {
router.HandleFunc("/_matrix/client/r0/capabilities", CapabilitiesHandler) router.HandleFunc("/_matrix/client/r0/capabilities", CapabilitiesHandler)
router.HandleFunc("/", RootHandler) router.HandleFunc("/", RootHandler)
if port <= 0 || port > 65535 {
return nil, errors.New("invalid port number")
}
httpServer := new(http.Server) httpServer := new(http.Server)
httpServer.Addr = ":8008" httpServer.Addr = ":" + strconv.Itoa(port)
httpServer.Handler = router httpServer.Handler = router
server := &Server{ server := &Server{
@ -43,7 +52,7 @@ func New() *Server {
router: router} router: router}
currServer = server currServer = server
return server return server, nil
} }
func (server *Server) Run() error { func (server *Server) Run() error {