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) }
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) }
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) }
func AdvancedShortlinkCreationHandler(w http.ResponseWriter, r *http.Request) { log.Debug("AdvancedShortlinkCreationHandler") //Load params decoder := json.NewDecoder(r.Body) var request AdvancedShortlinkRequest err := decoder.Decode(&request) if err != nil { http.Error(w, "Invalid request body", 400) return } origin := request.Origin token := request.Token token, err = RegisterShortlink(origin, token) if err != nil { http.Error(w, err.Error(), 404) return } creationResult := CreationResult{origin, "http://" + r.Host + "/" + token} json.NewEncoder(w).Encode(creationResult) }