Ejemplo n.º 1
0
func MonitoringHandler(w http.ResponseWriter, r *http.Request) {
	log.Debug("MonitoringHandler")
	//Load params
	vars := mux.Vars(r)
	token := vars["value"]
	shortlink, err := ReadFromRedis(token)
	if err != nil {
		http.Error(w, "Token not found", 404)
		return
	}
	json.NewEncoder(w).Encode(shortlink)
}
Ejemplo n.º 2
0
func ShortlinkCreationHandler(w http.ResponseWriter, r *http.Request) {
	log.Debug("ShortlinkCreationHandler")

	//Load params
	vars := mux.Vars(r)
	origin := vars["value"]
	token := r.FormValue("custom")
	origin = "http://" + origin

	token, err := RegisterShortlink(origin, token)
	if err != nil {
		http.Error(w, "Invalid origin parameter", 404)
		return
	}

	creationResult := CreationResult{origin, "http://" + r.Host + "/" + token}
	json.NewEncoder(w).Encode(creationResult)
}
Ejemplo n.º 3
0
func RedirectionHandler(w http.ResponseWriter, r *http.Request) {
	log.Debug("RedirectionHandler")

	// Extract request parameters
	vars := mux.Vars(r)
	token := vars["value"]

	// Fetch Shortlink in Redis
	shortlink, err := ReadFromRedis(token)
	if err != nil {
		http.Error(w, "Token not found", 404)
		return
	}

	// Increment
	shortlink.Count++
	UpdateShortlink(shortlink)

	log.Info("Redirecting http://" + r.Host + "/" + shortlink.Token + " to " + shortlink.Origin)
	// Redirect
	http.Redirect(w, r, shortlink.Origin, http.StatusFound)
}