2018-11-19 07:03:33 -09:00
|
|
|
package pages
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2019-12-26 07:21:48 -09:00
|
|
|
"git.binarythought.com/cdramey/qurl/obj"
|
2018-11-19 07:03:33 -09:00
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
type apijson struct {
|
|
|
|
URL string `json:"url",omitempty`
|
|
|
|
Exists bool `json:"exists",omitempty`
|
|
|
|
Error string `json:"error",omitempty`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx *RootHandler) ServeAPI(w http.ResponseWriter, r *http.Request) {
|
|
|
|
var (
|
2019-12-26 07:21:48 -09:00
|
|
|
j apijson
|
|
|
|
q *obj.QURL
|
2018-11-19 07:03:33 -09:00
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
|
|
|
u := r.FormValue("url")
|
|
|
|
|
|
|
|
if u == "" {
|
|
|
|
j.Error = "Not a valid URL."
|
|
|
|
goto complete
|
|
|
|
}
|
|
|
|
|
|
|
|
q, err = ctx.Storage.GetQURLByURL(u)
|
|
|
|
if err != nil {
|
|
|
|
j.Error = err.Error()
|
|
|
|
goto complete
|
|
|
|
}
|
|
|
|
|
|
|
|
// Deal with URLs that already exist in the database
|
|
|
|
if q != nil {
|
|
|
|
j.Exists = true
|
2019-12-26 07:21:48 -09:00
|
|
|
j.URL = fmt.Sprintf("https://qurl.org/%s", obj.ToString(q.ID))
|
2018-11-19 07:03:33 -09:00
|
|
|
goto complete
|
|
|
|
}
|
|
|
|
|
2019-12-26 07:21:48 -09:00
|
|
|
q = &obj.QURL{
|
2018-11-19 07:03:33 -09:00
|
|
|
URL: u,
|
|
|
|
Created: time.Now(),
|
|
|
|
}
|
|
|
|
if h, _, err := net.SplitHostPort(r.RemoteAddr); err == nil && h != "" {
|
|
|
|
q.IP = net.ParseIP(h)
|
|
|
|
}
|
|
|
|
if b := r.Header.Get("User-Agent"); b != "" {
|
|
|
|
q.Browser = b
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the URL we're adding is valid
|
|
|
|
err = q.CheckValid()
|
|
|
|
if err != nil {
|
|
|
|
j.Error = err.Error()
|
|
|
|
goto complete
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the URL
|
|
|
|
err = ctx.Storage.AddQURL(q)
|
|
|
|
if err != nil {
|
|
|
|
j.Error = err.Error()
|
|
|
|
goto complete
|
|
|
|
}
|
|
|
|
|
2019-12-26 07:21:48 -09:00
|
|
|
j.URL = fmt.Sprintf("https://qurl.org/%s", obj.ToString(q.ID))
|
2018-11-19 07:03:33 -09:00
|
|
|
|
|
|
|
complete:
|
|
|
|
|
|
|
|
var buf bytes.Buffer
|
|
|
|
encoder := json.NewEncoder(&buf)
|
|
|
|
err = encoder.Encode(j)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, fmt.Sprintf("JSON encoding error: %s", err.Error()),
|
|
|
|
http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
w.Header().Set("Content-Length", fmt.Sprintf("%d", buf.Len()))
|
|
|
|
w.Write(buf.Bytes())
|
|
|
|
}
|