Shrty is a simple to use, go based link shortner with api connection.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

55 lines
1.2 KiB

package main
import (
"net/http"
"fmt"
"text/template"
)
type Page struct {
Host string
Title string
Body string
}
// Load the main Page with an Imput field for the URL to short.
func index(w http.ResponseWriter, r *http.Request) {
t, _ := template.ParseFiles("template/index.gohtml")
t.Execute(w, Page{
Host: config.Host,
Title: config.Title,
})
}
// Redirect to the URL behind the requested token.
func redirect(w http.ResponseWriter, r *http.Request){
if redirect, ok := short.Redirect(r.URL.Path[1:]); ok{
http.Redirect(w, r, string(redirect), http.StatusMovedPermanently)
fmt.Printf("Token: %s Redirect to: %s \n", string(r.URL.Path[1:]), redirect)
} else {
http.Redirect(w, r, "/", http.StatusMovedPermanently)
}
}
//
func shorten(w http.ResponseWriter, r *http.Request){
var token string
err := short.Short(r.FormValue("url"), &token)
if err != nil {
panic(err)
}
P := Page{Title: "Url:", Body: r.Host + "/" + token}
t, _ := template.ParseFiles("template/short.gohtml")
t.Execute(w, P)
}
func all(w http.ResponseWriter, r *http.Request){
t, _ := template.New("all").Parse("{{.}}")
t.Execute(w, short.GetAll())
}
func check(err error) {
if err != nil {
panic(err)
}
}