Skip to content

Commit

Permalink
feat(readonly): add check for readonly & GET in middleware
Browse files Browse the repository at this point in the history
if in readonly mode, check to make sure we are only allowing GET
requests through
  • Loading branch information
ramfox committed Mar 23, 2018
1 parent 832f7e3 commit 92a2e84
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion api/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ package api

import (
"context"
"fmt"
"net/http"
"time"

util "github.com/datatogether/api/apiutil"
)

// middleware handles request logging
Expand All @@ -30,10 +33,19 @@ func (s *Server) middleware(handler http.HandlerFunc) http.HandlerFunc {
// }
s.addCORSHeaders(w, r)

handler(w, r)
if ok := s.readOnlyCheck(r); ok {
handler(w, r)
} else {
util.WriteErrResponse(w, http.StatusForbidden, fmt.Errorf("qri server is in read-only mode, only certain GET requests are allowed"))
}
}
}

func (s *Server) readOnlyCheck(r *http.Request) bool {
// return !s.cfg.ReadOnly || r.Method == "GET"
return true
}

// addCORSHeaders adds CORS header info for whitelisted servers
func (s *Server) addCORSHeaders(w http.ResponseWriter, r *http.Request) {
origin := r.Header.Get("Origin")
Expand Down

0 comments on commit 92a2e84

Please sign in to comment.