You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

78 lines
2.0 KiB
Go

package main
import (
"net/http"
"encoding/json"
"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{Title: "Shrt"})
}
// Redirect to the URL behind the requested token.
func redirect(w http.ResponseWriter, r *http.Request){
if redirect, ok := short.Get(r.URL.Path[1:]); ok{
http.Redirect(w, r, string(redirect), 301)
fmt.Printf("Token: %s Redirect to: %s \n", string(r.URL.Path[1:]), redirect)
} else {
http.Redirect(w, r, "/", 301)
}
}
//
func shorten(w http.ResponseWriter, r *http.Request){
var test string
token, err := short.Add(r.FormValue("url"), &test)
if err != nil {
panic(err)
}
P := Page{Title: "Url:", Body: r.Host + "/" + token}
t, _ := template.ParseFiles("template/short.gohtml")
t.Execute(w, P)
}
// GET: http://example.com/api/action/shorten?key=API_KEY_HERE&url=https://google.com&custom_ending=CUSTOM_ENDING
// Response: {"action": "shorten","result": "https://example.com/5kq"}
func shortenJSON(w http.ResponseWriter, r *http.Request){
decoder := json.NewDecoder(r.Body)
var response shrt
err := decoder.Decode(&response)
if err != nil {
panic(err)
}
defer r.Body.Close()
fmt.Println(response)
/*response := map[string]string{
"action": "shorten",
"result": "",
}*/
}
// GET: http://example.com/api/action/lookup?key=API_KEY_HERE&url_ending=5kq
// Response: {"action":"lookup","result": {"long_url": "https:\/\/google.com","created_at": {"date":"2016-02-12 15:20:34.000000","timezone_type":3,"timezone":"UTC"},"clicks":"0"}}
func lookup(w http.ResponseWriter, r *http.Request){
/*response := map[string]interface{}{
"action": "lookup",
"result": map[string]string{
"long_url": "",
"created_at": "",
"clicks": "",
}
}*/
}
func all(w http.ResponseWriter, r *http.Request){
t, _ := template.New("all").Parse("{{.}}")
t.Execute(w, short.All())
}