diff --git a/main.go b/cmd/main.go similarity index 54% rename from main.go rename to cmd/main.go index 0b58403..4931593 100644 --- a/main.go +++ b/cmd/main.go @@ -2,16 +2,18 @@ package main import ( "log" + + internal "github.com/nxshock/signaller/internal" ) var ( - server *Server + server *internal.Server ) func init() { - server = New() + server = internal.New() server.Address = "localhost" - server.Backend = NewMemoryBackend() + server.Backend = internal.NewMemoryBackend() server.Backend.Register("andrew", "1", "") } diff --git a/backend.go b/internal/backend.go similarity index 82% rename from backend.go rename to internal/backend.go index db73228..62778d3 100644 --- a/backend.go +++ b/internal/backend.go @@ -1,6 +1,6 @@ -package main +package internal -import "github.com/nxshock/signaller/models" +import "github.com/nxshock/signaller/internal/models" type Backend interface { Register(username, password, device string) (token string, error *models.ApiError) diff --git a/consts.go b/internal/consts.go similarity index 98% rename from consts.go rename to internal/consts.go index 71708f5..d88649f 100644 --- a/consts.go +++ b/internal/consts.go @@ -1,4 +1,4 @@ -package main +package internal const ( Version = "r0.5.0" diff --git a/errors.go b/internal/errors.go similarity index 86% rename from errors.go rename to internal/errors.go index 2323ddd..0bfcf75 100644 --- a/errors.go +++ b/internal/errors.go @@ -1,9 +1,9 @@ -package main +package internal import ( "net/http" - "github.com/nxshock/signaller/models" + "github.com/nxshock/signaller/internal/models" ) func errorResponse(w http.ResponseWriter, code models.ApiError, httpCode int, message string) { diff --git a/handlers.go b/internal/handlers.go similarity index 91% rename from handlers.go rename to internal/handlers.go index bda8abd..8f17045 100644 --- a/handlers.go +++ b/internal/handlers.go @@ -1,4 +1,4 @@ -package main +package internal import ( "encoding/json" @@ -8,7 +8,7 @@ import ( "strconv" "strings" - "github.com/nxshock/signaller/models" + "github.com/nxshock/signaller/internal/models" ) func RootHandler(w http.ResponseWriter, r *http.Request) { @@ -58,7 +58,7 @@ func LoginHandler(w http.ResponseWriter, r *http.Request) { request.Identifier.User = strings.TrimPrefix(request.Identifier.User, "@") } - token, apiErr := server.Backend.Login(request.Identifier.User, request.Password, request.DeviceID) + token, apiErr := currServer.Backend.Login(request.Identifier.User, request.Password, request.DeviceID) if apiErr != nil { errorResponse(w, *apiErr, http.StatusForbidden, "") return @@ -87,7 +87,7 @@ func LogoutHandler(w http.ResponseWriter, r *http.Request) { return } - apiErr := server.Backend.Logout(token) + apiErr := currServer.Backend.Logout(token) if apiErr != nil { errorResponse(w, *apiErr, http.StatusBadRequest, "") // TODO: check code return @@ -112,7 +112,7 @@ func RegisterHandler(w http.ResponseWriter, r *http.Request) { var request models.RegisterRequest getRequest(r, &request) // TODO: handle error - token, apiErr := server.Backend.Register(request.Username, request.Password, request.DeviceID) + token, apiErr := currServer.Backend.Register(request.Username, request.Password, request.DeviceID) if apiErr != nil { errorResponse(w, *apiErr, http.StatusBadRequest, "") return @@ -145,7 +145,7 @@ func SyncHandler(w http.ResponseWriter, r *http.Request) { return } - response, _ := server.Backend.Sync(token, request) // TODO: handle error + response, _ := currServer.Backend.Sync(token, request) // TODO: handle error response.NextBatch = "123" response.Rooms = models.RoomsSyncReply{} diff --git a/models/enums.go b/internal/models/enums.go similarity index 100% rename from models/enums.go rename to internal/models/enums.go diff --git a/models/errors.go b/internal/models/errors.go similarity index 100% rename from models/errors.go rename to internal/models/errors.go diff --git a/models/replies.go b/internal/models/replies.go similarity index 100% rename from models/replies.go rename to internal/models/replies.go diff --git a/models/requests.go b/internal/models/requests.go similarity index 100% rename from models/requests.go rename to internal/models/requests.go diff --git a/models/sync.go b/internal/models/sync.go similarity index 100% rename from models/sync.go rename to internal/models/sync.go diff --git a/models/types.go b/internal/models/types.go similarity index 100% rename from models/types.go rename to internal/models/types.go diff --git a/server.go b/internal/server.go similarity index 93% rename from server.go rename to internal/server.go index 5812fa9..cb736de 100644 --- a/server.go +++ b/internal/server.go @@ -1,4 +1,4 @@ -package main +package internal import ( "net/http" @@ -6,6 +6,8 @@ import ( "github.com/gorilla/mux" ) +var currServer Server + type Server struct { httpServer *http.Server router *mux.Router @@ -31,6 +33,7 @@ func New() *Server { httpServer: httpServer, router: router} + currServer = *server return server } diff --git a/tokenutils.go b/internal/tokenutils.go similarity index 96% rename from tokenutils.go rename to internal/tokenutils.go index b42eee5..49ddf71 100644 --- a/tokenutils.go +++ b/internal/tokenutils.go @@ -1,4 +1,4 @@ -package main +package internal import ( "crypto/rand" diff --git a/users.go b/internal/users.go similarity index 98% rename from users.go rename to internal/users.go index d90fd3f..0654528 100644 --- a/users.go +++ b/internal/users.go @@ -1,4 +1,4 @@ -package main +package internal import ( "encoding/json" @@ -6,7 +6,7 @@ import ( "os" "sync" - "github.com/nxshock/signaller/models" + "github.com/nxshock/signaller/internal/models" ) var first bool