mirror of
https://github.com/idanoo/GoScrobble
synced 2025-07-01 13:42:20 +00:00
Basic auth flow
This commit is contained in:
parent
965df85383
commit
50154a009c
8 changed files with 265 additions and 20 deletions
|
@ -113,6 +113,15 @@ func generateJsonMessage(m string) []byte {
|
|||
return js
|
||||
}
|
||||
|
||||
// generateJsonError - Generates a err:str response
|
||||
func generateJsonError(m string) []byte {
|
||||
jr := jsonResponse{
|
||||
Err: m,
|
||||
}
|
||||
js, _ := json.Marshal(&jr)
|
||||
return js
|
||||
}
|
||||
|
||||
// tokenMiddleware - Validates token to a user
|
||||
func tokenMiddleware(next http.HandlerFunc) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
|
@ -136,7 +145,7 @@ func limitMiddleware(next http.HandlerFunc, limiter *IPRateLimiter) http.Handler
|
|||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
limiter := limiter.GetLimiter(r.RemoteAddr)
|
||||
if !limiter.Allow() {
|
||||
msg := generateJsonMessage("Too many requests")
|
||||
msg := generateJsonError("Too many requests")
|
||||
w.WriteHeader(http.StatusTooManyRequests)
|
||||
w.Write(msg)
|
||||
return
|
||||
|
@ -183,7 +192,7 @@ func handleLogin(w http.ResponseWriter, r *http.Request) {
|
|||
ip := getUserIp(r)
|
||||
data, err := loginUser(&logReq, ip)
|
||||
if err != nil {
|
||||
throwBadReq(w, err.Error())
|
||||
throwOkMessage(w, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue