From 833bc6e2f4d23d70c06117df3c6b3ca4b4aa5d60 Mon Sep 17 00:00:00 2001 From: ChronosX88 Date: Fri, 19 Jul 2019 11:39:53 +0400 Subject: [PATCH 1/5] refactor: Change project structure to default (Standard Golang project layout) --- main.go => cmd/main.go | 8 +++++--- backend.go => internal/backend.go | 4 ++-- consts.go => internal/consts.go | 2 +- errors.go => internal/errors.go | 4 ++-- handlers.go => internal/handlers.go | 12 ++++++------ {models => internal/models}/enums.go | 0 {models => internal/models}/errors.go | 0 {models => internal/models}/replies.go | 0 {models => internal/models}/requests.go | 0 {models => internal/models}/sync.go | 0 {models => internal/models}/types.go | 0 server.go => internal/server.go | 5 ++++- tokenutils.go => internal/tokenutils.go | 2 +- users.go => internal/users.go | 4 ++-- 14 files changed, 23 insertions(+), 18 deletions(-) rename main.go => cmd/main.go (54%) rename backend.go => internal/backend.go (82%) rename consts.go => internal/consts.go (98%) rename errors.go => internal/errors.go (86%) rename handlers.go => internal/handlers.go (91%) rename {models => internal/models}/enums.go (100%) rename {models => internal/models}/errors.go (100%) rename {models => internal/models}/replies.go (100%) rename {models => internal/models}/requests.go (100%) rename {models => internal/models}/sync.go (100%) rename {models => internal/models}/types.go (100%) rename server.go => internal/server.go (93%) rename tokenutils.go => internal/tokenutils.go (96%) rename users.go => internal/users.go (98%) 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 From a92219372ec4c5dcfbe5faa26a8cf5838c686aaa Mon Sep 17 00:00:00 2001 From: ChronosX88 Date: Fri, 19 Jul 2019 11:40:15 +0400 Subject: [PATCH 2/5] chore: Add Makefile --- Makefile | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 Makefile diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..763a28a --- /dev/null +++ b/Makefile @@ -0,0 +1,5 @@ +all: + cd cmd; \ + go build +update-deps: + go mod tidy \ No newline at end of file From 3b5bc7a77b828cd7ecde37f4145dd43391b34a17 Mon Sep 17 00:00:00 2001 From: ChronosX88 Date: Fri, 19 Jul 2019 11:45:11 +0400 Subject: [PATCH 3/5] fix: Change port number of server (because Linux prohibits listening on 80 port for non-root users) --- internal/server.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/server.go b/internal/server.go index cb736de..f729f69 100644 --- a/internal/server.go +++ b/internal/server.go @@ -26,7 +26,7 @@ func New() *Server { router.HandleFunc("/", RootHandler) httpServer := new(http.Server) - httpServer.Addr = ":80" + httpServer.Addr = ":8008" httpServer.Handler = router server := &Server{ From 87815347d737cddd9d6fe34b296f531f43208b0b Mon Sep 17 00:00:00 2001 From: ChronosX88 Date: Fri, 19 Jul 2019 11:45:50 +0400 Subject: [PATCH 4/5] other: Add exec to .gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index f1c181e..7d2734e 100644 --- a/.gitignore +++ b/.gitignore @@ -10,3 +10,4 @@ # Output of the go coverage tool, specifically when used with LiteIDE *.out +/cmd/cmd From d73c80740772624617dd39db38efc6d496b18161 Mon Sep 17 00:00:00 2001 From: ChronosX88 Date: Fri, 19 Jul 2019 13:29:59 +0400 Subject: [PATCH 5/5] chore: Change .travis.yml --- .travis.yml | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/.travis.yml b/.travis.yml index 0fa3cee..16fb851 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,8 +1,10 @@ language: go sudo: false go: - - tip + - 1.12.x +env: + - GO111MODULE=on before_install: - - go get github.com/mattn/goveralls -script: - - $GOPATH/bin/goveralls -service=travis-ci + - make update-deps +install: + - make \ No newline at end of file