예제 #1
0
파일: router.go 프로젝트: crob1140/CodeWiz
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)
	})
}
예제 #2
0
파일: main.go 프로젝트: crob1140/CodeWiz
func main() {

	initLogger()

	dbDriver := config.GetString(keys.DatabaseDriver)
	assertConfigExists(keys.DatabaseDriver, dbDriver)

	dbDSN := config.GetString(keys.DatabaseDSN)
	assertConfigExists(keys.DatabaseDSN, dbDSN)

	port := config.GetString(keys.Port)
	assertConfigExists(keys.Port, port)

	migrationsPath := config.GetString(keys.DatabaseMigrationsPath)
	assertConfigExists(keys.DatabaseMigrationsPath, migrationsPath)

	log.Debug("Opening database connection", log.Fields{"driver": dbDriver, "dsn": dbDSN})
	ds, err := datastore.Open(dbDriver, dbDSN)
	if err != nil {
		log.Fatal("Failed to open database connection", log.Fields{
			"error": err,
		})
	}

	errs, ok := ds.UpSync(migrationsPath)
	if !ok {
		for _, err := range errs {
			log.Fatal("Failed to synchronise datastore tables", log.Fields{
				"error": err,
			})
		}
	}

	server := NewServer(ds)

	log.Info("Server is now listening for requests", log.Fields{
		"port": port,
	})
	server.ListenAndServe(":" + port)
}
예제 #3
0
파일: login.go 프로젝트: crob1140/CodeWiz
func loginActionHandler(w http.ResponseWriter, r *http.Request, context *context) {

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

	user, validationErrs, err := validateLoginRequest(r, router.userDao)
	if err != nil {
		custom500Handler(w, r)
		return
	}

	if len(validationErrs) == 0 {
		// Log the user in by saving their username as a session attribute
		session.Values["userID"] = user.ID
		if err := session.Save(r, w); err != nil {
			http.Error(w, err.Error(), http.StatusInternalServerError)
			return
		}

		log.Debug("User has logged in", log.Fields{"username": user.Username})

		// Redirect the user to the dashboard
		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 login page
		loginUrl := router.Login()
		http.Redirect(w, r, loginUrl.String(), http.StatusSeeOther)
	}
}