Ejemplo n.º 1
0
func createUserIdentificationMiddleware(userDao *users.Dao) routes.Middleware {
	return routes.MiddlewareFunc(func(w http.ResponseWriter, r *http.Request, context *routes.Context, next routes.HandlerFunc) {
		username, password, ok := r.BasicAuth()
		if ok {
			user, err := userDao.GetByUsername(username)
			if err != nil {
				log.Error("Failed to fetch user from datastore", log.Fields{
					"username": username,
					"error":    err,
				})

				w.WriteHeader(http.StatusInternalServerError)
				w.Write(toJson(Error{
					Message: "An internal server error has occurred.",
					Code:    CodeInternalError,
				}))

				return
			}

			if user != nil {
				if user.VerifyPassword(password) {
					context.User = user
				}
			} else {
				// TODO
			}
		}

		next(w, r, context)
	})
}
Ejemplo n.º 2
0
func createRecoveryMiddleware() routes.Middleware {
	return routes.MiddlewareFunc(func(w http.ResponseWriter, r *http.Request, context *routes.Context, next routes.HandlerFunc) {
		defer func() {
			if recovery := recover(); recovery != nil {

				err, ok := recovery.(error)
				if !ok {
					err = fmt.Errorf("pkg: %v", recovery)
				}

				username := "******"
				if context.User != nil {
					username = context.User.Username
				}

				body, _ := ioutil.ReadAll(r.Body)
				log.Error("Panic occurred while serving API v1 request", log.Fields{
					"error": err,
					"url":   r.URL.String(),
					"user":  username,
					"body":  body,
					"stack": string(debug.Stack()),
				})

				w.WriteHeader(http.StatusInternalServerError)
				w.Write(toJson(Error{
					Message: "An internal server error has occurred.",
					Code:    CodeInternalError,
				}))
			}
		}()

		next(w, r, context)
	})
}
Ejemplo n.º 3
0
func createLoggerMiddleware() routes.Middleware {
	return routes.MiddlewareFunc(func(w http.ResponseWriter, r *http.Request, context *routes.Context, next routes.HandlerFunc) {

		username := "******"
		if context.User != nil {
			username = context.User.Username
		}

		log.Debug("Serving API v1 request", log.Fields{
			"url":  r.URL.String(),
			"user": username,
		})

		next(w, r, context)
	})
}