|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"encoding/json"
|
|
|
|
"github.com/kreativmonkey/shrt/shrty"
|
|
|
|
)
|
|
|
|
|
|
|
|
type response struct {
|
|
|
|
Action string `json:"action"`
|
|
|
|
Status string `json:"status_code"`
|
|
|
|
Result interface{} `json:"result"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 APIRequest(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 APIshorten(w http.ResponseWriter, r *http.Request){
|
|
|
|
query := r.URL.Query()
|
|
|
|
queryu, err := url.QueryUnescape(query.Get("url"))
|
|
|
|
check(err)
|
|
|
|
|
|
|
|
var token string
|
|
|
|
err = short.Short(queryu, &token)
|
|
|
|
check(err)
|
|
|
|
|
|
|
|
respond := response{
|
|
|
|
Action: "shorten",
|
|
|
|
Result: r.Host + "/" + token,
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
if err := json.NewEncoder(w).Encode(&respond); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 APIlookup(w http.ResponseWriter, r *http.Request){
|
|
|
|
query := r.URL.Query()
|
|
|
|
queryu, err := url.QueryUnescape(query.Get("url_ending"))
|
|
|
|
check(err)
|
|
|
|
|
|
|
|
var result shrty.Data
|
|
|
|
var respond response
|
|
|
|
if ok := short.Get(queryu, &result); ok {
|
|
|
|
respond := response{
|
|
|
|
Action: "lookup",
|
|
|
|
Result: result,
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
if err := json.NewEncoder(w).Encode(&respond); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
|
|
|
|
w.WriteHeader(http.StatusNotFound)
|
|
|
|
if err := json.NewEncoder(w).Encode(&respond); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|