Improve api
This commit is contained in:
parent
e89eae4262
commit
b99c59b130
65
pastebin.go
65
pastebin.go
|
@ -6,7 +6,6 @@ import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"encoding/xml"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"html"
|
"html"
|
||||||
"html/template"
|
"html/template"
|
||||||
|
@ -54,6 +53,8 @@ var syntax, _ = ioutil.ReadFile("assets/syntax.html")
|
||||||
|
|
||||||
// Response API struct
|
// Response API struct
|
||||||
type Response struct {
|
type Response struct {
|
||||||
|
SUCCESS bool `json:"success"`
|
||||||
|
STATUS string `json:"status"`
|
||||||
ID string `json:"id"`
|
ID string `json:"id"`
|
||||||
TITLE string `json:"title"`
|
TITLE string `json:"title"`
|
||||||
SHA1 string `json:"sha1"`
|
SHA1 string `json:"sha1"`
|
||||||
|
@ -145,7 +146,7 @@ func Save(raw string, lang string, title string, expiry string) Response {
|
||||||
err := query.Scan(&id, &title, &hash, &paste, &delkey)
|
err := query.Scan(&id, &title, &hash, &paste, &delkey)
|
||||||
Check(err)
|
Check(err)
|
||||||
url := configuration.Address + "/p/" + id
|
url := configuration.Address + "/p/" + id
|
||||||
return Response{id, title, hash, url, len(paste), delkey}
|
return Response{true, "saved", id, title, hash, url, len(paste), delkey}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
id := GenerateName()
|
id := GenerateName()
|
||||||
|
@ -168,7 +169,7 @@ func Save(raw string, lang string, title string, expiry string) Response {
|
||||||
_, err = stmt.Exec(id, html.EscapeString(title), sha, dataEscaped, delKey, expiryTime)
|
_, err = stmt.Exec(id, html.EscapeString(title), sha, dataEscaped, delKey, expiryTime)
|
||||||
Check(err)
|
Check(err)
|
||||||
|
|
||||||
return Response{id, title, sha, url, len(dataEscaped), delKey}
|
return Response{true, "saved", id, title, sha, url, len(dataEscaped), delKey}
|
||||||
}
|
}
|
||||||
|
|
||||||
// DelHandler checks to see if delkey and pasteid exist in the database.
|
// DelHandler checks to see if delkey and pasteid exist in the database.
|
||||||
|
@ -176,7 +177,7 @@ func Save(raw string, lang string, title string, expiry string) Response {
|
||||||
func DelHandler(w http.ResponseWriter, r *http.Request) {
|
func DelHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
vars := mux.Vars(r)
|
vars := mux.Vars(r)
|
||||||
id := vars["pasteId"]
|
id := vars["pasteId"]
|
||||||
delkey := vars["delKey"]
|
delkey := r.FormValue("delkey")
|
||||||
|
|
||||||
db, err := sql.Open("mysql", DATABASE)
|
db, err := sql.Open("mysql", DATABASE)
|
||||||
Check(err)
|
Check(err)
|
||||||
|
@ -190,7 +191,13 @@ func DelHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
_, err = res.RowsAffected()
|
_, err = res.RowsAffected()
|
||||||
if err != sql.ErrNoRows {
|
if err != sql.ErrNoRows {
|
||||||
io.WriteString(w, id+" deleted")
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
b := Response{STATUS: "DELETED " + id}
|
||||||
|
err := json.NewEncoder(w).Encode(b)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -211,7 +218,7 @@ func SaveHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
b := Save(paste, lang, title, expiry)
|
b := Save(paste, lang, title, expiry)
|
||||||
|
|
||||||
switch output {
|
switch output {
|
||||||
case "json":
|
default:
|
||||||
w.Header().Set("Content-Type", "application/json")
|
w.Header().Set("Content-Type", "application/json")
|
||||||
err := json.NewEncoder(w).Encode(b)
|
err := json.NewEncoder(w).Encode(b)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -219,28 +226,8 @@ func SaveHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
case "xml":
|
|
||||||
x, err := xml.MarshalIndent(b, "", " ")
|
|
||||||
if err != nil {
|
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
w.Header().Set("Content-Type", "application/xml")
|
|
||||||
w.Write(x)
|
|
||||||
|
|
||||||
case "html":
|
|
||||||
w.Header().Set("Content-Type", "text/html")
|
|
||||||
io.WriteString(w, "<p><b>URL</b>: <a href='"+b.URL+"'>"+b.URL+"</a></p>")
|
|
||||||
io.WriteString(w, "<p><b>Delete Key</b>: <a href='"+configuration.Address+"/del/"+b.ID+"/"+b.DELKEY+"'>"+b.DELKEY+"</a></p>")
|
|
||||||
|
|
||||||
case "redirect":
|
case "redirect":
|
||||||
http.Redirect(w, r, b.URL, 301)
|
http.Redirect(w, r, b.URL, 301)
|
||||||
|
|
||||||
default:
|
|
||||||
w.Header().Set("Content-Type", "text/plain; charset=UTF-8; imeanit=yes")
|
|
||||||
io.WriteString(w, b.URL+"\n")
|
|
||||||
io.WriteString(w, "delete key: "+b.DELKEY+"\n")
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -269,7 +256,7 @@ func GetPaste(paste string, lang string) (string, string) {
|
||||||
var expiry string
|
var expiry string
|
||||||
err = db.QueryRow("select title, data, expiry from pastebin where id=?", param1).Scan(&title, &s, &expiry)
|
err = db.QueryRow("select title, data, expiry from pastebin where id=?", param1).Scan(&title, &s, &expiry)
|
||||||
Check(err)
|
Check(err)
|
||||||
if time.Now().Format("2006-01-02 15:04:05") > expiry {
|
if time.Now().Format("2006-01-02 15:04:05") >= expiry {
|
||||||
stmt, err := db.Prepare("delete from pastebin where id=?")
|
stmt, err := db.Prepare("delete from pastebin where id=?")
|
||||||
Check(err)
|
Check(err)
|
||||||
_, err = stmt.Exec(param1)
|
_, err = stmt.Exec(param1)
|
||||||
|
@ -288,6 +275,21 @@ func GetPaste(paste string, lang string) (string, string) {
|
||||||
return html.UnescapeString(s), html.UnescapeString(title)
|
return html.UnescapeString(s), html.UnescapeString(title)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// APIHandler handles get requests of pastes
|
||||||
|
func APIHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
vars := mux.Vars(r)
|
||||||
|
paste := vars["pasteId"]
|
||||||
|
|
||||||
|
b, _ := GetPaste(paste, "")
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
err := json.NewEncoder(w).Encode(b)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
// PasteHandler handles the generation of paste pages with the links
|
// PasteHandler handles the generation of paste pages with the links
|
||||||
func PasteHandler(w http.ResponseWriter, r *http.Request) {
|
func PasteHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
vars := mux.Vars(r)
|
vars := mux.Vars(r)
|
||||||
|
@ -399,14 +401,17 @@ func main() {
|
||||||
// create new mux router
|
// create new mux router
|
||||||
router := mux.NewRouter()
|
router := mux.NewRouter()
|
||||||
|
|
||||||
|
// serverside rending stuff
|
||||||
router.HandleFunc("/p/{pasteId}", PasteHandler).Methods("GET")
|
router.HandleFunc("/p/{pasteId}", PasteHandler).Methods("GET")
|
||||||
router.HandleFunc("/raw/{pasteId}", RawHandler).Methods("GET")
|
router.HandleFunc("/raw/{pasteId}", RawHandler).Methods("GET")
|
||||||
router.HandleFunc("/p/{pasteId}/{lang}", PasteHandler).Methods("GET")
|
router.HandleFunc("/p/{pasteId}/{lang}", PasteHandler).Methods("GET")
|
||||||
router.HandleFunc("/clone/{pasteId}", CloneHandler).Methods("GET")
|
router.HandleFunc("/clone/{pasteId}", CloneHandler).Methods("GET")
|
||||||
router.HandleFunc("/download/{pasteId}", DownloadHandler).Methods("GET")
|
router.HandleFunc("/download/{pasteId}", DownloadHandler).Methods("GET")
|
||||||
router.HandleFunc("/p", SaveHandler).Methods("POST")
|
// api
|
||||||
router.HandleFunc("/p/{output}", SaveHandler).Methods("POST")
|
router.HandleFunc("/api", SaveHandler).Methods("POST")
|
||||||
router.HandleFunc("/p/{pasteId}/{delKey}", DelHandler).Methods("DELETE")
|
router.HandleFunc("/api/{output}", SaveHandler).Methods("POST")
|
||||||
|
router.HandleFunc("/api/{pasteid}", APIHandler).Methods("GET")
|
||||||
|
router.HandleFunc("/api/{pasteId}", DelHandler).Methods("DELETE")
|
||||||
router.HandleFunc("/", RootHandler)
|
router.HandleFunc("/", RootHandler)
|
||||||
err = http.ListenAndServe(configuration.Port, router)
|
err = http.ListenAndServe(configuration.Port, router)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
Loading…
Reference in New Issue