Beispiel #1
0
func render(w http.ResponseWriter, templateName string, data interface{}) {
	path, _ := filepath.Abs(templateDirectory + "/" + templateName)
	tmpl, err := template.ParseFiles(path)
	if err != nil {
		log.Error("Failed to parse template", log.Fields{"template": templateName, "error": err})
	}
	err = tmpl.Execute(w, data)
	if err != nil {
		log.Error("Failed to execute template", log.Fields{"template": templateName, "data": data, "error": err})
	}
}
Beispiel #2
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)
	})
}
Beispiel #3
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)
	})
}
Beispiel #4
0
func initLogger() {
	levelString := config.GetString(keys.LogLevel)
	level := log.InfoLevel
	if levelString != "" {
		if parsedLevel, err := log.ParseLevel(levelString); err != nil {
			log.Error("Unrecognised log level", log.Fields{
				"level": levelString,
			})
		} else {
			level = parsedLevel
		}
	}

	log.SetLevel(level)
}
Beispiel #5
0
func createWizardActionHandler(w http.ResponseWriter, r *http.Request, context *context) {

	user := context.User
	router := context.Router
	session := context.Session

	wizard := extractWizardFromRequest(r, user.ID)
	validator := wizards.NewValidator(router.wizardDao)
	validationErrs, err := validator.Validate(wizard)
	if err != nil {
		custom500Handler(w, r)
		return
	}

	if len(validationErrs) == 0 {
		if err := router.wizardDao.Insert(wizard); err != nil {
			log.Error("Error occurred while inserting wizard", log.Fields{"error": err})
			custom500Handler(w, r)
			return
		}

		// Send the user back to the dasboard
		dashboardUrl := router.Dashboard()
		http.Redirect(w, r, dashboardUrl.String(), http.StatusSeeOther)
	} else {
		// Add the errors to a flash message so that we can access them
		// after redirection
		session.AddFlash(validationErrs, "errs")
		if err := session.Save(r, w); err != nil {
			http.Error(w, err.Error(), http.StatusInternalServerError)
			return
		}

		// Send the user back to the creation page
		wizardCreationUrl := router.WizardCreation()
		http.Redirect(w, r, wizardCreationUrl.String(), http.StatusSeeOther)
	}
}