Refactor route handlers

This commit is contained in:
Marc Di Luzio 2020-06-04 17:21:23 +01:00
parent fd4b4205ba
commit b95d92350a
3 changed files with 66 additions and 77 deletions

View file

@ -3,6 +3,7 @@ package server
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"io"
"net/http" "net/http"
"github.com/google/uuid" "github.com/google/uuid"
@ -10,10 +11,40 @@ import (
"github.com/mdiluz/rove/pkg/version" "github.com/mdiluz/rove/pkg/version"
) )
// RequestHandler describes a function that handles any incoming request and can respond
type RequestHandler func(io.ReadCloser, io.Writer) error
// Route defines the information for a single path->function route // Route defines the information for a single path->function route
type Route struct { type Route struct {
path string path string
handler func(http.ResponseWriter, *http.Request) method string
handler RequestHandler
}
// RequestHandlerHTTP wraps a request handler in http checks
func RequestHandlerHTTP(method string, handler RequestHandler) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
// Log the request
fmt.Printf("%s\t%s\n", r.Method, r.RequestURI)
// Verify we're hit with the right method
if r.Method != method {
w.WriteHeader(http.StatusMethodNotAllowed)
} else if err := handler(r.Body, w); err != nil {
// Log the error
fmt.Printf("Failed to handle http request: %s", err)
// Respond that we've had an error
w.WriteHeader(http.StatusInternalServerError)
} else {
// 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)
}
}
} }
// NewRouter sets up the server mux // NewRouter sets up the server mux
@ -23,29 +54,34 @@ func (s *Server) SetUpRouter() {
var routes = []Route{ var routes = []Route{
{ {
path: "/status", path: "/status",
method: http.MethodGet,
handler: s.HandleStatus, handler: s.HandleStatus,
}, },
{ {
path: "/register", path: "/register",
method: http.MethodPost,
handler: s.HandleRegister, handler: s.HandleRegister,
}, },
{ {
path: "/spawn", path: "/spawn",
method: http.MethodPost,
handler: s.HandleSpawn, handler: s.HandleSpawn,
}, },
{ {
path: "/commands", path: "/commands",
method: http.MethodPost,
handler: s.HandleCommands, handler: s.HandleCommands,
}, },
{ {
path: "/view", path: "/view",
method: http.MethodPost,
handler: s.HandleView, handler: s.HandleView,
}, },
} }
// Set up the handlers // Set up the handlers
for _, route := range routes { for _, route := range routes {
s.router.HandleFunc(route.path, route.handler) s.router.HandleFunc(route.path, RequestHandlerHTTP(route.method, route.handler))
} }
} }
@ -55,27 +91,19 @@ type StatusResponse struct {
Version string `json:"version"` Version string `json:"version"`
} }
// HandleStatus handles HTTP requests to the /status endpoint // HandleStatus handles the /status request
func (s *Server) HandleStatus(w http.ResponseWriter, r *http.Request) { func (s *Server) HandleStatus(b io.ReadCloser, w io.Writer) error {
fmt.Printf("%s\t%s\n", r.Method, r.RequestURI)
// Verify we're hit with a get request
if r.Method != http.MethodGet {
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
// Simply encode the current status
var response = StatusResponse{ var response = StatusResponse{
Ready: true, Ready: true,
Version: version.Version, Version: version.Version,
} }
// 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 // Reply with the current status
json.NewEncoder(w).Encode(response) json.NewEncoder(w).Encode(response)
return nil
} }
// BasicResponse describes the minimum dataset for a response // BasicResponse describes the minimum dataset for a response
@ -101,9 +129,8 @@ type RegisterResponse struct {
Id string `json:"id"` Id string `json:"id"`
} }
// HandleRegister handles HTTP requests to the /register endpoint // HandleRegister handles /register endpoint
func (s *Server) HandleRegister(w http.ResponseWriter, r *http.Request) { func (s *Server) HandleRegister(b io.ReadCloser, w io.Writer) error {
fmt.Printf("%s\t%s\n", r.Method, r.RequestURI)
// Set up the response // Set up the response
var response = RegisterResponse{ var response = RegisterResponse{
@ -112,15 +139,9 @@ func (s *Server) HandleRegister(w http.ResponseWriter, r *http.Request) {
}, },
} }
// Verify we're hit with a get request
if r.Method != http.MethodPost {
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
// Pull out the registration info // Pull out the registration info
var data RegisterData var data RegisterData
err := json.NewDecoder(r.Body).Decode(&data) err := json.NewDecoder(b).Decode(&data)
if err != nil { if err != nil {
fmt.Printf("Failed to decode json: %s\n", err) fmt.Printf("Failed to decode json: %s\n", err)
@ -143,15 +164,13 @@ func (s *Server) HandleRegister(w http.ResponseWriter, r *http.Request) {
} }
} }
// 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)
// Log the response // Log the response
fmt.Printf("\tresponse: %+v\n", response) fmt.Printf("\tresponse: %+v\n", response)
// Reply with the current status // Reply with the current status
json.NewEncoder(w).Encode(response) json.NewEncoder(w).Encode(response)
return nil
} }
// SpawnData is the data to be sent for the spawn command // SpawnData is the data to be sent for the spawn command
@ -167,15 +186,7 @@ type SpawnResponse struct {
} }
// HandleSpawn will spawn the player entity for the associated account // HandleSpawn will spawn the player entity for the associated account
func (s *Server) HandleSpawn(w http.ResponseWriter, r *http.Request) { func (s *Server) HandleSpawn(b io.ReadCloser, w io.Writer) error {
// Verify we're hit with a get request
if r.Method != http.MethodPost {
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
fmt.Printf("%s\t%s\n", r.Method, r.RequestURI)
// Set up the response // Set up the response
var response = SpawnResponse{ var response = SpawnResponse{
BasicResponse: BasicResponse{ BasicResponse: BasicResponse{
@ -185,7 +196,7 @@ func (s *Server) HandleSpawn(w http.ResponseWriter, r *http.Request) {
// Pull out the incoming info // Pull out the incoming info
var data SpawnData var data SpawnData
if err := json.NewDecoder(r.Body).Decode(&data); err != nil { if err := json.NewDecoder(b).Decode(&data); err != nil {
fmt.Printf("Failed to decode json: %s\n", err) fmt.Printf("Failed to decode json: %s\n", err)
response.Error = err.Error() response.Error = err.Error()
@ -208,15 +219,13 @@ func (s *Server) HandleSpawn(w http.ResponseWriter, r *http.Request) {
} }
} }
// 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)
// Log the response // Log the response
fmt.Printf("\tresponse: %+v\n", response) fmt.Printf("\tresponse: %+v\n", response)
// Reply with the current status // Reply with the current status
json.NewEncoder(w).Encode(response) json.NewEncoder(w).Encode(response)
return nil
} }
const ( const (
@ -241,15 +250,7 @@ type CommandsData struct {
} }
// HandleSpawn will spawn the player entity for the associated account // HandleSpawn will spawn the player entity for the associated account
func (s *Server) HandleCommands(w http.ResponseWriter, r *http.Request) { func (s *Server) HandleCommands(b io.ReadCloser, w io.Writer) error {
// Verify we're hit with a get request
if r.Method != http.MethodPost {
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
fmt.Printf("%s\t%s\n", r.Method, r.RequestURI)
// Set up the response // Set up the response
var response = BasicResponse{ var response = BasicResponse{
Success: false, Success: false,
@ -257,7 +258,7 @@ func (s *Server) HandleCommands(w http.ResponseWriter, r *http.Request) {
// Pull out the incoming info // Pull out the incoming info
var data CommandsData var data CommandsData
if err := json.NewDecoder(r.Body).Decode(&data); err != nil { if err := json.NewDecoder(b).Decode(&data); err != nil {
fmt.Printf("Failed to decode json: %s\n", err) fmt.Printf("Failed to decode json: %s\n", err)
response.Error = err.Error() response.Error = err.Error()
@ -269,6 +270,7 @@ func (s *Server) HandleCommands(w http.ResponseWriter, r *http.Request) {
} else if inst, err := s.accountant.GetPrimary(id); err != nil { } else if inst, err := s.accountant.GetPrimary(id); err != nil {
response.Error = fmt.Sprintf("Provided account has no primary: %s", err) response.Error = fmt.Sprintf("Provided account has no primary: %s", err)
} else { } else {
// log the data sent // log the data sent
fmt.Printf("\tcommands data: %v\n", data) fmt.Printf("\tcommands data: %v\n", data)
@ -290,15 +292,13 @@ func (s *Server) HandleCommands(w http.ResponseWriter, r *http.Request) {
} }
} }
// 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)
// Log the response // Log the response
fmt.Printf("\tresponse: %+v\n", response) fmt.Printf("\tresponse: %+v\n", response)
// Reply with the current status // Reply with the current status
json.NewEncoder(w).Encode(response) json.NewEncoder(w).Encode(response)
return nil
} }
// ViewData describes the input data to request an accounts current view // ViewData describes the input data to request an accounts current view
@ -312,15 +312,7 @@ type ViewResponse struct {
} }
// HandleView handles the view request // HandleView handles the view request
func (s *Server) HandleView(w http.ResponseWriter, r *http.Request) { func (s *Server) HandleView(b io.ReadCloser, w io.Writer) error {
// Verify we're hit with a get request
if r.Method != http.MethodPost {
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
fmt.Printf("%s\t%s\n", r.Method, r.RequestURI)
// Set up the response // Set up the response
var response = ViewResponse{ var response = ViewResponse{
BasicResponse: BasicResponse{ BasicResponse: BasicResponse{
@ -330,7 +322,7 @@ func (s *Server) HandleView(w http.ResponseWriter, r *http.Request) {
// Pull out the incoming info // Pull out the incoming info
var data CommandsData var data CommandsData
if err := json.NewDecoder(r.Body).Decode(&data); err != nil { if err := json.NewDecoder(b).Decode(&data); err != nil {
fmt.Printf("Failed to decode json: %s\n", err) fmt.Printf("Failed to decode json: %s\n", err)
response.Error = err.Error() response.Error = err.Error()
@ -348,13 +340,11 @@ func (s *Server) HandleView(w http.ResponseWriter, r *http.Request) {
fmt.Println(id) fmt.Println(id)
} }
// 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)
// Log the response // Log the response
fmt.Printf("\tresponse: %+v\n", response) fmt.Printf("\tresponse: %+v\n", response)
// Reply with the current status // Reply with the current status
json.NewEncoder(w).Encode(response) json.NewEncoder(w).Encode(response)
return nil
} }

View file

@ -16,7 +16,7 @@ func TestHandleStatus(t *testing.T) {
response := httptest.NewRecorder() response := httptest.NewRecorder()
s := NewServer() s := NewServer()
s.HandleStatus(response, request) RequestHandlerHTTP(http.MethodGet, s.HandleStatus)(response, request)
var status StatusResponse var status StatusResponse
json.NewDecoder(response.Body).Decode(&status) json.NewDecoder(response.Body).Decode(&status)
@ -41,7 +41,7 @@ func TestHandleRegister(t *testing.T) {
response := httptest.NewRecorder() response := httptest.NewRecorder()
s := NewServer() s := NewServer()
s.HandleRegister(response, request) RequestHandlerHTTP(http.MethodPost, s.HandleRegister)(response, request)
var status RegisterResponse var status RegisterResponse
json.NewDecoder(response.Body).Decode(&status) json.NewDecoder(response.Body).Decode(&status)
@ -63,7 +63,7 @@ func TestHandleSpawn(t *testing.T) {
request, _ := http.NewRequest(http.MethodPost, "/spawn", bytes.NewReader(b)) request, _ := http.NewRequest(http.MethodPost, "/spawn", bytes.NewReader(b))
response := httptest.NewRecorder() response := httptest.NewRecorder()
s.HandleSpawn(response, request) RequestHandlerHTTP(http.MethodPost, s.HandleSpawn)(response, request)
var status SpawnResponse var status SpawnResponse
json.NewDecoder(response.Body).Decode(&status) json.NewDecoder(response.Body).Decode(&status)
@ -99,7 +99,7 @@ func TestHandleCommands(t *testing.T) {
request, _ := http.NewRequest(http.MethodPost, "/commands", bytes.NewReader(b)) request, _ := http.NewRequest(http.MethodPost, "/commands", bytes.NewReader(b))
response := httptest.NewRecorder() response := httptest.NewRecorder()
s.HandleCommands(response, request) RequestHandlerHTTP(http.MethodPost, s.HandleCommands)(response, request)
var status BasicResponse var status BasicResponse
json.NewDecoder(response.Body).Decode(&status) json.NewDecoder(response.Body).Decode(&status)

View file

@ -33,8 +33,7 @@ type Server struct {
server *http.Server server *http.Server
router *mux.Router router *mux.Router
persistence int persistence int
persistenceLocation string
sync sync.WaitGroup sync sync.WaitGroup
} }