Refactor into server object to handle registered accounts
This commit is contained in:
parent
eccb726f74
commit
93decc027b
13 changed files with 304 additions and 128 deletions
47
pkg/server/accounts.go
Normal file
47
pkg/server/accounts.go
Normal file
|
@ -0,0 +1,47 @@
|
|||
package server
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
// Account represents a registered user
|
||||
type Account struct {
|
||||
// Name simply describes the account and must be unique
|
||||
Name string
|
||||
|
||||
// id represents a unique ID per account and is set one registered
|
||||
id uuid.UUID
|
||||
}
|
||||
|
||||
// Accountant manages a set of accounts
|
||||
type Accountant struct {
|
||||
accounts []Account
|
||||
}
|
||||
|
||||
// NewAccountant creates a new accountant
|
||||
func NewAccountant() *Accountant {
|
||||
return &Accountant{}
|
||||
}
|
||||
|
||||
// RegisterAccount adds an account to the set of internal accounts
|
||||
func (a *Accountant) RegisterAccount(acc Account) (Account, error) {
|
||||
|
||||
// Set the account ID to a new UUID
|
||||
acc.id = uuid.New()
|
||||
|
||||
// Verify this acount isn't already registered
|
||||
for _, a := range a.accounts {
|
||||
if a.Name == acc.Name {
|
||||
return Account{}, fmt.Errorf("Account name already registered")
|
||||
} else if a.id == acc.id {
|
||||
return Account{}, fmt.Errorf("Account ID already registered")
|
||||
}
|
||||
}
|
||||
|
||||
// Simply add the account to the list
|
||||
a.accounts = append(a.accounts, acc)
|
||||
|
||||
return acc, nil
|
||||
}
|
49
pkg/server/accounts_test.go
Normal file
49
pkg/server/accounts_test.go
Normal file
|
@ -0,0 +1,49 @@
|
|||
package server
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestNewAccountant(t *testing.T) {
|
||||
// Very basic verify here for now
|
||||
accountant := NewAccountant()
|
||||
if accountant == nil {
|
||||
t.Error("Failed to create accountant")
|
||||
}
|
||||
}
|
||||
|
||||
func TestAccountant_RegisterAccount(t *testing.T) {
|
||||
|
||||
accountant := NewAccountant()
|
||||
|
||||
// Start by making two accounts
|
||||
|
||||
namea := "one"
|
||||
a := Account{Name: namea}
|
||||
acca, err := accountant.RegisterAccount(a)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
} else if acca.Name != namea {
|
||||
t.Errorf("Missmatched account name after register, expected: %s, actual: %s", namea, acca.Name)
|
||||
}
|
||||
|
||||
nameb := "two"
|
||||
b := Account{Name: nameb}
|
||||
accb, err := accountant.RegisterAccount(b)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
} else if accb.Name != nameb {
|
||||
t.Errorf("Missmatched account name after register, expected: %s, actual: %s", nameb, acca.Name)
|
||||
}
|
||||
|
||||
// Verify our accounts have differing IDs
|
||||
if acca.id == accb.id {
|
||||
t.Error("Duplicate account IDs fo separate accounts")
|
||||
}
|
||||
|
||||
// Verify another request gets rejected
|
||||
_, err = accountant.RegisterAccount(a)
|
||||
if err == nil {
|
||||
t.Error("Duplicate account name did not produce error")
|
||||
}
|
||||
}
|
97
pkg/server/router.go
Normal file
97
pkg/server/router.go
Normal file
|
@ -0,0 +1,97 @@
|
|||
package server
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"github.com/gorilla/mux"
|
||||
)
|
||||
|
||||
// NewRouter sets up the server mux
|
||||
func (s *Server) SetUpRouter() {
|
||||
s.router = mux.NewRouter().StrictSlash(true)
|
||||
|
||||
// Set up the handlers
|
||||
s.router.HandleFunc("/status", s.HandleStatus)
|
||||
s.router.HandleFunc("/register", s.HandleRegister)
|
||||
}
|
||||
|
||||
// StatusResponse is a struct that contains information on the status of the server
|
||||
type StatusResponse struct {
|
||||
Ready bool `json:"ready"`
|
||||
}
|
||||
|
||||
// HandleStatus handles HTTP requests to the /status endpoint
|
||||
func (s *Server) HandleStatus(w http.ResponseWriter, r *http.Request) {
|
||||
fmt.Printf("%s\t%s", r.Method, r.RequestURI)
|
||||
|
||||
// Verify we're hit with a get request
|
||||
if r.Method != http.MethodGet {
|
||||
w.WriteHeader(http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
|
||||
var response = StatusResponse{
|
||||
Ready: true,
|
||||
}
|
||||
|
||||
// Be a good citizen and set the header for the return
|
||||
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
|
||||
// Reply with the current status
|
||||
json.NewEncoder(w).Encode(response)
|
||||
}
|
||||
|
||||
// RegisterData describes the data to send when registering
|
||||
type RegisterData struct {
|
||||
Name string `json:"id"`
|
||||
}
|
||||
|
||||
// RegisterResponse describes the response to a register request
|
||||
type RegisterResponse struct {
|
||||
Id string `json:"id"`
|
||||
|
||||
Success bool `json:"success"`
|
||||
Error string `json:"error"`
|
||||
}
|
||||
|
||||
// HandleRegister handles HTTP requests to the /register endpoint
|
||||
func (s *Server) HandleRegister(w http.ResponseWriter, r *http.Request) {
|
||||
fmt.Printf("%s\t%s", r.Method, r.RequestURI)
|
||||
|
||||
// Verify we're hit with a get request
|
||||
if r.Method != http.MethodPost {
|
||||
w.WriteHeader(http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
|
||||
// Pull out the registration info
|
||||
var data RegisterData
|
||||
json.NewDecoder(r.Body).Decode(&data)
|
||||
|
||||
// Register the account with the server
|
||||
acc := Account{Name: data.Name}
|
||||
acc, err := s.accountant.RegisterAccount(acc)
|
||||
|
||||
// Set up the response
|
||||
var response = RegisterResponse{
|
||||
Success: false,
|
||||
}
|
||||
|
||||
// If we didn't fail, respond with the account ID string
|
||||
if err == nil {
|
||||
response.Success = true
|
||||
response.Id = acc.id.String()
|
||||
} else {
|
||||
response.Error = err.Error()
|
||||
}
|
||||
|
||||
// Be a good citizen and set the header for the return
|
||||
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
|
||||
// Reply with the current status
|
||||
json.NewEncoder(w).Encode(response)
|
||||
}
|
25
pkg/server/router_test.go
Normal file
25
pkg/server/router_test.go
Normal file
|
@ -0,0 +1,25 @@
|
|||
package server
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestHandleStatus(t *testing.T) {
|
||||
request, _ := http.NewRequest(http.MethodGet, "/status", nil)
|
||||
response := httptest.NewRecorder()
|
||||
|
||||
s := NewServer(8080)
|
||||
s.Initialise()
|
||||
|
||||
s.HandleStatus(response, request)
|
||||
|
||||
var status StatusResponse
|
||||
json.NewDecoder(response.Body).Decode(&status)
|
||||
|
||||
if status.Ready != true {
|
||||
t.Errorf("got false for /status")
|
||||
}
|
||||
}
|
49
pkg/server/server.go
Normal file
49
pkg/server/server.go
Normal file
|
@ -0,0 +1,49 @@
|
|||
package server
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"github.com/gorilla/mux"
|
||||
"github.com/mdiluz/rove/pkg/game"
|
||||
)
|
||||
|
||||
// Server contains the relevant data to run a game server
|
||||
type Server struct {
|
||||
port int
|
||||
|
||||
accountant *Accountant
|
||||
world *game.World
|
||||
|
||||
router *mux.Router
|
||||
}
|
||||
|
||||
// NewServer sets up a new server
|
||||
func NewServer(port int) *Server {
|
||||
return &Server{
|
||||
port: port,
|
||||
accountant: NewAccountant(),
|
||||
world: game.NewWorld(),
|
||||
}
|
||||
}
|
||||
|
||||
// Initialise sets up internal state ready to serve
|
||||
func (s *Server) Initialise() {
|
||||
// Set up the world
|
||||
s.world = game.NewWorld()
|
||||
fmt.Printf("World created\n\t%+v\n", s.world)
|
||||
|
||||
// Create a new router
|
||||
s.SetUpRouter()
|
||||
fmt.Printf("Routes Created\n")
|
||||
}
|
||||
|
||||
// Run executes the server
|
||||
func (s *Server) Run() {
|
||||
// Listen and serve the http requests
|
||||
fmt.Println("Serving HTTP")
|
||||
if err := http.ListenAndServe(fmt.Sprintf(":%d", s.port), s.router); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue