refactor: Change project structure to default (Standard Golang project layout)

This commit is contained in:
ChronosX88 2019-07-19 11:39:53 +04:00
parent b598613caa
commit 833bc6e2f4
Signed by: ChronosXYZ
GPG Key ID: 085A69A82C8C511A
14 changed files with 23 additions and 18 deletions

View File

@ -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", "")
}

View File

@ -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)

View File

@ -1,4 +1,4 @@
package main
package internal
const (
Version = "r0.5.0"

View File

@ -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) {

View File

@ -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{}

View File

@ -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
}

View File

@ -1,4 +1,4 @@
package main
package internal
import (
"crypto/rand"

View File

@ -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