Example #1
0
func Auth(store *db.Storage) func(http.Handler) http.Handler {
	return func(h http.Handler) http.Handler {
		return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
			var err error
			defer func() {
				if err != nil {
					data, _ := models.NewErr("authentication failed").JSON()
					w.Header().Set("Content-Type", fmt.Sprintf("%s; charset=%s", "application/json", "utf-8"))
					w.Header().Set("Content-Length", fmt.Sprintf("%d", len(data)))
					fmt.Fprintf(w, string(data))
				}
			}()
			params := r.URL.Query()
			token := params.Get("token")

			fmt.Println(time.Now())
			str, err := store.GetStr("mus:token")
			fmt.Println(time.Now())
			if err != nil {
				return
			}

			if str == token {
				h.ServeHTTP(w, r)
			}
			return
		})
	}
}
Example #2
0
func JsonView(fn func(w http.ResponseWriter, r *http.Request) (string, error)) http.HandlerFunc {

	return func(w http.ResponseWriter, r *http.Request) {

		data, err := fn(w, r)
		if err != nil {

			if v, ok := err.(errors.DropboxError); ok {
				e, _ := models.NewErr(v.GetMessage()).JSON()
				data = string(e)
			} else {
				e, _ := models.NewErr(err.Error()).JSON()
				data = string(e)
			}

		}
		w.Header().Set("Content-Type", fmt.Sprintf("%s; charset=%s", "application/json", "utf-8"))
		w.Header().Set("Content-Length", fmt.Sprintf("%d", len(data)))
		fmt.Fprintf(w, data)

	}
}