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
|
@ -3,28 +3,21 @@ package main
|
|||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
|
||||
"github.com/mdiluz/rove/pkg/rovegame"
|
||||
"github.com/mdiluz/rove/pkg/server"
|
||||
)
|
||||
|
||||
var port = flag.Int("port", 8080, "The port to host on")
|
||||
|
||||
func main() {
|
||||
server := server.NewServer(*port)
|
||||
|
||||
fmt.Println("Initialising...")
|
||||
|
||||
// Set up the world
|
||||
world := rovegame.NewWorld()
|
||||
fmt.Printf("World created\n\t%+v\n", world)
|
||||
|
||||
// Create a new router
|
||||
router := NewRouter()
|
||||
fmt.Printf("Router Created\n")
|
||||
server.Initialise()
|
||||
|
||||
// Set up the close handler
|
||||
c := make(chan os.Signal)
|
||||
|
@ -37,9 +30,5 @@ func main() {
|
|||
|
||||
fmt.Println("Initialised")
|
||||
|
||||
// Listen and serve the http requests
|
||||
fmt.Println("Serving HTTP")
|
||||
if err := http.ListenAndServe(fmt.Sprintf(":%d", *port), router); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
server.Run()
|
||||
}
|
||||
|
|
|
@ -1,13 +0,0 @@
|
|||
package main
|
||||
|
||||
import "github.com/google/uuid"
|
||||
|
||||
type Player struct {
|
||||
id uuid.UUID
|
||||
}
|
||||
|
||||
func NewPlayer() Player {
|
||||
return Player{
|
||||
id: uuid.New(),
|
||||
}
|
||||
}
|
|
@ -1,13 +0,0 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestNewPlayer(t *testing.T) {
|
||||
a := NewPlayer()
|
||||
b := NewPlayer()
|
||||
if a.id == b.id {
|
||||
t.Error("Player IDs matched")
|
||||
}
|
||||
}
|
|
@ -1,56 +0,0 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"github.com/gorilla/mux"
|
||||
"github.com/mdiluz/rove/pkg/rove"
|
||||
)
|
||||
|
||||
// NewRouter sets up the server mux
|
||||
func NewRouter() (router *mux.Router) {
|
||||
router = mux.NewRouter().StrictSlash(true)
|
||||
|
||||
// Set up the handlers
|
||||
router.HandleFunc("/status", HandleStatus)
|
||||
router.HandleFunc("/register", HandleRegister)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// HandleStatus handles HTTP requests to the /status endpoint
|
||||
func HandleStatus(w http.ResponseWriter, r *http.Request) {
|
||||
fmt.Printf("%s\t%s", r.Method, r.RequestURI)
|
||||
|
||||
var response = rove.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)
|
||||
}
|
||||
|
||||
// HandleRegister handles HTTP requests to the /register endpoint
|
||||
func HandleRegister(w http.ResponseWriter, r *http.Request) {
|
||||
fmt.Printf("%s\t%s", r.Method, r.RequestURI)
|
||||
|
||||
// TODO: Add this user to the server
|
||||
player := NewPlayer()
|
||||
var response = rove.RegisterResponse{
|
||||
Success: true,
|
||||
Id: player.id.String(),
|
||||
}
|
||||
|
||||
// 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)
|
||||
}
|
|
@ -1,24 +0,0 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/mdiluz/rove/pkg/rove"
|
||||
)
|
||||
|
||||
func TestHandleStatus(t *testing.T) {
|
||||
request, _ := http.NewRequest(http.MethodGet, "/status", nil)
|
||||
response := httptest.NewRecorder()
|
||||
|
||||
HandleStatus(response, request)
|
||||
|
||||
var status rove.StatusResponse
|
||||
json.NewDecoder(response.Body).Decode(&status)
|
||||
|
||||
if status.Ready != true {
|
||||
t.Errorf("got false for /status")
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue