コード例 #1
0
ファイル: webui.go プロジェクト: roboticdog/mernessa_copy
// Handler for main information to be read and written from
func handler(w http.ResponseWriter, r *http.Request) {
	// Create a new appengine context for logging purposes
	c := appengine.NewContext(r)

	// set the appengine transport using the http request
	backend.SetTransport(r)

	// get the current user
	user := getUser(w, r, c)

	// update the new cache on submit
	cookie, _ := r.Cookie("submitting")
	if cookie != nil && cookie.Value == "true" {
		err := updatingCache_User(r, c, user)
		if err != nil {
			c.Errorf(err.Error())
		}
		http.SetCookie(w, &http.Cookie{Name: "submitting", Value: ""})
	}

	// Send to tag subpage
	if r.URL.Path == "/tag" && r.FormValue("tagSearch") != "" {
		tagHandler(w, r, c, user)
		return
	}

	// Send to user subpage
	if r.URL.Path == "/user" {
		userHandler(w, r, c, user)
		return
	}

	//Send to viewTags page
	if r.URL.Path == "/viewTags" {
		viewTagsHandler(w, r, c, user)
		return
	}

	//Send to viewUsers page
	if r.URL.Path == "/viewUsers" {
		viewUsersHandler(w, r, c, user)
		return
	}

	if r.URL.Path == "/userPage" {
		userPageHandler(w, r, c, user)
		return
	}

	if r.URL.Path == "/search" {
		searchHandler(w, r, c, user)
		return
	}

	page := template.Must(template.ParseFiles("public/template.html"))
	pageQuery := []string{
		"",
		"",
	}
	// WriteResponse creates a new response with the various caches
	if err := page.Execute(w, writeResponse(user, data, c, pageQuery)); err != nil {
		c.Criticalf("%v", err.Error())
	}
}
コード例 #2
0
ファイル: webui.go プロジェクト: laurenmanzo/mernessa
// Handler for main information to be read and written from.
// Does following functions in order:
//   Refreshes sql db for changes to questions in SE API.
//   Updates local cache if there's been changes to the db.
//   Gets the current user to send in response.
//   If a form has been submitted, the local cache and db gets updated with new values
//   Finds the current subpage and redirects to the relevant handler
func handler(w http.ResponseWriter, r *http.Request) {

	// Set context for logging
	ctx := appengine.NewContext(r)

	if checkForDBConnection() == false {
		connectToDB(ctx)
	}

	backend.SetTransport(ctx)
	if strings.HasPrefix(r.URL.Path, "/pullNewQn") {
		newQnHandler(w, r, ctx)
		return
	}

	// Pull any new questions added to StackOverflow
	lastPull = updateDB(db, ctx, lastPull)

	// Get the current user
	user := getUser(w, r, ctx)

	// Collect page number
	pageNum, _ := strconv.Atoi(r.FormValue("page"))
	if pageNum == 0 {
		pageNum = 1
	}

	// Update the new cache on submit if submitting cookie is set
	cookie, _ := r.Cookie("submitting")
	if cookie != nil && cookie.Value == "true" {
		// Update the cache based on the form values sent in the request
		updateTime, err := updatingCache_User(ctx, r, user)
		if err != nil {
			log.Errorf(ctx, "Error updating cache: %v", err.Error())
		} else {
			mostRecentUpdate = updateTime
		}

		// Removing the cookie
		http.SetCookie(w, &http.Cookie{Name: "submitting", Value: ""})
	}

	// Send to valid subpages
	// else errorHandler
	if strings.HasPrefix(r.URL.Path, "/?") || strings.HasPrefix(r.URL.Path, "/home") || r.URL.Path == "/" {
		// Get data to send to page
		data, updateTime, err := readFromDb(ctx, "")
		if err != nil {
			log.Errorf(ctx, "Error reading from db: %v", err.Error())
		} else {
			mostRecentUpdate = updateTime
		}

		// Parse the html template to serve to the page
		page := template.Must(template.ParseFiles("public/template.html"))
		pageQuery := []string{
			"",
			"",
		}

		// WriteResponse creates a new response with the various caches
		if err := page.Execute(w, writeResponse(user, data, pageNum, pageQuery)); err != nil {
			log.Errorf(ctx, "%v", err.Error())
		}
	} else if strings.HasPrefix(r.URL.Path, "/tag") && r.FormValue("tagSearch") != "" {
		tagHandler(w, r, ctx, pageNum, user)
	} else if strings.HasPrefix(r.URL.Path, "/user") {
		userHandler(w, r, ctx, pageNum, user)
	} else if strings.HasPrefix(r.URL.Path, "/viewTags") {
		viewTagsHandler(w, r, ctx, pageNum, user)
	} else if strings.HasPrefix(r.URL.Path, "/viewUsers") {
		viewUsersHandler(w, r, ctx, pageNum, user)
	} else if strings.HasPrefix(r.URL.Path, "/search") {
		searchHandler(w, r, ctx, pageNum, user)
	} else if strings.HasPrefix(r.URL.Path, "/addQuestion") {
		addQuestionHandler(w, r, ctx, pageNum, user)
	} else if strings.HasPrefix(r.URL.Path, "/addNewQuestion") {
		addNewQuestionToDatabaseHandler(w, r, ctx)
	} else {
		errorHandler(w, r, ctx, http.StatusNotFound, "")
	}
}