func (a *App) Shorten(w http.ResponseWriter, r *http.Request) {
	var id int64
	var body Body
	decoder := json.NewDecoder(r.Body)
	if err := decoder.Decode(&body); err != nil {
		respondWithError(w, http.StatusBadRequest, "Invalid request payload")
		return
	}
	defer r.Body.Close()
	url := body.URL
	if !isValidURL(url) {
		respondWithError(w, http.StatusBadRequest, "Invalid url")
		return
	}
	a.DB.QueryRow("SELECT id FROM shortened_urls WHERE long_url = ?", url).Scan(&id)
	if id == 0 {
		res, err := a.DB.Exec(`INSERT INTO shortened_urls (long_url, created) VALUES(?, now())`, url)
		if err != nil {
			respondWithError(w, http.StatusInternalServerError, err.Error())
			return
		}
		id, _ = res.LastInsertId()
	}
	hash := encode(id)
	body.URL = r.Host + "/" + hash
	sendResponse(w, http.StatusOK, body)
}
Shorten url POST method
package shortener
import (
	"encoding/json"
	"net/http"
	"net/url"
	"github.com/gorilla/mux"
)
type Body struct {
	URL string `json:"url"`
}
func (a *App) Home(w http.ResponseWriter, r *http.Request) {
	w.Write([]byte("Nothing to see here"))
}