handle.go 742 B

123456789101112131415161718192021222324252627282930
  1. package http
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "net/http"
  6. )
  7. type (
  8. NotFound struct {
  9. }
  10. NotAllowed struct {
  11. }
  12. )
  13. func (n NotFound) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
  14. writer.WriteHeader(http.StatusNotFound)
  15. json.NewEncoder(writer).Encode(responsePayload{
  16. Code: http.StatusNotFound,
  17. Reason: fmt.Sprintf("requested URL %s was not found on this server", request.URL.Path),
  18. })
  19. }
  20. func (n NotAllowed) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
  21. writer.WriteHeader(http.StatusMethodNotAllowed)
  22. json.NewEncoder(writer).Encode(responsePayload{
  23. Code: http.StatusMethodNotAllowed,
  24. Reason: fmt.Sprintf("%s URL %s was not allow on this server", request.Method, request.URL.Path),
  25. })
  26. }