From 23f9d45ed5c289e56da38169571732b7dd8d2f85 Mon Sep 17 00:00:00 2001 From: Daniel Krol Date: Thu, 21 Jul 2022 15:14:21 -0400 Subject: [PATCH] More useful 404, particularly for API version change --- server/server.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/server/server.go b/server/server.go index 0f34f2e..3dc6d52 100644 --- a/server/server.go +++ b/server/server.go @@ -21,6 +21,9 @@ const PathRegister = PathPrefix + "/signup" const PathPassword = PathPrefix + "/password" const PathWallet = PathPrefix + "/wallet" +const PathUnknownEndpoint = PathPrefix + "/" +const PathWrongApiVersion = "/api/" + type Server struct { auth auth.AuthInterface store store.StoreInterface @@ -177,12 +180,25 @@ func validateEmail(email auth.Email) bool { // TODO - both wallet and token requests should be PUT, not POST. // PUT = "...creates a new resource or replaces a representation of the target resource with the request payload." +func (s *Server) unknownEndpoint(w http.ResponseWriter, req *http.Request) { + errorJson(w, http.StatusNotFound, "Unknown Endpoint") + return +} + +func (s *Server) wrongApiVersion(w http.ResponseWriter, req *http.Request) { + errorJson(w, http.StatusNotFound, "Wrong API version. Current version is "+ApiVersion+".") + return +} + func (s *Server) Serve() { http.HandleFunc(PathAuthToken, s.getAuthToken) http.HandleFunc(PathWallet, s.handleWallet) http.HandleFunc(PathRegister, s.register) http.HandleFunc(PathPassword, s.changePassword) + http.HandleFunc(PathUnknownEndpoint, s.unknownEndpoint) + http.HandleFunc(PathWrongApiVersion, s.wrongApiVersion) + fmt.Println("Serving at localhost:8090") http.ListenAndServe("localhost:8090", nil) }