-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Cleaned up a bit, added Api Add/Update handling
- Loading branch information
Martin
committed
May 8, 2014
1 parent
81d1d96
commit 2c89baa
Showing
5 changed files
with
120 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package main | ||
|
||
import( | ||
"fmt" | ||
"net/http" | ||
"encoding/json" | ||
) | ||
|
||
type ApiModifyKeySuccess struct { | ||
Key string `json:"key"` | ||
Status string `json:"status"` | ||
Action string `json:"action"` | ||
} | ||
|
||
func addKeyHandler(w http.ResponseWriter, r *http.Request) { | ||
keyName := r.URL.Path[len("/tyk/key/"):] | ||
success := true | ||
|
||
if r.Method == "POST" || r.Method == "PUT" { | ||
decoder := json.NewDecoder(r.Body) | ||
var newSession SessionState | ||
err := decoder.Decode(&newSession) | ||
|
||
if err != nil { | ||
log.Error("Couldn't decode new session object") | ||
log.Error(err) | ||
success = false | ||
} else { | ||
// Update our session object (create it) | ||
authManager.UpdateSession(keyName, newSession) | ||
} | ||
|
||
var responseMessage []byte | ||
var action string | ||
if r.Method == "POST" { | ||
action = "added" | ||
} else { | ||
action = "modified" | ||
} | ||
|
||
if success { | ||
response := ApiModifyKeySuccess{ | ||
keyName, | ||
"ok", | ||
action} | ||
|
||
responseMessage, err = json.Marshal(&response) | ||
|
||
if err != nil { | ||
log.Error("Could not create response message") | ||
log.Error(err) | ||
responseMessage = []byte(systemError) | ||
} | ||
} | ||
|
||
fmt.Fprintf(w, string(responseMessage)) | ||
} else { | ||
fmt.Fprintf(w, string(systemError)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package main | ||
|
||
import( | ||
"fmt" | ||
"net/http" | ||
"net/http/httputil" | ||
) | ||
|
||
type ApiError struct { | ||
Message string | ||
} | ||
|
||
func handler(p *httputil.ReverseProxy) func(http.ResponseWriter, *http.Request) { | ||
return func(w http.ResponseWriter, r *http.Request) { | ||
|
||
// Check for API key existence | ||
authHeaderValue := r.Header.Get("authorisation") | ||
if authHeaderValue != "" { | ||
// Check if API key valid | ||
key_authorised, thisSessionState := authManager.IsKeyAuthorised(authHeaderValue) | ||
if key_authorised { | ||
// If valid, check if within rate limit | ||
forwardMessage := sessionLimiter.ForwardMessage(&thisSessionState) | ||
if forwardMessage { | ||
success_handler(w, r, p) | ||
} else { | ||
handle_error(w, r, "Rate limit exceeded", 429) | ||
} | ||
authManager.UpdateSession(authHeaderValue, thisSessionState) | ||
} else { | ||
handle_error(w, r, "Key not authorised", 403) | ||
} | ||
} else { | ||
handle_error(w, r, "Authorisation field missing", 400) | ||
} | ||
} | ||
} | ||
|
||
func success_handler(w http.ResponseWriter, r *http.Request, p *httputil.ReverseProxy) { | ||
p.ServeHTTP(w, r) | ||
} | ||
|
||
func handle_error(w http.ResponseWriter, r *http.Request, err string, err_code int) { | ||
w.WriteHeader(err_code) | ||
thisError := ApiError{fmt.Sprintf("%s", err)} | ||
templates.ExecuteTemplate(w, "error.json", &thisError) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
{ | ||
"error": {{error_message}} | ||
"error": "{{.Message}}" | ||
} |